Create Responsive Carousels with Owl Carousel | HTML, CSS, JavaScript Tutorial

Faraz

By Faraz -

Learn to build interactive and responsive carousels or sliders using Owl Carousel, HTML, CSS, and JavaScript with our comprehensive tutorial.


Create Responsive Carousels Slider with Owl Carousel.webp

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. JavaScript Code
  5. Conclusion
  6. Preview

Carousels and sliders have become essential elements in web design, allowing for the efficient display of multiple images or content within a limited space. Among the plethora of carousel plugins available, Owl Carousel stands out for its versatility and ease of use. In this tutorial, we'll delve into the process of creating dynamic and responsive carousels using Owl Carousel, accompanied by HTML, CSS, and JavaScript. Whether you're a novice developer or an experienced pro, this guide will walk you through each step, from setting up the HTML structure to adding advanced customization options. Let's embark on this journey to elevate your web design skills and create captivating carousels that enhance user experience.

Source Code

Step 1 (HTML Code):

To begin, create the basic HTML structure for your carousel. Define a container element where your carousel will reside, and within it, include the necessary HTML elements for each carousel item.

How to do it:

  • Create a <div> element with a unique ID to serve as the container for your carousel.
  • Inside the container, add <div> elements for each carousel item, typically with a class name like "item".

Let's break down the HTML code:

1. <!DOCTYPE html>: This declaration specifies the document type and version of HTML being used.

2. <html lang="en">: This is the opening tag for the HTML document. The lang="en" attribute specifies that the language of the document is English.

3. <head>: This section contains meta-information about the HTML document, such as the character encoding, viewport settings, and title.

  • <meta charset="UTF-8">: This specifies the character encoding for the document as UTF-8, which supports a wide range of characters.
  • <meta http-equiv="X-UA-Compatible" content="IE=edge">: This meta tag ensures that the document renders in the highest mode available in the user's browser (Internet Explorer).
  • <meta name="viewport" content="width=device-width, initial-scale=1.0">: This meta tag sets the viewport width to the device width and sets the initial zoom level to 1.0, making the webpage responsive on various devices.
  • <title>Carousel / Slider using Owl Carousel</title>: This sets the title of the webpage displayed in the browser's title bar or tab.
  • <link rel="stylesheet" href="https://cdnjs.cl../carousel.min.css">: This links to the CSS file of the Owl Carousel plugin, which provides styles for the carousel.
  • <link rel="stylesheet" href="https://cdnjs.clo../owl.theme.min.css">: This links to the theme CSS file of the Owl Carousel plugin, which provides additional styling options.
  • <link rel="stylesheet" href="styles.css">: This links to a custom CSS file named styles.css for additional styling of the carousel.
  • </head>: This closes the head section of the document.

4. <body>: This is the opening tag for the body section of the HTML document, where the content of the webpage is defined.

5. <div class="container">: This div element serves as a container for the carousel.

6. <div id="main-slider" class="owl-carousel">: This div element with the id main-slider and class owl-carousel is the main container for the carousel items. Owl Carousel will use this element to initialize the carousel.

7. <div class="slider-item equal-height style-6" style="height: 254px;">: These div elements represent individual slides in the carousel. Each slide contains an image (<img>), header (<div class="header">), description (<div class="desc">), and details (<div class="details quote">).

8. <script type="text/javascript" src="https://code.jqu.."></script>: This script tag imports the jQuery library, which is required for the Owl Carousel plugin.

9. <script type="text/javascript" src="https://cdnjs.cl../owl.carousel.min.js"></script>: This script tag imports the Owl Carousel plugin, which enables the carousel functionality.

10. <script src="script.js"></script>: This script tag links to a custom JavaScript file named script.js, which contains initialization and configuration for the Owl Carousel plugin.

11. </body>: This closes the body section of the HTML document.

12. </html>: This is the closing tag for the HTML document.

Step 2 (CSS Code):

Once the HTML structure is in place, style your carousel to match the design of your website. Use CSS to customize the appearance of the carousel, including its size, layout, navigation buttons, and transition effects.

How to do it:

  • Target the container and carousel items using CSS selectors.
  • Apply styles such as width, height, margins, and padding to achieve the desired layout.
  • Use CSS to customize the appearance of navigation buttons, pagination, and other controls.

Let's break down the CSS code:

1. body: This selector targets the <body> element of the HTML document. It sets the background color to "wheat".

2. .owl-carousel .owl-item img: This selector targets <img> elements within elements that have both the classes "owl-carousel" and "owl-item". It specifies styling for these images, including making them display as block elements, setting their width to 17% of their container, floating them to the left, adding a 5px solid white border, giving them a border-radius of 20px, and setting margins for spacing.

3. .slider-item.equal-height.style-6: This selector targets elements with the classes "slider-item", "equal-height", and "style-6". It sets the background color to #eee and gives them a border-radius of 10px, with a margin of 10px.

4. .cell-right: This selector targets elements with the class "cell-right" and sets the text alignment to center, adds a right margin of 80px, and adjusts padding for top and bottom spacing.

5. .header: This selector targets elements with the class "header" and sets the font weight to 600.

6. .details.quote: This selector targets elements with the classes "details" and "quote" and sets padding to 17px on the top and bottom, and 55px on the left and right.

7. et_right_sidebar #main-content .container:before: This selector targets the pseudo-element :before of elements with the class "container" within an element with the id "main-content" that is inside an element with the class "et_right_sidebar". It sets the display property to "none", effectively hiding it.

8. #main-content .container: This selector targets elements with the class "container" that is within an element with the id "main-content". It sets padding on the top to 10px.

9. i.fa.fa-quote-left: This selector targets <i> elements with the classes "fa" and "fa-quote-left". It sets padding on the left and right to 10px and changes the color to #999.

body{
  background-color: wheat;
}

.owl-carousel .owl-item img {
  display: block;
  width: 17%;
  float: left;
  border: 5px solid #fff;
  border-radius: 20px;
  margin-left: 54px;
  margin-right: 35px;
  margin-top: 15px;
}
.slider-item.equal-height.style-6 {
  background-color: #eee;
  border-radius: 10px;
  margin: 10px;
}
.cell-right {
  text-align: center;
  margin-right: 80px;
  padding-top: 35px;
  padding-bottom: 20px;
}
.header {
  font-weight: 600;
}
.details.quote {
  padding: 17px 55px;
}
.et_right_sidebar #main-content .container:before{
  display: none;
}
#main-content .container {
  padding-top: 10px;
}
i.fa.fa-quote-left {
  padding: 0px 10px;
  color: #999;
} 

Step 3 (JavaScript Code):

Next, add functionality to your carousel using JavaScript. Initialize the Owl Carousel plugin with the appropriate options and settings to enable smooth navigation, autoplay, and other interactive features.

How to do it:

  • Include the Owl Carousel JavaScript file in your HTML document.
  • Initialize the carousel by selecting the container element and calling the Owl Carousel function with the desired options.
  • Customize settings such as autoplay, navigation, slide speed, and responsive breakpoints as needed.

Here's an explanation of the code:

1. $(document).ready(function(){...});: This function ensures that the enclosed code executes only after the HTML document has been fully loaded and parsed.

2. $("#main-slider").owlCarousel({ ... });: This line selects the element with the ID "main-slider" and applies the Owl Carousel plugin to it.

3. Inside the curly braces {}, various configuration options for the carousel are provided:

  • items: 2: Specifies that two items will be visible in the carousel at a time.
  • itemsDesktop: [1000, 2]: Sets the number of items to be displayed when the screen width is 1000 pixels or more to 2.
  • itemsDesktopSmall: [980, 1]: Sets the number of items to be displayed when the screen width is between 980 and 1000 pixels to 1.
  • itemsTablet: [768, 1]: Sets the number of items to be displayed when the screen width is between 768 and 980 pixels to 1.
  • pagination: true: Enables pagination (usually dots or numbers indicating the current slide).
  • navigation: true: Enables navigation arrows (typically "<" for previous and ">" for next).
  • navigationText: ["<", ">"]: Defines custom navigation text, in this case, "<" for previous and ">" for next.
  • autoPlay: true: Enables automatic slideshow, where the slides transition automatically without user input.
$(document).ready(function(){
  $("#main-slider").owlCarousel({
      items:2,
      itemsDesktop:[1000,2],
      itemsDesktopSmall:[980,1],
      itemsTablet:[768,1],
      pagination:true,
      navigation:true,
      navigationText:["<",">"],
      autoPlay:true
  });
});

Final Output:

Create Responsive Carousels Slider with Owl Carousel.gif

See the Pen Untitled by Faraz (@codewithfaraz) on CodePen.

Conclusion:

In this tutorial, we've covered the basics of creating a carousel/slider using Owl Carousel, HTML, CSS, and JavaScript. By following these steps and exploring the customization options, you can create dynamic and visually appealing carousels for your website. Experiment with different configurations and unleash the full potential of Owl Carousel in your projects. Happy sliding!

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺