CSS only expanding slider π²

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Super happy to read this on a Saturday morning! Thank you β€οΈ
I'm sure that at the time of writing this blog post, you would have never thought of writing more than 700+ posts continuously. You truly are an inspiration to many.
I did not actually!
Such a great blessing to being able to look back at where things started and how they are going π
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Sliders are amazing, a welcome addition to every website, but sometimes can become very complicated with text flying in and images animating from all sides.
Let's say we just want a simple expandable slider, we can achieve this with just css you heard me right, CSS only!
For our html we are going to use the following setup:
<div class="container">
<div class="slider">
<div class="slide">
<img src="https://images.unsplash.com/photo-1533468432434-200de3b5d528?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=975&q=80" />
</div>
<div class="slide">
<img src="https://images.unsplash.com/photo-1586254574632-55e4aaea7793?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80" />
</div>
<div class="slide">
<img src="https://images.unsplash.com/photo-1536536982624-06c1776e0ca8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80" />
</div>
<div class="slide">
<img src="https://images.unsplash.com/photo-1502827186494-9f7976a04548?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=976&q=80" />
</div>
<div class="slide">
<img src="https://images.unsplash.com/photo-1545559054-8f4f9e855781?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80" />
</div>
</div>
</div>
Nothing fancy a container (optional) and a slider div with 5 slide divs inside, each slide contains a image in this case.
Now for the CSS we use the following:
.container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
.slider {
width: 80vw;
height: 80vh;
display: flex;
}
.slide {
position: relative;
flex: 1 0 auto;
overflow: hidden;
transition: all 0.5s ease-in-out;
}
.slide:hover {
flex: 5;
}
.slide img {
position: absolute;
max-height: 100%;
left: 50%;
transform: translateX(-50%);
}
.container the container is a wrapper because I didn't want the slider to be full with.
We give the container display: flex and align everything horizontal and vertical as we learned here.
.slider The slider just has 80vw (viewport width) and 80vh (viewport height) and display flex, you can read more about the viewport elements in this article.
.slide is where the fun begins, we tell it to be relative to center the image, later on, then we add a flex: 1 0 auto; which will distribute them equally. then we set overflow: hidden; to not show the extra image part. And we add some smooth animation with: transition: all 0.5s ease-in-out;.
Once you hover on the slide we set the flex to 5 which will make that element bigger and because our transition is in place it will look smooth.
Last we position the image inside, we make them absolute positioned to center them better.
We choose to align them 50% from the left and then return them back -50% with transform. This will make sure the absolute positioning is entered.
This looks like the following demo:
See the Pen CSS only expanding slider π² by Chris Bongers (@rebelchris) on CodePen.
In the previous example, we used images, but we can also use background images, that will look something like this:
<div class="container">
<div class="slider">
<div class="slide slide1"></div>
<div class="slide slide2"></div>
<div class="slide slide3"></div>
<div class="slide slide4"></div>
<div class="slide slide5"></div>
</div>
</div>
Then for the CSS we can do the following:
.slider {
width: 100vw;
height: 100vh;
display: flex;
}
.slide {
position: relative;
flex: 1 0 auto;
transition: all 0.5s ease-in-out;
}
.slide:hover {
flex: 5;
}
.slide1 {
background-image: url('https://images.unsplash.com/photo-1533468432434-200de3b5d528?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=975&q=80');
background-position: center center;
background-size: cover;
}
//...repeat for all your slides
As you can see this is actually easier, but some people prefer to have the actual image element in place.
With this you can make this:
See the Pen Dragon Ball Super Slider CSS Only by Chris Bongers (@rebelchris) on CodePen.
Flex has really wide support and polyfills available, it also depends on the rest of your structure which ones you are going to need.

Thank you for reading my blog, feel free to subscribe to my email newsletter and connect on Facebook or Twitter