> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/how-can-i-add-class-to-the-first-and-last-item-among-the-visible-items-of-owl-carousel-2-ok.md).

# How can I add class to the first and last item among the visible items of Owl Carousel 2? (ok)

```
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="owlcarousel.css">
    
  </head>

  <body>
    <div class="latest-work-carousel">
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
      <div class="item">
          <div class="work-content">
              <h4>Southland Arthritis <span>Branding • UX/UI • Web</span></h4>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec tortor finibus, blandit leo sed, tincidunt turpis. Morbi iaculis tortor eu sodales aliquam. Integer nibh purus,</p>
              <a href="#" title="View cASE Study" class="case-study-btn">View cASE Study</a>
          </div>
      </div>
  </div>
  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  
  <script src="owlcarousel.js"></script>
  <script src="script.js"></script>
    
  </body>

</html>
```

```
// Code goes here


jQuery(document).ready(function($) {
    
    var carousel = $(".latest-work-carousel");
    carousel.owlCarousel({
        loop : true,
        items : 3,
        margin:0,
        nav : true,
        dots : false,
    });

    checkClasses();
    carousel.on('translated.owl.carousel', function(event) {
        checkClasses();
    });

    function checkClasses(){
        var total = $('.latest-work-carousel .owl-stage .owl-item.active').length;
        
        $('.latest-work-carousel .owl-stage .owl-item').removeClass('firstActiveItem lastActiveItem');
        
        $('.latest-work-carousel .owl-stage .owl-item.active').each(function(index){
            if (index === 0) {
                // this is the first one
                $(this).addClass('firstActiveItem');
            }
            if (index === total - 1 && total>1) {
                // this is the last one
                $(this).addClass('lastActiveItem');
            }
        });
    }

        
});


```

```
/* Styles go here */

.firstActiveItem {
  background: red;
}

.lastActiveItem {
  background: blue;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/how-can-i-add-class-to-the-first-and-last-item-among-the-visible-items-of-owl-carousel-2-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
