# How to Build a Responsive Slider With Swiper.js

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.

## 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.

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

• 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.

## 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:

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:

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:

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:

The initial appearance of the dot navigation is this one:

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.

The required styles:

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

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

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.

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!

### Swiper.js Video Tutorial

Now you’ve learned the fundamentals of Swiper, why not try your hand at a couple of other Swiper.js carousels? Follow Adi over on the Tuts+ YouTube channel (and don’t forget to subscribe for more videos!)