How to Build a Responsive Slider With Swiper.js

* { box-sizing: border-box; } body {margin: 0;}*{box-sizing:border-box;}body{margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;}

Have you ever built an advanced slider for a project? If so, you might have taken advantage of any one of a number of JavaScript carousels. In the past, I’ve covered two of them: slick and Owl. Today, I’ll introduce another well-known one: Swiper.

Ready to learn something new?

Our Slider Project

Check what we’ll be building:

Be sure to open the demo on a large screen and resize your window to see how the page layout changes.

What is Swiper Slider?

Swiper is a free JavaScript plugin created by Vladimir Kharlampidi that lets you create modern, responsive sliders.

The Swiper logo

With almost 30k GitHub stars at the time of this writing, it’s considered one of the most dominant JavaScript plugins out there. To better understand its capabilities, check out the available demos.

Swiper isn’t only available for use with pure JavaScript. If you plan to build an app with one of the popular JavaScript frameworks (e.g. React) and need a slider, consider looking at the associated built-in Swiper component.

Getting Started With Swiper

To get started with Swiper, begin by downloading and installing the following files in your project:

  • swiper-bundle.css or its minified version
  • swiper-bundle.js or its minified version

You can grab a copy of these Swiper files by visiting the GitHub repo, using a package manager (e.g. npm), or loading the necessary assets through a CDN (e.g. cdnjs). For this tutorial, I’ll choose the last option.

For this tutorial, beyond the Swiper files, I’ve also incorporated Bootstrap 5’s CSS file.

With that in mind, if you look under the Settings tab of our demo pen, you’ll see that there are two external CSS files and one external JavaScript file.

The required CSS assetsThe required CSS assetsThe required CSS assets
The required JS assetThe required JS assetThe required JS asset

1. Identify the Layout

To get started, let’s first identify the project scope.

Today’s demo is a web page dedicated to Tanzania, a country with immense beauty. To set up the page, we’ll grab some content from Wikipedia and images from Unsplash.

Let’s determine how the page layout will appear on various screens.

The Mobile Layout

On small screens (<768px), its appearance will be like this:

The mobile layoutThe mobile layoutThe mobile layout

Notice that each slider shows the first slide and half of the second one.

The Tablet Layout

Next, on medium screens (768px), its appearance will change as follows:

The tablet layoutThe tablet layoutThe tablet layout

Notice that the first slider shows the first two slides and half of the third one, while the second slider still shows two slides.

The Desktop Layout

Finally, on large screens (1200px), it will have this appearance:

The desktop layoutThe desktop layoutThe desktop layout

Again, consider that the first slider shows the first three slides and half of the fourth one, while the second slider shows the first two slides and half of the third one.

Also, pay attention to another thing: the right side of the first slider is aligned to the container width that, as we’ll see, will be 1100px. In the same way, the left slide of the second slider is aligned to the container width.

The red area indicates the empty space that will be increased as the viewport becomes bigger and bigger.

2. Define the HTML Markup

The markup for our page will consist of four sections:

  • The first and third sections will contain info about Tanzania taken from Wikipedia.
  • The second and fourth sections will include two equal carousels that will display Tanzania through Unsplash photos. These sections will only have a different class that will determine their layout. That said, the second section will have the section-with-right-offset class, while the fourth section the section-with-left-offset one. 

Here’s the page structure:

3. Specify the Main Styles

Let’s now concentrate on the most important styles of our page.

Here are the remarkable things:

  • The container will have a maximum width of 1100px.
  • The initial dimensions of the Unsplash images won’t be equal. With that in mind, the carousel images will have a fixed height that will vary across the screen sizes. Here we’ll use the object-fit: cover property value for fitting the images within the container. Alternatively, we could have added the images as background ones.
  • By default, all image captions will be hidden apart from the caption of the active slide. As the active slide changes, the associated caption will appear with small slide animation.

Here are the associated styles:

Customize Dot Navigation

The initial appearance of the dot navigation is this one:

The default dots navigation

Why not make it more informative and attractive? To achieve this, we’ll add some styles and update its default markup during the plugin initialization.

Here’s the updated navigation:

The dots navigation after adding styles

The required styles:

Be sure to click on a dot to see the little slide animation that happens.

4. Add the JavaScript

At this point, we’re ready to turn our attention to JavaScript.

Offset Carousel Sections

As you can see from the previous visualizations, the carousel sections will be full-screen on screens up to 1199px wide. On larger screens, their left or right side will stop being full-screen and be aligned to the container width. As discussed earlier, this behavior will be determined by the section-with-left-offset and section-with-right-offset classes. This way, we’ll be able to generate unique layouts that won’t be limited to sections that follow a grid system.

The offsetThe offsetThe offset

Here’s the JavaScript code that implements this functionality:

Initialize Swiper

This is the last required step to initialize Swiper. Here, we pass as part of the configuration object all our customizations. Look at the relevant code snippet below:

As you can see, we pass in an array the number of slides that should appear depending on the viewport width. By using, not only integers but also decimals, we’ll be able to show just a portion of a slide.

Even though it isn’t necessary, the breakpoint values that determine when the number of visible slides changes will match Bootstrap’s breakpoints. Anyhow, be sure to read the API documentation to get a better understanding of what all these configuration parameters do. 

Conclusion

And we’re done, folks! In this tutorial, we created an asymmetric page layout with just a few lines of JavaScript code and the power of Swiper.js.

We only covered just the very basics of this plugin. There are so many more advanced sliders that you can build with minimal effort. Just get some inspiration from a source like Dribbble and practice yourselves! This is the best way to learn!

Let’s look again at what we built:

As always, thanks a lot for reading!

More Slider, Lightbox, and Carousel Tutorials