Exceeding the set boundary with CSS for animated typing styles

I am experiencing an issue with the typing animation where it stops at a specific character count every time. If I make the statement longer, it gets cut off; if I make it shorter, it continues until reaching that set character count. Although I know I need to adjust the number of steps (currently set at 50), the actual stopping point is at 28 characters.

@import url('https://fonts.googleapis.com/css2?family=Courier+Prime&family=Source+Code+Pro:wght@200&display=swap');
html {
  min-height: 100%;
  overflow: hidden;
}

body {
  height: calc(100vh - 8em);
  padding: 0;
  margin: 0;
  color: #FFF;
  font-family: 'Courier Prime', monospace;
  background-color: rgb(0, 0, 0);
}

.video-bg {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  opacity: .5;
}

.line {
  position: relative;
  top: 75%;
  width: 24em;
  margin: auto;
  border-right: 2px solid #FFF;
  font-size: 18px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  transform: translateY(-50);
}

.anim-typewriter {
  animation: typewriter 5s steps(50) 1s 1 normal both, blinkTextCursor 750ms steps(40) infinite normal;
}

@keyframes typewriter {
  from {
    width: 0;
  }
  to {
    width: 16em;
  }
}

@keyframes blinkTextCursor {
  from {
    border-right-color: transparent;
  }
  to {
    border-right-color: rgb(155, 211, 71);
  }
}
<p class="line anim-typewriter">Under construction...</p>

Answer №1

To adjust where the keyframes will end, simply decrease the width parameter in this snippet:

@keyframes typewriter {
  from {
    width: 0;
  }
  to {
    width: 16em;
  }
}

If you change the width to 13em, the cursor will blink at the end of your text:

@import url('https://fonts.googleapis.com/css2?family=Courier+Prime&family=Source+Code+Pro:wght@200&display=swap');
html {
  min-height: 100%;
  overflow: hidden;
}

body {
  height: calc(100vh - 8em);
  padding: 0;
  margin: 0;
  color: #FFF;
  font-family: 'Courier Prime', monospace;
  background-color: rgb(0, 0, 0);
}

.video-bg {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  opacity: .5;
}

.line {
  position: relative;
  top: 75%;
  width: 24em;
  margin: auto;
  border-right: 2px solid #FFF;
  font-size: 18px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  transform: translateY(-50);
}

.anim-typewriter {
  animation: typewriter 5s steps(50) 1s 1 normal both, blinkTextCursor 750ms steps(40) infinite normal;
}

@keyframes typewriter {
  from {
    width: 0;
  }
  to {
    width: 13em;
  }
}

@keyframes blinkTextCursor {
  from {
    border-right-color: transparent;
  }
  to {
    border-right-color: rgb(155, 211, 71);
  }
}
<p class="line anim-typewriter">Under construction...</p>

This method works well, but for strings with variable lengths in the future, it's advisable to update the width based on the length of the string (number of characters).

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

JQuery Reveals AJAX Request with Malfunctioning HTML Markup

I have been working on parsing an XML file and displaying the output in a div. However, I've encountered an issue where the .append() function is not generating the expected HTML output. Below is the JQuery code snippet I am using: var container = $ ...

What does it mean in Javascript when b1 is undefined while o1 has a value and is equal to b1?

Having some issues getting variables to work with drop down options on a page. At first, I couldn't even extract a value from the function but managed to do so by removing "var" from o1. Strange thing is, when I type o1 into the js console on chrome i ...

Adjust the colors of an image without using CSS filters

Is there a way to modify the color of an image using CSS? I typically use the following code to achieve this effect: filter: hue-rotate(339deg) brightness(1); However, this method has its limitations. For instance, if the image is predominantly black, th ...

Difficulty displaying background image on iOS devices like iPhone and iPad

Our Magento website has a background image that expands according to the content, working well on PCs and Macs. However, the white background does not show up on iOS devices. I have attached two screenshots - one showing how it appears on a regular PC bro ...

Get the file in Safari web browsers

Looking for a solution to download media files (mp3, mp4) supported by the Safari browser? I attempted using the HTML5 download attribute but found it only works on Chrome and IE. Any suggestions for an HTML or JavaScript code that would work instead? ...

Customized HTML Template Tag

While I understand that using custom html tags is generally frowned upon for various reasons, I have a specific scenario that I believe might warrant the use of a custom html tag. I hope to either be proven wrong or discover a better way of achieving my ob ...

align video element to the right within a container div

I'm attempting to align a video element to the bottom right inside a div. Here is the code I have so far, which successfully aligns the video element to the bottom but not to the right side of the div container: video { } .border.rounded-0.border- ...

How about a group-input containing a element added in the center?

Looking to insert a prepend element between two input elements without relying on extra CSS tricks. Is it possible to achieve this using just Bootstrap and without any additional CSS? <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bo ...

Reduce the size of text using HTML and CSS

I'm currently working on replicating an image using HTML and CSS that will be placed beneath the avatar on a forum. Here is what I have managed to achieve so far: https://jsfiddle.net/J7VBV/220/ table.avatarStats { border-spacing: 0; border- ...

CSS based on Media Query width is not being applied after content has been rewritten by an AJAX call

Check out this website (created using Zurb Foundation Framework) where, on screens wider than 425px, there is a row of books displayed on a background image resembling a bookshelf within a specific div element. However, when viewing the page on a phone o ...

Ways to determine if the user is using a mobile device with CSS

Is there a way to detect mobile users using CSS? I am working on a project that requires detecting mobile users, but I am unsure how to do so. Can you provide any guidance? ...

css for floating labels in input and textarea using bootstrap

I have implemented the Bootstrap Float-label feature found at https://github.com/tonystar/float-label-css Note: I made some custom changes to suit my needs. Issues : 1) While clicking on an input, the float label functions correctly. However, the bootst ...

What could be causing my HTML table to stretch all the way to the bottom and right?

https://i.sstatic.net/SpG52.png The layout appears to be extending to the right and bottom, despite my attempts to fix it. I am utilizing the Bootstrap table class in my code. Here is the code snippet: HTML: <head> <link rel="stylesheet" ...

Is there a way to send the image object to the onclick function as it is being assigned?

I apologize if my question is a bit unclear, as I am currently teaching myself how to use javascript. I am working on generating image thumbnails dynamically and would like the ability for users to enlarge the image when they click on the thumbnails. The p ...

Deleting class from several pages

Can jquery be used to remove a class from elements on multiple pages within the same file? <html> <script src="scripts/jquery.min.js"></script> <script> $(document).ready(function(){ $("div.mydiv").click(fun ...

Checking for valid usernames using Regex in JavaScript

I have attempted to use the following regex to validate against the criteria listed below, but it seems to fail in certain cases. Can you help me identify what might be wrong with my approach? Regular Expression: /[a-z]|\d|\_{4, 16}$/.test(user ...

When using Javascript to include a selection in an HTML drop-down menu, no changes occur

No matter how many code examples I copy from various sources, my attempts to add options to an HTML drop down list have been unsuccessful. Here is the script that I am working with: <!DOCTYPE html> <html><head></head> <body> & ...

The bottom section of the main page

I've successfully implemented a footer that sticks to the bottom of each page as intended. However, I'm facing an issue when there's a large amount of text on a particular page that requires scrolling. The scroll continues beyond the footer, ...

Hovering over the entire page triggers an interactive effect on every element

So I've implemented a hover effect for my links, but strangely the whole page is being affected. Here is my current style: a:hover, :visited, :active, :hover { color:#fff; text-shadow: -1px 1px 8px #ffc, 1px -1px 8px #fff; text-decoration: none; col ...

Setting fixed width for table headers in Bootstrap 3

Can someone explain why the width of "th" is not fixed and new line breaks occur when the content is too big? Currently, the "th" width expands. How can this be fixed? The width of my first column is col-lg-2. Click here for an example image ...