INTRODUCTION
NO FRILLS SLIDES
Welcome to a tutorial on how to create a simple pure CSS text slider. Sick of those complicated slides that require extra libraries? Why not make your own? Pure CSS text sliders are actually pretty simple, and this guide will walk you through how to create one – Read on!
I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
CONFESSION
AN HONEST DISCLOSURE
Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!
NAVIGATION
TABLE OF CONTENTS
Section A | Section B | Extra |
Closing |
SECTION A
HORIZONTAL TEXT SLIDER
Let us start with a horizontal text slider (that scrolls right to left).
STEP 1) THE HTML
Horizontal Text Slider Example
Horizontal Text Slider
Yep, there is no rocket science here:
- is the “main container” for the text slider.is the “secondary container” in which we will do some CSS animation magic with.
- Finally,
are the individual slides.STEP 2) THE CSS
1-horizontal.css/* [SLIDER] */ #slider, #slider .slide{ width: 500px; height: 250px; } #slider { overflow: hidden; margin: 0 auto; font-size: 1.2em; background: #ffd7d1; } #slider .container { position: relative; width: 9000px; /* Assign an insanely large width */ top: 0; right: 0; animation: slide-animation 25s infinite; } #slider .slide { position: relative; float: left; box-sizing: border-box; padding: 10px 20px; } /* [ANIMATION] */ @keyframes slide-animation { 0% { opacity: 0; right: 0; } 11% { opacity: 1; right: 0; } 22% { right: 100%; } 33% { right: 100%; } 44% { right: 200%; } 55% { right: 200%; } 66% { right: 300%; } 77% { right: 300%; } 88% { opacity: 1; right: 400%; } 100% { opacity: 0; right: 400%; } } /* [DOES NOT MATTER] */ html, body { font-family: arial; background: #ffe9d8; } h1 { text-align: center; }
- First, we line up all the
.slide
usingfloat: left
. The goal is to create a “super long” unbroken horizontal string of slides. - For that to happen, we have to set the
.container
to have an insanely large width. - Next, we set
overflow: hidden
on the#slider
main container to prevent an ugly scrollbar. - The width and height of every
.slide
is then set to match the main#slider
container… Or the CSS animation magic will not work properly. - Finally, we attach a
keyframes
animation on.container
– The idea is simple, playing with the right position to “scroll” through the slides.
SECTION B
VERTICAL TEXT SLIDERWith the horizontal text slider, we can simply do a small tweak to the CSS and turn it into a vertical text slider.
STEP 1) THE HTML
2-vertical.htmlVertical Text Slider Example Vertical Text Slider
Yep, this is pretty much the same… Just some changes in the CSS.
STEP 2) THE CSS
2-vertical.css/* [SLIDER] */ #slider, #slider .slide{ width: 500px; height: 250px; } #slider { overflow: hidden; margin: 0 auto; font-size: 1.2em; background: #ffd7d1; } #slider .container { position: relative; bottom: 0; right: 0; animation: slide-animation 20s infinite; } #slider .slide { position: relative; box-sizing: border-box; padding: 10px 20px; } /* [ANIMATION] */ @keyframes slide-animation { 0% { opacity: 0; bottom: 0; } 11% { opacity: 1; bottom: 0; } 22% { bottom: 100%; } 33% { bottom: 100%; } 44% { bottom: 200%; } 55% { bottom: 200%; } 66% { bottom: 300%; } 77% { bottom: 300%; } 88% { opacity: 1; bottom: 400%; } 100% { opacity: 0; bottom: 400%; } } /* [DOES NOT MATTER] */ html, body { font-family: arial; background: #ffe9d8; } h1 { text-align: center; }
The working principals behind the vertical slider are the same as the horizontal one.
- All the
.slide
are lined up vertically, which is already done “naturally” with the defaultdisplay:block
. - Similarly, we set
overflow: hidden
on the#slider
main container to hide the ugly scrollbar. - The width and height of every
.slide
must match the main#slider
container as usual. - Finally, attach the
keyframes
animation on.container
– Instead playing with the right position, we will play with the bottom position to scroll vertically.
EXTRA
DOWNLOAD & DEMOThat’s it for all the code, and here is the download link as promised – Plus a demo of both the horizontal and vertical slides.
DEMO
LIMITATIONS
As you can already see, maintaining this slider can be quite a hassle – If you add or remove a slide, you will have to update the keyframes accordingly. Also, even though this simple text scroller does not have a single line of Javascript, it is also not responsive. You can try messing around with
min-width
andmax-width
, but the chances are, you will still need some sort of “CSS hack” to get it to work.DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
CLOSING
WHAT’S NEXT?Thank you for reading, and we have come to the end of this short guide. I hope that it has helped you to create a better website, and if you have anything to add to this guide, please feel free to comment below. Good luck and happy coding!
The post 2 Steps Simple Pure CSS Text Slider appeared first on Code Boxx.