CSS Flexbox Image Carousel

Category: CSS & CSS3 , Slider | December 16, 2017
Author: Genio Dronof
Views Total: 2,362
Official Page: Go to website
Last Update: December 16, 2017
License: MIT

Preview:

CSS Flexbox Image Carousel

Description:

A smooth, elegant, responsive background image slider/carousel built using CSS flexbox and html radio inputs.

How to use it:

The basic HTML structure for the carousel.

<div class="mySlider">
  <input type="radio" name="slider" class="slide-radio1" checked id="slider_1">
  <input type="radio" name="slider" class="slide-radio2" id="slider_2">
  <div class="slider-pagination">
    <label for="slider_1" class="page1"></label>
    <label for="slider_2" class="page2"></label>
  </div>
  <div class="next control">
    <label for="slider_1" class="numb1"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
      <path d="M298.3 256L131.1 81.9c-4.2-4.3-4.1-11.4.2-15.8l29.9-30.6c4.3-4.4 11.3-4.5 15.5-.2L380.9 248c2.2 2.2 3.2 5.2 3 8.1.1 3-.9 5.9-3 8.1L176.7 476.8c-4.2 4.3-11.2 4.2-15.5-.2L131.3 446c-4.3-4.4-4.4-11.5-.2-15.8L298.3 256z"/>
      </svg> </label>
    <label for="slider_2" class="numb2"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
      <path d="M298.3 256L131.1 81.9c-4.2-4.3-4.1-11.4.2-15.8l29.9-30.6c4.3-4.4 11.3-4.5 15.5-.2L380.9 248c2.2 2.2 3.2 5.2 3 8.1.1 3-.9 5.9-3 8.1L176.7 476.8c-4.2 4.3-11.2 4.2-15.5-.2L131.3 446c-4.3-4.4-4.4-11.5-.2-15.8L298.3 256z"/>
      </svg> </label>
  </div>
  <div class="previous control">
    <label for="slider_1" class="numb1"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
      <path d="M213.7 256L380.9 81.9c4.2-4.3 4.1-11.4-.2-15.8l-29.9-30.6c-4.3-4.4-11.3-4.5-15.5-.2L131.1 247.9c-2.2 2.2-3.2 5.2-3 8.1-.1 3 .9 5.9 3 8.1l204.2 212.7c4.2 4.3 11.2 4.2 15.5-.2l29.9-30.6c4.3-4.4 4.4-11.5.2-15.8L213.7 256z"/>
      </svg> </label>
    <label for="slider_2" class="numb2"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
      <path d="M213.7 256L380.9 81.9c4.2-4.3 4.1-11.4-.2-15.8l-29.9-30.6c-4.3-4.4-11.3-4.5-15.5-.2L131.1 247.9c-2.2 2.2-3.2 5.2-3 8.1-.1 3 .9 5.9 3 8.1l204.2 212.7c4.2 4.3 11.2 4.2 15.5-.2l29.9-30.6c4.3-4.4 4.4-11.5.2-15.8L213.7 256z"/>
      </svg> </label>
  </div>
  <div class="slider slide_img_01"></div>
  <div class="slider slide_img_02"></div>
</div>

The basic CSS styles for the carousel.

.mySlider {
  position : relative;
  display : block;
  overflow : hidden;
  width : 100%;
  max-width : 100vw;
  height : 100vh;
  max-height : 100%;
  margin : 0 auto;
}

.slider {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  align-content: center;
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 1;
  z-index: 0;
  transition: transform 1600ms;
  transform: scale(1);
}

Add background images to the slides.

.slide_img_01 {
  background : url('1.jpg') no-repeat center;
  background-size: cover;
  left: 0;
}

.slide_img_02 {
  background : url('2.jpg') no-repeat center;
  background-size : cover;
  left: 100%
}

The CSS for the navigation arrows.

.control {
  position: absolute;
  top: 50%;
  margin-top: -24px;
  z-index: 55;
}

.control label svg {
  width : 48px;
  height : 48px;
  fill : #fff;
  stroke : currentColor;
  stroke-width : 0;
}

.control label svg:hover {
  transition : all .2s ease;
  fill : #000;
}

.control label {
  z-index: 0;
  display: none;
  cursor: pointer;
  opacity: 0.5;
}

.control label:hover { opacity: 1.0; }

.next { right: 1%; }

.previous { left: 1%; }

The CSS for the bottom pagination bullets.

.slider-pagination {
  position: absolute;
  bottom: 20px;
  width: 100%;
  left: 0;
  text-align: center;
  z-index: 1000;
}

.slider-pagination label {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  background: rgba(255,255,255,0.2);
  margin: 0 2px;
  border: solid 1px rgba(255,255,255,0.4);
  cursor: pointer;
}

.slide-radio1:checked ~ .next .numb2, .slide-radio2:checked ~ .next .numb1, .slide-radio2:checked ~ .previous .numb1, .slide-radio1:checked ~ .previous .numb2 {
  display: block;
  z-index: 1
}

.slide-radio1:checked ~ .slider-pagination .page1, .slide-radio2:checked ~ .slider-pagination .page2 { background: rgba(255,255,255,1) }

Apply the slide animation to the carousel using CSS3 transforms.

.slide-radio1:checked ~ .slider { transform: translateX(0%); }

.slide-radio2:checked ~ .slider { transform: translateX(-100%); }

Make the carousel fully responsive.

@media only screen and (max-width: 767px) {

.mySlider {
  position : relative;
  width : 100%;
  height : 400px;
}

.slider > div { padding: 0 2% }

.control {
  position: absolute;
  top: 60%;
  margin-top: -24px;
  z-index: 55;
}

.control label svg {
  width : 32px;
  height : 32px;
  fill : #fff;
  stroke : currentColor;
  stroke-width : 0;
}
}

You Might Be Interested In:


Leave a Reply