What are some ways to stop animated scrolling text from unexpectedly resetting in HTML and CSS?

I created this code snippet on CodePen to illustrate my point: https://codepen.io/sakana-boy/pen/wvBedWo

.scroll-text {
    overflow: hidden;
    position: relative;
}

.scroll-text * {
    white-space: pre;
    transform: translateX(100%);
    animation: scroll 5s steps(70, end) infinite;
}

@keyframes scroll {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
<div class="scroll-text" style="width: 355px;">
  <div>hi i am very long text lolololololoololo this is very long sdafkalshgjalwke more text wow ok TOO LONG!!! you Will not See THIS text because it gets cut off : ( very sad </div>
</div>
<div class="scroll-text" style="width: 355px;">
  <div>this text is shorter so all the text properly is shown</div>
</div>

In essence, I have text that I want to dynamically populate and scroll within a narrower parent div than the text itself. The issue I am facing, as you can see in the CodePen above, is that the scrolling abruptly restarts mid-animation. While I could adjust the value in the 100% keyframe for translateX, I cannot predetermine this value due to the dynamic nature of the text. Furthermore, setting a high value may result in longer text appearing blank in the textbox for an extended period.

Is there a way to dynamically set the keyframe values at 0% and 100% so that the entire text is displayed throughout the animation? The desired behavior is to scroll through all the content without restarting prematurely. I aim to achieve this without increasing the width of the parent divs to prevent text overflow into other elements on my webpage.

If achieving this solely with HTML and CSS is not feasible, I am willing to explore JavaScript solutions, preferably using vanilla JS to avoid incorporating jQuery.

Answer №1

When styling, remember to include position: relative and transform:

  • transform: translateX(100%) will stretch to 100% of the content (text);
  • position: relative; left: 100% will expand to 100% of the container;

To ensure the inner div fits the content's width, use display: table.

.scroll-text {
    overflow: hidden;
    position: relative;
}

.scroll-text * {
    display: table;
    white-space: pre;
    position: relative;
    animation: scroll 5s linear infinite;
}

@keyframes scroll {
  0% { left: 100%; transform: translateX(0); }
  100% { left: 0; transform: translateX(-100%); }
}
<div class="scroll-text" style="width: 355px;">
  <div>hi i am very long text lolololololoololo this is very long sdafkalshgjalwke more text wow ok TOO LONG!!! you Will not See THIS text because it gets cut off : ( very sad </div>
</div>
<div class="scroll-text" style="width: 355px;">
  <div>this text is shorter so all the text properly is shown</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The reason why the script and div tags are so common in HTML is because they serve different purposes. However, it's

Hey there, friend! I've searched on baidu.com, cnds, and stack overflow, but couldn't find an answer. I have two questions: "why can script and div tags be used so much in HTML?" and "why is there only one html and body tag in HTML?" For example: ...

What steps should I follow to enable a tooltip in this specific situation using qtip?

In my database, I have tables for venues, venue types, and map icons with the following relationships: A venue belongs to a venue type A venue type belongs to a map icon Each venue result is shown on the index page as a partial. Each partial ...

Utilizing a foreach loop to control the behavior of two distinct radio buttons as a unified

Can someone help me with a code containing a 5 star rating system (radio button) for two different questions? The issue is that the ratings for each question are linked, so when I make a selection in one question, it clears the selection in the other. It& ...

Retrieving object by a value within a nested array in Javascript

I need to retrieve all objects that have a specific 'id' within a nested array. Specifically, I want to find all person objects with hobbies id of 2 (hiking) in the provided sample data. This inquiry tackles the challenge of extracting all value ...

What is the best way to divide data using [/ -]?

I am trying to split a string that shows a date. The date is shown in the format dd/mm/yyyy or dd-mm-yyyy. This is my code (the current string is formatted as mm/dd/yyyy): <input type="data" id="dat"> ..... var dataString=$('#dat').val(); ...

Utilizing JavaScript files within Angular2 components: A guide

I need to insert a widget that runs on load. Typically, in a regular HTML page, I would include the script: <script src="rectangleDrawing.js"></script> Then, I would add a div as a placeholder: <div name="rectangle></div> The is ...

Convert a JSON Object using AngularJs

Is there a method in Angular to restructure this JSON Object? I am looking to convert the JSON object from its original format: $scope.TestJson = { "filters": [ { "dataPropertyID": "VoidType", ...

VueJS form validation does not account for empty inputs in both fields

One of the challenges I'm facing is generating a form with Vue.js using the input fields below: { name: 'first_name', type: 'text', label: 'First Name', placeholder: 'First Name', ...

Trigger an event, pause, and subsequently trigger another one within Vue

I have successfully emitted the following events to the parent component: this.$emit('sendToParent1', true); this.$emit('sendToParent2'); this.$emit('sendToParent3'); this.$emit('sendToParent4', true); this.$emit(&ap ...

Exploring the options: choosing an element from a dropdown menu using a div tag

Struggling to choose an option from a drop-down that contains a div tag instead of the usual select tag. While my code successfully opens the corresponding div, it fails to select the desired element. Here is the snippet of HTML tags: <div id="selecta ...

Issues with adjusting the height using CSS transformations are not being resolved

There seems to be an issue with animating the height property of an element as it is not animating at all. Check out the fiddle where the animation is attempted. Here is the HTML: <ul> <li> li 1 </li> <li> ...

Issue with function incorrectly computing values and returning NaN

My challenge is to develop a countdown timer, but it keeps returning NaN instead of counting down. I have multiple counters in place - one that counts down, another that counts up until the stop time specified by stoptime, and a third that kicks in after s ...

When combining <a> with the class line-through, the line does not appear

I am currently utilizing a class to replace the deprecated strike in HTML: .entfall { text-decoration: line-through; } However, when using Firefox 38.x and the following code: <span class="entfall"><a href="some link">text</a></span ...

Guide to placing a button on the same line as text with the use of JavaScript

Does anyone know how to add a button to the right of text that was added using JS DOM? I've tried multiple techniques but can't seem to get it right - the button always ends up on the next line. Any tips on how to achieve this? var text = docu ...

Styling dates in React

How can I properly display the date in the correct format? I'm seeing the date displayed incorrectly on the screen as 32/03/2020, but the 32nd day doesn't exist. Below is the correct date format displayed in the image. I'm new to Reactjs, ...

What is the best way to rearrange the elements in a database after they have been displayed?

I have a feature on my website where you can enter text into a textbox and then click a button to save it in a database. The saved entries are displayed along with buttons for deleting an entry and moving it to the top of the list. <?php $resultset2 = ...

There seems to be an error with JVectorMap: the <g> attribute transform is expecting a number but is instead receiving "scale(NaN) translate(N..."

Hey there! I'm currently working on creating a form with a map to select the country of birth using JVectorMap. However, when checking the Google Chrome developer console, I encountered the following error message: jquery-jvectormap-2.0.5.min.js:1 Err ...

Using CSS backdrop-filter to create a unique blurred border effect for your web elements

In my quest for achieving unique visual effects, I have been experimenting with the backdrop-filter and element borders. While it is easy to create an element that blurs its background, I am facing challenges in making the filter affect only the border are ...

Transform HTML into PDF while maintaining the original letter spacing

We are in need of converting numerous files to enable search and word highlighting functionality in a web browser, as well as server-side indexing for the searchable words. I have previously utilized an online service like (Step 1, Step 2 on linked page) ...

What causes the text-align property to function in varying ways across different tags?

Can you explain why the text-align property behaves differently on different tags? In two cases, I am trying to center my text. In the first case, I use a parent div tag with an anchor tag inside. When I need to center align, I apply the property to the p ...