loading...
Cover image for How to Build A Slider

How to Build A Slider

jeannienguyen profile image Jeannie Nguyen ・1 min read

Here's how to make a simple slider - they're easy to build and they look great on your page.

Here's the markup for your slider:

<div class="slider">
    <div class="slide"></div>
    <div class="slide"></div>
    <div class="slide"></div>
    <div class="slide"></div>
    <div class="slide"></div>
</div>

Now add some CSS:

.slider {
    max-width: 300px;
    height: 300px;
    display: flex;
    overflow-x: auto;
}
.slide {
    max-width: 300px;
    flex-shrink: 0;
}

Let's add smooth scroll and snap each slide into place:

.slider {
    ...
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x mandatory;
}
.slide {
    ...
    scroll-snap-align: center;
}

That's it! You made a slider. Along with some images and other content you want to add, it'll look something like this:

Alt Text

Posted on by:

jeannienguyen profile

Jeannie Nguyen

@jeannienguyen

Writing about everything I learn on the web and sharing it so you can learn with me.

Discussion

markdown guide