Create Pure CSS Carousel Sliders Using HTML and CSS

Faraz

By Faraz -

Learn how to create a pure CSS carousel slider using HTML and CSS, without JavaScript or jQuery, and make it responsive for your website.


carousel.png

Table of Contents

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

Surely you have seen many carousel sliders built with JavaScript. But how about creating one with just pure CSS? You probably think JavaScript sliders work great on all modern browsers, don't you?

This myth is very old now because, after the invention of CSS3, most functions can be easily reproduced with CSS.

Carousel sliders are a popular way to showcase multiple images or products on a website. By using pure CSS to create a slider, without any JavaScript or jQuery, you can reduce page load times and improve website performance. So today I'm going to create a simple carousel slider using HTML and Pure CSS

Let's start making these amazing pure CSS carousels/sliders Using HTML and CSS step by step.

Join My Telegram Channel to Download the Project: Click Here

Prerequisites:

Before starting this tutorial, you should have a basic understanding of HTML, and CSS. Additionally, you will need a code editor such as Visual Studio Code or Sublime Text to write and save your code.

Source Code

Step 1 (HTML Code):

The first thing we need to do is create our HTML File. We'll start with well-organized markup. Each slider sets instead of a container so you can easily choose whoever you want. To style each carousel differently, we add an unordered UL tag and define my class slides. we use input type radio to handle the click function.

After creating the files just paste the following codes into your file. Make sure to save your HTML document with a .html extension, so that it can be properly viewed in a web browser.

This is the basic structure of our carousel sliders using HTML, and now we can move on to styling it using CSS.

Step 2 (CSS Code):

Once the basic HTML structure of the carousel sliders is in place, the next step is to add styling to the carousel sliders using CSS. CSS allows us to control the visual appearance of the carousel sliders, including things like layout, and typography.

Next, we will create our CSS file. We will use some basic CSS rules in this file to create our carousel sliders. We will also add some padding and margin properties to ensure that everything looks correct.

This will give our carousel sliders an upgraded presentation. Create a CSS file with the name of styles.css and paste the given codes into your CSS file. Remember that you must create a file with the .css extension.

.carousel {
  margin-left: 15%;
  margin-right: 15%;
}

ul.slides {
  display: block;
  position: relative;
  height: 600px;
  margin: 0;
  padding: 0;
  overflow: hidden;
  list-style: none;
}

.slides * {
  user-select: none;
  -ms-user-select: none;
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

ul.slides input {
  display: none; 
}


.slide-container { 
  display: block; 
}

.slide-image {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  opacity: 0;
  transition: all .7s ease-in-out;
}   

.slide-image img {
  width: auto;
  min-width: 100%;
  height: 100%;
}

.carousel-controls {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  font-size: 100px;
  line-height: 600px;
  color: #fff;
}

.carousel-controls label {
  display: none;
  position: absolute;
  padding: 0 20px;
  opacity: 0;
  transition: opacity .2s;
  cursor: pointer;
}

.slide-image:hover + .carousel-controls label{
  opacity: 0.5;
}

.carousel-controls label:hover {
  opacity: 1;
}

.carousel-controls .prev-slide {
  width: 49%;
  text-align: left;
  left: 0;
}

.carousel-controls .next-slide {
  width: 49%;
  text-align: right;
  right: 0;
}

.carousel-dots {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 20px;
  z-index: 999;
  text-align: center;
}

.carousel-dots .carousel-dot {
  display: inline-block;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #fff;
  opacity: 0.5;
  margin: 10px;
}

input:checked + .slide-container .slide-image {
  opacity: 1;
  transform: scale(1);
  transition: opacity 1s ease-in-out;
}

input:checked + .slide-container .carousel-controls label {
   display: block; 
}

input#img-1:checked ~ .carousel-dots label#img-dot-1,
input#img-2:checked ~ .carousel-dots label#img-dot-2,
input#img-3:checked ~ .carousel-dots label#img-dot-3,
input#img-4:checked ~ .carousel-dots label#img-dot-4,
input#img-5:checked ~ .carousel-dots label#img-dot-5,
input#img-6:checked ~ .carousel-dots label#img-dot-6 {
opacity: 1;
}

input:checked + .slide-container .nav label { display: block; } 

Final Output:

carousel.gif

Conclusion:

Creating a pure CSS carousel slider can be a great way to improve website performance and reduce page load times. By using HTML and CSS, you can create a functional and visually appealing slider without the need for JavaScript or jQuery. By following the steps in this tutorial, you'll be able to create a pure CSS carousel slider that is responsive and easy to use.

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