Simple Responsive Text Slider In HTML CSS (Horizontal & Vertical)

Welcome to a tutorial on how to create a simple responsive text slider in HTML CSS. So you want to add a text slider to your page? There’s no need to use third-party frameworks, here’s how to create one using pure HTML CSS – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Click here to download | Example on CodePen

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS…

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a “paid scripts” business, and I don’t “block people with Adblock”. Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

HTML CSS TEXT SLIDERS

All right, let us start with a horizontal text slider (that scrolls right to left).

 

TUTORIAL VIDEO

 

1) HORIZONTAL TEXT SLIDER

1A) HORIZONTAL SLIDER HTML 

1a-horizontal.html

The episode orbits? The panic overwhelms a fuse.

The lasting astronomer balances the counter reminder.

Her birthday calculates past a juice!

Last slide.

The idea here is to sandwich the slides between 2

wrappers.

 

1B) HORIZONTAL SLIDER CSS

1b-horizontal.css

/* (A) FORCE ALL SLIDES INTO SINGLE ROW */
.hmove { display: flex; }
.hslide { width: 100%; flex-shrink: 0; }
.hwrap { overflow: hidden; }

/* (B) SHIFT SLIDES WITH CSS ANIMATION */
/* (B1) SLIDES POSITION */
.hmove { position: relative; top: 0; right: 0; }
@keyframes slideh {
  0% { right: 0; } 24% { right: 0; }
  25% { right: 100%; } 49% { right: 100%; }
  50% { right: 200%; } 74% { right: 200%; }
  75% { right: 300%; } 99% { right: 300%; }
  100% { right: 0; }
}
 
/* (B2) MOVE SLIDES */
.hmove { animation: slideh 12s infinite; }
.hmove:hover { animation-play-state: paused; }

This looks like quite a handful, but keep calm and study slowly.

  1. The first thing we do is to lay all the slides in a long horizontal row.
    • Set the inner .hmove wrapper to display: flex.
    • .hslide { width: 100%; flex-shrink: 0; } will arrange all slides into a single row.
    • Use overflow: hidden to hide the ugly scrollbar on the outer .hwrap wrapper.
  2. Next, deal with the slide rotation. Basically, .hmove { position: relative; right: 0% } will show the first slide, right: 100% for the second, right: 200% for the third, and so on…
    • (B1) To “automate” the slide rotation, create a sequence of @keyframes with respective right: N%.
    • (B2) Attach the keyframes to .hmove, CSS animation will do the rest of the magic.
    • (B2) Pause the slide rotation on mouse hover.

 

 

1C) HORIZONTAL SLIDER DEMO

The episode orbits? The panic overwhelms a fuse.

The lasting astronomer balances the counter reminder.

Her birthday calculates past a juice!

Last slide.

 

1D) ADDING & REMOVING SLIDES