Create an animation that transitions a line from 0% width to 100% width, moving from left to right

Desiring to create a pseudo-element line that expands from left to right across its container, then contracts from right to left back to zero width. Note: in my JavaScript code, I apply the "underlined-animated" class after a specific timeout.

I've experimented with various keyframes in an attempt to achieve the desired effect.

em::after {
    content: "";
    height: 1px;
    width: 0;
    display: inline-block;
    position: absolute;
    left: 0;
    background: green;
    transition: all;
}

.underlined-animated::after {
    animation: underline-animated 5s forwards;
}

@keyframes underline-animated {
    0%   {width: 0;}
    50%  {width: 100%; left: initial; right:0;}
    100% {width: 0;}
}

The intended outcome is for the line to expand smoothly from left to right until it reaches 100% width of the container, and then contract from right to left until it returns to 0% width – all within a single animation sequence.

However, the current implementation results in a peculiarly growing and shrinking line instead.

Answer №1

Try animating the right property up to 50% and then use the left property from 50% to the end instead of animating the width:

em::after {
  content: "";
  height: 1px;
  display: inline-block;
  position: absolute;
  left: 0;
  background: green;
  transition: all;
}

.underlined-animated::after {
  animation: underline-animated 5s forwards;
}

@keyframes underline-animated {
  0% {
    right: 100%;
  }
  50% {
    right: 0;
    left: 0;
  }
  100% {
    right: 0;
    left: 100%;
  }
}
<em class="underlined-animated">I'm the text</em>

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

Utilizing mat dialog in conjunction with the bootstrap sticky top class to enhance functionality

I am encountering an issue where, upon clicking to delete an entry, a mat dialog should appear with everything else in the background greyed out. However, the problem I am facing is that when I attempt to delete an entry, the dialog appears but the sticky ...

Adjust the width to ensure the height is within the bounds of the window screen?

I am currently in the process of developing a responsive website, and my goal is to have the homepage display without any need for scrolling. The layout consists of a 239px tall header, a footer that is 94px tall, and an Owl Carousel that slides through im ...

Center a single div vertically within a parent div by utilizing a fluid layout

Attempting to align text elements within a responsive layout presents challenges due to the fluid nature of <div> sizes. I recently discovered a fantastic method for horizontally and vertically centering a child <div> inside its parent <div ...

Adjust font size proportions in CSS3

I am currently experimenting with the new font-face feature in CSS3 to use custom TTF/OTF fonts. My question is: can I adjust the default font size ratio? For example, if I am using Tahoma with a default 9pt font size and want to switch to my own custom ...

Looking for assistance on how to automatically close the sidebar or pull navigation menu after clicking on a menu item

My website is designed to be responsive and includes a navigation slider that is hidden off-screen by default. The slider features a vertical push/pull bar that remains visible for users to access the menu. The entire navigation slider is fixed on the page ...

Transforming bootstrap 4 rows into cards or a card deck

Having some difficulty configuring Bootstrap cards to display correctly. The original template had one item per row in columns, but I want to transform it into polaroid-style cards while still using Bootstrap 4 for a clean look. Original template (excerpt ...

What is preventing me from using CSS to customize elements within InfoWindows?

I've been attempting to adjust the font-size of elements within my InfoWindows, but I'm facing some challenges. Here's a snippet of the code for my InfoWindow: <InfoWindow marker={this.state.activeMarker} visible={thi ...

Tips for aligning a div in the center while adjusting screen size

My goal is to showcase a dashboard with a card layout. The desired layout can be seen https://i.sstatic.net/OWgpE.png: Here is a snippet from the HTML File: <div id="overallCanvasWrapper" class="statistic-container"> <p ng-show=" ...

How to apply an icon image using the background property in CSS in Vue 3

Need help setting background image from the public folder |_public | |_images | |_icon.png |_src |_components |_TheHeader.vue I'm having difficulty trying to set an image as a background in TheHeader.vue using CSS, but the image is located in ...

Is there a way to trigger a Bootstrap modal to open upon clicking on an input?

Currently using Bootstrap, I have a search form and a modal in my project. My goal is to have the modal open when clicking on the search form, rather than having it triggered by a button as most online resources suggest. Is there a way to achieve this func ...

Issue with footer display while utilizing Bootstrap's cover template

When I utilize the Bootstrap cover template, I encounter an issue where if I resize the window to a smaller level or include enough text to require scrolling, the footer background color turns into a solid color. This is also visible in the template I am u ...

The Bootstrap Alert triggers the appearance of unnecessary whitespace

I'm in the process of setting up a basic support center using HTML. Everything was going smoothly until I incorporated a bootstrap alert. Placing the alert inside col-lg-4 as shown in the code snippet from bootsnipp caused an unwanted whitespace to ap ...

What is the most effective way to create a live preview using AngularJS and CSS?

Is there a way to dynamically change the background color of the body based on the hexadecimal value entered in a textfield, without using jQuery? I want the change to happen live as the user types. The current code works but it doesn't feel right. I ...

The width of the HTML select dropdown needs to be adjusted as it appears too large

I've been trying to find a solution to my issue, but I'm having trouble locating one. It could be because English is not my first language and maybe I'm not searching using the right terms. The problem I'm facing is with implementing t ...

The dropdown menu in the navigation bar is not properly lined up with its corresponding menu item

I've been struggling to align my submenu with the navbar, and I suspect there is an issue with the CSS that I can't seem to figure out. CodePEN: https://codepen.io/wowasdi/pen/xxKzdQq Even after trying to adjust the navbar CSS settings by changi ...

Another inquiry about bootstrap: Adjusting vertical height only if content does not overflow

My current setup is as follows: <div class="container px-0"> <nav class="..."> </nav> <div class="row px-3"> ...content </div> <div class="row footer-spacer&q ...

Ensure that only one button from the collection is activated

One of the features on my website includes 3 blocks with buttons. These buttons trigger a change in the displayed picture when clicked. However, it is crucial to ensure that when a new track is activated, the previous button returns to its original state. ...

Modifying a variable when a specific number of elements are clicked using jQuery

I have created a script for my portfolio that is functional but I want to streamline it so that I only need to edit the HTML, not the script itself. The main requirements for the code are: Count the number of <img> tags within the element with id ...

How come my "Pop-up box" doesn't appear when clicked a second time?

Although the code above works perfectly when I run it, it causes errors on my website. The issue I'm facing is that on the first click, all links work fine. However, when I try to click a second time, it shows a blank page and I can't click anywh ...

Personalizing the User Interface of Azure AD B2C using dynamic HTML

After customizing my own html and CSS for Azure AD B2C sign-up and sign-in policy using the steps outlined in this resource, I have successfully created custom Sign-Up and Sign-In pages. However, my next goal is to make the sign-up page dynamic by passing ...