2 Ways to Create HTML & CSS News Ticker (Horizontal and Vertical)

INTRODUCTION

NEWS TICKER WITH PURE CSS

Welcome to a tutorial on how to create a news ticker with just pure HTML and CSS. Need to add a block of scrolling news text? Yep, it is actually possible with today’s technology, using only pure CSS. There will not be a single line of Javascript involved in this tutorial – Read on to find out!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything.

 

 

 

PREAMBLE

DOWNLOAD & NOTES

First, here is the download link to the example source code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code in a zip file – I have released it under the MIT License, so feel free to build on top of it if you want to.

 

QUICK NOTES

  • There is nothing to install, so just download and unzip into a folder.
  • ticker.html is a horizontal ticker, while ticker-vert.html is a vertical ticker.

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

SECTION A

HORIZONTAL NEWS TICKER

We shall start with a common horizontal news ticker, credits to Hipster Ipsum for the original code I found at Codepen.

 

THE HTML

1a-ticker.html
<div class="tcontainer"><div class="ticker-wrap"><div class="ticker-move">
  <div class="ticker-item">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
  <div class="ticker-item">Aliquam consequat varius consequat.</div>
  <div class="ticker-item">Fusce dapibus turpis vel nisi malesuada sollicitudin.</div>
  <div class="ticker-item">Pellentesque auctor molestie orci ut blandit.</div>
</div></div></div>

Yep… That is a lot of <div>, 3 layers to be exact. Will go through that later, but if you want to add more items, just sandwich more <div class="ticker-item"> within.

 

 

THE CSS

1b-ticker.css
/* OUTER CONTAINER */
.tcontainer {
  width: 100%;
  overflow: hidden; /* Hide scroll bar */
}
 
/* MIDDLE CONTAINER */
.ticker-wrap {
  width: 100%;
  padding-left: 100%; /* Push contents to right side of screen */
  background-color: #eee;
}

/* INNER CONTAINER */
@keyframes ticker {
  0% { transform: translate3d(0, 0, 0); }
  100% { transform: translate3d(-100%, 0, 0); }
}
.ticker-move {
  /* Basically move items from right side of screen to left in infinite loop */
  display: inline-block;
  white-space: nowrap;
  padding-right: 100%;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  animation-name: ticker;
  animation-duration: 10s;
}
.ticker-move:hover{
  animation-play-state: paused; /* Pause scroll on mouse hover */
}

/* ITEMS */
.ticker-item{
  display: inline-block; /* Lay items in a horizontal line */
  padding: 0 2rem;
}

If you are interested in how this ticker works in detail, here is how from the outermost element to innermost.

  • .tcontainer Just a 100% width container with overflow: hidden to hide the scrollbars.
  • .ticker-wrap Another 100% width container, and has padding-left: 100% to push all the internal item offscreen to the right.
  • .ticker-move This layer does the actual ticker magic with CSS animation.
  • @keyframes ticker Basically, an animation sequence that moves .ticker-move from right to left.
  • .ticker-item Individual items of the ticker. Displayed in inline-block, so all the text lines up in a single row.

In a nutshell, all the text items are pushed off the screen using padding-left: 100%, then moved back using a CSS animated translate3d(-100%, 0, 0). In an infinite animation loop, that is.

 

THE DEMO

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aliquam consequat varius consequat.
Fusce dapibus turpis vel nisi malesuada sollicitudin.
Pellentesque auctor molestie orci ut blandit.

 

THE LIMITATIONS & ADDING ITEMS

  • As you can see, the animation-duration is fixed in CSS.
  • Meaning, if more text is added, the ticker speed will become faster. You have to manually readjust the duration yourself based on the amount of text.
  • This will also mean having a different ticker speed on various screen sizes.

 

 

RECOMMENDED SOLUTION

Yep, this is kind of dumb, but limit the amount of text that you put in, so that the speed is more-or-less controlled. You can also use the media query to control the ticker speed on different screen sizes.

@media only screen and (max-width: 768px) {
  .ticker-move { animation-duration: 20s; }
}

 

SECTION B

VERTICAL NEWS TICKER

We shall now move into a vertical news ticker. Credits to Rishabh for the original code at CSSDeck.

 

THE HTML

2a-ticker.html
<div class="tickerv-wrap"><ul>
  <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
  <li>Aliquam consequat varius consequat.</li>
  <li>Fusce dapibus turpis vel nisi malesuada sollicitudin.</li>
  <li>Pellentesque auctor molestie orci ut blandit.</li>
</ul></div>

The HTML for the vertical ticker is a lot easier, just wrap a list inside a <div>. Captain Obvious, add more <li> if you need more items.

 

 

THE CSS

2b-ticker.css
/* WRAPPER */
.tickerv-wrap {
  background: #eee;
  box-sizing: content-box;
  height: 25px; /* Take note of this */
  overflow: hidden; /* Hide scrollbars */
  padding: 10px;
} 

/* TICKER ANIMATION */
@keyframes tickerv {
  0%   {margin-top: 0;}
  /* Quite literally -ve height of wrapper */
  25%  {margin-top: -26px;} /* 1 X 25 px */
  50%  {margin-top: -52px;} /* 2 X 25 px */
  75%  {margin-top: -78px;} /* 3 X 25 px */
  100% {margin-top: 0;} /* Back to first line */
}
.tickerv-wrap ul {
  padding: 0;
  margin: 0;
  list-style-type: none;
  animation-name: tickerv; /* Loop through items */
  animation-duration: 10s;
  animation-iteration-count: infinite;
  animation-timing-function: cubic-bezier(1, 0, .5, 0);
}
.tickerv-wrap ul:hover {
  /* Pause on mouse hover */
  animation-play-state: paused;
}

/* ITEMS */
.tickerv-wrap ul li {
  font-size: 18px;
  line-height: 26px /* Same as wrapper height */
}

The vertical ticker is a little easier to understand than the horizontal one:

  • Basically, we give the list items a fixed height .tickerv-wrap ul li { line-height: 26px }.
  • Then, we put this list into a container that will only display one line .tickerv-wrap { height: 25px }.
  • Finally, as you may have already guessed, CSS animation magic @keyframes tickerv will scroll through all the items.

 

 

THE DEMO

  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • Aliquam consequat varius consequat.
  • Fusce dapibus turpis vel nisi malesuada sollicitudin.
  • Pellentesque auctor molestie orci ut blandit.

 

LIMITATIONS & ADDING ITEMS

Just like the above horizontal ticker, the keyframes in the CSS animation is fixed, meaning:

  • If you want to add more or remove lines, you have to readjust the entire @keyframes tickerv manually.
  • Also, remember to adjust the animation-duration as well.

But apart from that, not much pain to using this vertical version.

 

 

EXTRA

USEFUL BITS & LINKS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

Changed your mind? Want to use libraries in your project? Here are a few news tickers that are based on libraries.

 

EXTRA

VIDEO TUTORIAL

For you guys who want more, here’s the video tutorial, and shameless self-promotion – Subscribe to the Code Boxx YouTube channel for more!

 

YOUTUBE TUTORIAL

 

INFOGRAPHIC CHEAT SHEET

Pure CSS News Ticker (click to enlarge)

 

CLOSING

WHAT NEXT?

Thank you for reading, and we have come to the end of this short tutorial. I hope it has helped you create better websites, and if you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!

14 thoughts on “2 Ways to Create HTML & CSS News Ticker (Horizontal and Vertical)”

  1. TL;DR – Sir, you’re a credit to your profession…sublime & succinct, I’ve been searching a solution on this level – and it WORKS – no questions asked. Thank you again…

  2. All of this looks very PROMISING,
    but THIS PAGE isn’t delivering on its offer to DOWNLOAD the “.zip” files!!
    The links are non-existent or mal-functioning !!!

    1. The zip file is on the server and link is WORKING!? Sometimes, I don’t understand people… You could have just COPY-AND-PASTED the code in just 1 minute. Thanks for reporting anyway.

  3. The end of the text never scrolls past…. it just instantly disappears… and then shows the beginning again.
    Pointless.

    1. Then just change the pointless keyframes to scroll past the pointless -100% mark 100% { transform: translate3d(-120%, 0, 0); }.
      Or maybe pointlessly freeze the last frame for a while with 75% { transform: translate3d(-80%, 0, 0); } 100% { transform: translate3d(-80%, 0, 0); }.

      Tutorials do really become pointless when people pointlessly miss out on the important points. They don’t bother to read nor find out how CSS animations work, then pointlessly cite a pointless word such as pointless. 😆

  4. Super! I’m not a coder but managed to get it to work on a Weebly site home page through the embed code widget. It solved a feature request. Thanks. Now my next mountain is to figure out how to make it dynamic with php or something so I don’t have to manually change the html every week.

  5. If have more line items to go through what do I change so it can get through them all? Only gets through about 4 and resets.

Leave a Comment

Your email address will not be published. Required fields are marked *