Scrolling Text in HTML without Marquee Tag

The marquee tag was used (in older versions of HTML) to create an automatic scrolling effect for text or images. But HTML5 deprecated <marquee> tag as its function can be better handled by CSS animations. So, how can we create scrolling text in HTML without the marquee tag? The answer is simple, using CSS animations.

The marquee animation is useful to scroll the latest news/posts or updates on the webpage. It can also be used for advertisement purposes or where you wanted to integrated this animation. As you have seen in the above preview, a bar containing the boxes for text just like a news ticker. You can check it on the demo page to see the marquee animation.

The basic requirement of this little project is to move text from one side to another continuously. For this purpose, the CSS translate transformation property provides us an easy way to create a scrolling effect just like the marquee tag does. So, let’s get started with HTML to create a scrolling text effect.

HTML Structure for Scrolling Text

In HTML, create a div element with a class name "Marquee" and place your text wrapped with another div tag and define its class name "Marquee-tag". The basic HTML structure for the scrolling text is as follows:

<div class="marquee-container">
<div class="Marquee">
  <div class="Marquee-content">
    <div class="Marquee-tag">1</div>
    <div class="Marquee-tag">2</div>
    <div class="Marquee-tag">3</div>
    <div class="Marquee-tag">1</div>
    <div class="Marquee-tag">2</div>
    <div class="Marquee-tag">3</div>
    <div class="Marquee-tag">1</div>
    <div class="Marquee-tag">2</div>
    <div class="Marquee-tag">3</div>
    <div class="Marquee-tag">1</div>
    <div class="Marquee-tag">2</div>
    <div class="Marquee-tag">3</div>
  </div>
</div>
</div>

You can also place images inside the "Marquee-tag" div element. Likewise, you can add any HTML element that you want to scroll along with the text.

The CSS Styles

In the above HTML structure, we wrapped all marquee content into a container having a class “marquee-container”. So, style this container as follows:

.marquee-container{
    max-width: 860px;
    margin: 15px auto;
    overflow: hidden;
}

Now, target the "Marquee" class and set its linear-gradient background. Define its width, padding, and margin property. Similarly, define the border-box value for the box-sizing property and align the text to the center. Here the important thing is display property that you need to display flexbox. The font-size, color, and font-weight can be defined according to your needs.

.Marquee {
  background: -webkit-linear-gradient(225deg, #008ed9, #8b00db);
  background: -moz-linear-gradient(225deg, #008ed9, #8b00db);
  background: -o-linear-gradient(225deg, #008ed9, #8b00db);
  background: -ms-linear-gradient(225deg, #008ed9, #8b00db);
  background: linear-gradient(-135deg, #008ed9, #8b00db);
  width: 100vw;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 1em;
  color: #fff;
  font-weight: 200;
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: box;
  display: flex;
  -webkit-box-align: center;
  -moz-box-align: center;
  -o-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  overflow: hidden;
    margin: 15px auto;
}

After that, define the CSS styles for the “Marquee-content” class that is the wrapper element of scrolling text/images. Display it as a box element and define marquee animation using CSS linear infinite running property. We’ll define the keyframes for this (marquee) animation in the last step.

.Marquee-content {
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: box;
  display: flex;
  -webkit-animation: marquee 10s linear infinite running;
  -moz-animation: marquee 10s linear infinite running;
  -o-animation: marquee 10s linear infinite running;
  -ms-animation: marquee 10s linear infinite running;
  animation: marquee 10s linear infinite running;
}

In order to pause the scrolling text on the hover event, use the animation-play-state with hover pseudo-selector.

.Marquee-content:hover {
  -webkit-animation-play-state: paused;
  -moz-animation-play-state: paused;
  -o-animation-play-state: paused;
  -ms-animation-play-state: paused;
  animation-play-state: paused;
}

The "Marquee-tag" class contains the text or whatever you added in this div element. Set it width, margin, padding, and background property as described below:

.Marquee-tag {
  width: 200px;
  margin: 0 0.5em;
  padding: 0.5em;
  background: rgba(255,255,255,0.1);
  display: -webkit-inline-box;
  display: -moz-inline-box;
  display: -webkit-inline-flex;
  display: -ms-inline-flexbox;
  display: inline-box;
  display: inline-flex;
  -webkit-box-align: center;
  -moz-box-align: center;
  -o-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  -webkit-box-pack: center;
  -moz-box-pack: center;
  -o-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-transition: all 0.2s ease;
  -moz-transition: all 0.2s ease;
  -o-transition: all 0.2s ease;
  -ms-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

You can also specify styles for marquee items on the hover event. Like, you can set a custom background color or transform it as follows:

.Marquee-tag:hover {
  background: rgba(255,255,255,0.5);
  -webkit-transform: scale(1.1);
  -moz-transform: scale(1.1);
  -o-transform: scale(1.1);
  -ms-transform: scale(1.1);
  transform: scale(1.1);
  cursor: pointer;
}

Finally, add the following CSS3 animation keyframes for scrolling text that move the "Marquee-content" element from one side to another.

@-moz-keyframes marquee {
  0% {
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -o-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
  }
  100% {
    -webkit-transform: translate(-50%);
    -moz-transform: translate(-50%);
    -o-transform: translate(-50%);
    -ms-transform: translate(-50%);
    transform: translate(-50%);
  }
}
@-webkit-keyframes marquee {
  0% {
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -o-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
  }
  100% {
    -webkit-transform: translate(-50%);
    -moz-transform: translate(-50%);
    -o-transform: translate(-50%);
    -ms-transform: translate(-50%);
    transform: translate(-50%);
  }
}
@-o-keyframes marquee {
  0% {
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -o-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
  }
  100% {
    -webkit-transform: translate(-50%);
    -moz-transform: translate(-50%);
    -o-transform: translate(-50%);
    -ms-transform: translate(-50%);
    transform: translate(-50%);
  }
}
@keyframes marquee {
  0% {
    -webkit-transform: translateX(0);
    -moz-transform: translateX(0);
    -o-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
  }
  100% {
    -webkit-transform: translate(-50%);
    -moz-transform: translate(-50%);
    -o-transform: translate(-50%);
    -ms-transform: translate(-50%);
    transform: translate(-50%);
  }
}

That’s all! I hope you have successfully implemented this scrolling text animation. If you have any questions or suggestions, let me know by comment below.

You Might Be Interested In: