You could be considering “CSS is used for styling whereas JavaScript is used for interactivity, that’s simply the way in which the online works.”
However CSS hundreds on a webpage quicker than JavaScript. And CSS additionally causes much less reflow on a web page (which improves efficiency). So, it’s honest to say we should always use CSS for interactivity wherever potential.
1. Structure with HTML
The structure of our simple slider is easy. We’ll create a carousel-container
div to carry the carousel-slide
parts.
1 |
|
2 |
|
3 |
... |
4 |
|
That’s all we’d like so let’s transfer on to the styling of our carousel in HTML and CSS.
2. CSS Carousel behaviour
As soon as we now have our carousel slides arrange, we’ll type the CSS slider to have the next options:
- Scrollable content material
- Snap to subsequent slide
- Progress bar indicating slide progress
Scrollable content material
For the scrollable content material, we’ll use the show:flex
and overflow-x: auto
properties. We’ll additionally type slides so we will see 3 slides on the desktop display and 1 slide on cell.
1 |
.carousel-container { |
2 |
show: flex; |
3 |
overflow-x: auto; |
4 |
}
|
5 |
|
6 |
.carousel-slide { |
7 |
flex: 1 0 30%; |
8 |
}
|
9 |
|
10 |
@media (max-width: 600px) { |
11 |
.carousel-slide { |
12 |
flex: 1 0 90%; |
13 |
}
|
14 |
}
|
Snap to slip
Subsequent, to attain the snapping impact on the slides, we’ll use the CSS scroll-snap properties.
The scroll snap properties enable us outline “snapping” factors on a component. These are the factors of the aspect that we wish to be totally seen as soon as scrolling.
The up to date code of our carousel in CSS appears to be like like this:
1 |
.carousel-container { |
2 |
show: flex; |
3 |
overflow-x: auto; |
4 |
scroll-snap-type: x obligatory; |
5 |
}
|
6 |
|
7 |
.carousel-slide { |
8 |
flex: 1 0 30%; |
9 |
scroll-snap-align: heart; |
10 |
}
|
11 |
|
12 |
@media (max-width: 600px) { |
13 |
.carousel-slide { |
14 |
flex: 1 0 90%; |
15 |
}
|
16 |
}
|
Non-obligatory: CSS-only progress bar
Let’s maintain inline with our CSS-only interactivity. We are able to reap the benefits of native browser options to create a progress bar for our carousel in CSS.
We’ll do that by styling the carousel container scrollbar to present the looks of a progress bar. That is what the code appears to be like like:
1 |
.carousel-container::-webkit-scrollbar { |
2 |
top: 8px; |
3 |
}
|
4 |
|
5 |
.carousel-container::-webkit-scrollbar-thumb { |
6 |
background: #29AB87; |
7 |
}
|
8 |
|
9 |
.carousel-container::-webkit-scrollbar-track { |
10 |
background: #b1b3b399; |
11 |
}
|
12 |
|
13 |
.carousel-container::-webkit-scrollbar-track-piece:begin { |
14 |
background: #29AB87; |
15 |
}
|
Let’s take a look at the properties we’re utilizing:
-
::webkit-scrollbar
: all the scrollbar aspect -
::webkit-scrollbar-thumb
: the draggable bar on the scrollbar -
::webkit-scrollbar-track
: the trail that the scrollbar thumb is on -
::webkit-scrollbar-track-piece:begin
: the trail of the monitor not lined by the scrollbar thumb, the:begin
CSS selector targets solely the trail behind the scrollbar thumb



Within the diagram above, we will see what elements of the scrollbar are being focused. By making the -webkit-scrollbar-thumb
and ::webkit-scrollbar-track-piece:begin
the identical coloration in CSS, the scrollbar gives the look of being stuffed in because it’s being scrolled. This creates a progress bar.
Since our progress bar is definitely a scrollbar, it may possibly additionally be used to scroll via the CSS carousel. It leads to an extra function: it’s a win/win!
The ::webkit-scrollbar
properties are non-standard, fairly sketchy, and never supported by all browsers. That is why it’s not really useful to make use of this in a manufacturing atmosphere. Our progress bar will seem like a daily scrollbar on non-supported browsers. This additionally occurs should you select to not embrace these ::webkit-scrollbar
guidelines.
That’s all there may be to our simple slider! We have constructed a scrollable container with a nifty snapping function. We even added a progress bar utilizing solely CSS:
3. Extra options with JavaScript
We’ve gotten the fundamental function of the CSS carousel out of the way in which. Now we will go on so as to add much more options, utilizing JavaScript this time.
A kind of options is utilizing arrows to deal with the carousel navigation. In a earlier tutorial, we regarded into constructing a carousel with JavaScript (lower than 14 traces of code!). We are able to mix that tutorial with this one so as to add much more options to our simple slider.
That is what our mixed CSS carousel appears to be like like:
On this demo of a carousel in CSS and HTML, only for enjoyable, the code has been refactored to make use of even fewer traces of JavaScript. That is what our up to date carousel arrow perform appears to be like like:
1 |
const carousel = doc.querySelector(".carousel-container"); |
2 |
const slide = doc.querySelector(".carousel-slide"); |
3 |
|
4 |
perform handleCarouselMove(constructive = true) { |
5 |
const slideWidth = slide.clientWidth; |
6 |
carousel.scrollLeft = constructive ? carousel.scrollLeft + slideWidth : carousel.scrollLeft - slideWidth; |
7 |
}
|
Then we go that perform to our HTML arrows:
1 |
|
2 |
‹
|
3 |
|
4 |
|
5 |
|
6 |
›
|
7 |
|
And with that, we’ll name it a day on our modded-up carousel in CSS and HTML!
What’s subsequent?
You simply realized the way to create a simple carousel in HTML and CSS. There is a ton of helpful suggestions and methods but so that you can be taught. Try a few of our tutorials and get nearer to grow to be a CSS grasp!
Leave a Reply