DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Step-by-Step Guide to Creating a Calculator App With HTML and JS (With Factor Calculator Example)
  • The Best Programming Languages for Kids
  • How To Develop a Modern Image Gallery With HTML, CSS, and JavaScript

Trending

  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  1. DZone
  2. Coding
  3. JavaScript
  4. Creating Scrolling Text With HTML, CSS, and JavaScript

Creating Scrolling Text With HTML, CSS, and JavaScript

Learn five methods to create scrolling text with HTML, CSS, and JavaScript, including plain HTML, CSS animations, and more, for flexible web design solutions.

By 
John Negoita user avatar
John Negoita
·
Jan. 21, 25 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.1K Views

Join the DZone community and get the full member experience.

Join For Free

When you’ve been building web applications for over 25 years, as I have done, using HTML, CSS, and JavaScript has become second nature. 

In this article, I’ll show you some simple ways to create scrolling text using these tools, including five different methods for coding scrolling text with plain HTML, HTML and CSS, and finally, HTML + CSS + JS.

1. Plain HTML Scrolling Text


Simply add a tag around the text to create a scrolling effect. This tag offers a few interesting parameters, among which:

  • direction — can be left, right, up, or down
  • scrollamount — the speed of the scroll text
  • loop — how many times should the scrolling repeat

NOTE: This is a deprecated HTML tag, so I’d recommend restraint when using it for modern web projects.

2. Animated Scrolling Text With CSS


Define a CSS animation called scroll that animates the translateX property.

CSS
 
@keyframes scroll {
 0% { transform: translateX(100%); }
 100% { transform: translateX(-100%); }
}


Then apply that animation to your text element:
HTML
 
<div class="scrolling-text">Scrolling Text</div>


By adding this CSS:
Plain Text
 
.scrolling-text {
width: 100%;
overflow: hidden;
white-space: nowrap;
box-sizing: border-box;
animation: scroll 10s linear infinite;
}


Changing from translateX to translateY will give you a vertical scrolling text. Switching 0% and 100% around will give you the reverse scrolling text direction. And in the .scrolling-text CSS class, if you change the 10s duration of the animation, you change the speed of the scrolling text.

3. HTML + CSS + JavaScript Scrolling Text


HTML code:

HTML
 
<div class="scrolling-text">
Scrolling Text Scrolling Text Scrolling Text
</div>

CSS code:

CSS
 
.scrolling-text {
width: 30vw;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
font-size:clamp(16px,50dvh,220px);
white-space:nowrap;
margin: 0 auto;
}


JavaScript code:
JavaScript
 
const container = document.querySelector('.scrolling-text');
let scrollAmount = 0;

setInterval(() => {
 scrollAmount += 10;
 container.scrollLeft = scrollAmount;
 if (scrollAmount >= container.scrollWidth) {
  scrollAmount = 0;
 }
}, 20);


This is my most “unelegant” solution for making scrolling text. Basically, it’s cramming text in a container with a scroll and changing the scrolling of the container programmatically. When the scroll amount goes over the container amount, it resets.

4. Scroll Text With jQuery

CSS
 
$(document).ready(
 function loop() {
  $('.scrolling-text').css({scrollLeft:0});
  $('.scrolling-text').animate({ scrollLeft: "+=1000" }, 10000, 'linear', loop);
 }
);


Use the animate() jQuery function to animate the scrollLeft property, and this will create a scrolling text effect.

In my view, jQuery is a bit overkill in this situation, and it only makes sense if you have already used jQuery in your project.

Of course, animate() can also be used for animating the translateX or translateY properties as seen above.

5. Scrolling Text With Canvas HTML5


This is my favorite method. Especially because it’s so flexible and offers so many possibilities, like, for example, exporting the scrolling text as GIF or even video. You can see this in action by going to the Scrolling Text generator on PSDDude, where you can create your own customized scrolling text images and videos.

The HTML code is straightforward:

HTML
 
<canvas id="scrollingCanvas" width="300" height="50"></canvas>

And the JS is where the magic happens:
JavaScript
 
const canvas = document.getElementById('scrollingCanvas');
const ctx = canvas.getContext('2d');
const text = "Scrolling Text Example";
let x = canvas.width;

function draw() {
 ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
 ctx.font = '20px Arial';
 ctx.fillStyle = 'black';
 ctx.fillText(text, x, 30);
 x -= 2; // Adjust speed of scrolling here
 if (x < -ctx.measureText(text).width) {
  x = canvas.width; // Reset position when text is out of view
 }
 requestAnimationFrame(draw);
}

draw();


Using a loop with requestAnimationFrame() calling the function draw() is actually the way HTML5 games implement their graphics drawing. This is a cool way to create smooth-scrolling text.

You get the size on screen of the text using the measureText() context method. This allows creating a seamless scrolling text by resetting the text position when it reaches the end position.

Bonus: A Few Scrolling Text Ideas

LED Scrolling Text GIF

Learn more about it in this link.

LED Scrolling Text


Star Wars Opening Crawl Scrolling Text Generator

Learn more about it in this link.


Stock Market Scrolling Text

Learn more here.
Stock Market Scrolling Text


Weather Scrolling Text

Learn more about it here.


These were created with the scrolling text gif and video generator on PSDDude.

Conclusion

Now you know how to make scrolling text using HTML, CSS, and/or JavaScript.

What will you use this for? Drop me a comment to let me know. Also, if you feel I’ve missed an important method for creating scrolling text, give me your thoughts on that.

CSS HTML JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Step-by-Step Guide to Creating a Calculator App With HTML and JS (With Factor Calculator Example)
  • The Best Programming Languages for Kids
  • How To Develop a Modern Image Gallery With HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!