Scroll Text Effect Using HTML

Kumail.pk is a Free Platform of

Scroll Text Effect Using HTML

Scrolling text is text that moves onto a website page, by following the direction you define for it. It is dynamically displayed and has the properties you have set for this.

Types of such text:

  • Classic (the text is scrolling onto the page in an infinite number of iterations)

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.

Scroll Text Effect Using HTML

Scroll Text Effect Using HTML

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:

  1. <div class=“marquee-container”>
  2. <div class=“Marquee”>
  3. <div class=“Marquee-content”>
  4. <div class=“Marquee-tag”>1</div>
  5. <div class=“Marquee-tag”>2</div>
  6. <div class=“Marquee-tag”>3</div>
  7. <div class=“Marquee-tag”>1</div>
  8. <div class=“Marquee-tag”>2</div>
  9. <div class=“Marquee-tag”>3</div>
  10. <div class=“Marquee-tag”>1</div>
  11. <div class=“Marquee-tag”>2</div>
  12. <div class=“Marquee-tag”>3</div>
  13. <div class=“Marquee-tag”>1</div>
  14. <div class=“Marquee-tag”>2</div>
  15. <div class=“Marquee-tag”>3</div>
  16. </div>
  17. </div>
  18. </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:

  1. .marqueecontainer{
  2. maxwidth: 860px;
  3. margin: 15px auto;
  4. overflow: hidden;
  5. }

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.

  1. .Marquee {
  2. background: webkitlineargradient(225deg, #008ed9, #8b00db);
  3. background: mozlineargradient(225deg, #008ed9, #8b00db);
  4. background: olineargradient(225deg, #008ed9, #8b00db);
  5. background: mslineargradient(225deg, #008ed9, #8b00db);
  6. background: lineargradient(-135deg, #008ed9, #8b00db);
  7. width: 100vw;
  8. webkitboxsizing: borderbox;
  9. mozboxsizing: borderbox;
  10. boxsizing: borderbox;
  11. padding: 1em;
  12. color: #fff;
  13. fontweight: 200;
  14. display: webkitbox;
  15. display: mozbox;
  16. display: webkitflex;
  17. display: msflexbox;
  18. display: box;
  19. display: flex;
  20. webkitboxalign: center;
  21. mozboxalign: center;
  22. oboxalign: center;
  23. msflexalign: center;
  24. webkitalignitems: center;
  25. alignitems: center;
  26. overflow: hidden;
  27. margin: 15px auto;
  28. }

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.

  1. .Marqueecontent {
  2. display: webkitbox;
  3. display: mozbox;
  4. display: webkitflex;
  5. display: msflexbox;
  6. display: box;
  7. display: flex;
  8. webkitanimation: marquee 10s linear infinite running;
  9. mozanimation: marquee 10s linear infinite running;
  10. oanimation: marquee 10s linear infinite running;
  11. msanimation: marquee 10s linear infinite running;
  12. animation: marquee 10s linear infinite running;
  13. }

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

  1. .Marqueecontent:hover {
  2. webkitanimationplaystate: paused;
  3. mozanimationplaystate: paused;
  4. oanimationplaystate: paused;
  5. msanimationplaystate: paused;
  6. animationplaystate: paused;
  7. }

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:

  1. .Marqueetag {
  2. width: 200px;
  3. margin: 0 0.5em;
  4. padding: 0.5em;
  5. background: rgba(255,255,255,0.1);
  6. display: webkitinlinebox;
  7. display: mozinlinebox;
  8. display: webkitinlineflex;
  9. display: msinlineflexbox;
  10. display: inlinebox;
  11. display: inlineflex;
  12. webkitboxalign: center;
  13. mozboxalign: center;
  14. oboxalign: center;
  15. msflexalign: center;
  16. webkitalignitems: center;
  17. alignitems: center;
  18. webkitboxpack: center;
  19. mozboxpack: center;
  20. oboxpack: center;
  21. msflexpack: center;
  22. webkitjustifycontent: center;
  23. justifycontent: center;
  24. webkittransition: all 0.2s ease;
  25. moztransition: all 0.2s ease;
  26. otransition: all 0.2s ease;
  27. mstransition: all 0.2s ease;
  28. transition: all 0.2s ease;
  29. }

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:

  1. .Marqueetag:hover {
  2. background: rgba(255,255,255,0.5);
  3. webkittransform: scale(1.1);
  4. moztransform: scale(1.1);
  5. otransform: scale(1.1);
  6. mstransform: scale(1.1);
  7. transform: scale(1.1);
  8. cursor: pointer;
  9. }

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

  1. @-mozkeyframes marquee {
  2. 0% {
  3. webkittransform: translateX(0);
  4. moztransform: translateX(0);
  5. otransform: translateX(0);
  6. mstransform: translateX(0);
  7. transform: translateX(0);
  8. }
  9. 100% {
  10. webkittransform: translate(-50%);
  11. moztransform: translate(-50%);
  12. otransform: translate(-50%);
  13. mstransform: translate(-50%);
  14. transform: translate(-50%);
  15. }
  16. }
  17. @-webkitkeyframes marquee {
  18. 0% {
  19. webkittransform: translateX(0);
  20. moztransform: translateX(0);
  21. otransform: translateX(0);
  22. mstransform: translateX(0);
  23. transform: translateX(0);
  24. }
  25. 100% {
  26. webkittransform: translate(-50%);
  27. moztransform: translate(-50%);
  28. otransform: translate(-50%);
  29. mstransform: translate(-50%);
  30. transform: translate(-50%);
  31. }
  32. }
  33. @-okeyframes marquee {
  34. 0% {
  35. webkittransform: translateX(0);
  36. moztransform: translateX(0);
  37. otransform: translateX(0);
  38. mstransform: translateX(0);
  39. transform: translateX(0);
  40. }
  41. 100% {
  42. webkittransform: translate(-50%);
  43. moztransform: translate(-50%);
  44. otransform: translate(-50%);
  45. mstransform: translate(-50%);
  46. transform: translate(-50%);
  47. }
  48. }
  49. @keyframes marquee {
  50. 0% {
  51. webkittransform: translateX(0);
  52. moztransform: translateX(0);
  53. otransform: translateX(0);
  54. mstransform: translateX(0);
  55. transform: translateX(0);
  56. }
  57. 100% {
  58. webkittransform: translate(-50%);
  59. moztransform: translate(-50%);
  60. otransform: translate(-50%);
  61. mstransform: translate(-50%);
  62. transform: translate(-50%);
  63. }
  64. }

About Us

Kumail.pk is a Free Platform of Education initiated by Syed Kumail Hassan Shah (Director GIT Education)

E-Certification