What is the method in CSS for animating elements to shift to the right one letter at a time?

I am currently experimenting with moving letters individually from a word to the right, one by one. The effect I am going for is as if they are being pulled in that direction from the center. You can check out this cool pen (not mine, btw): https://codepen.io/egrucza/pen/LZdPeP.

What I want to achieve is similar to that, where the word is already present in HTML and when hovered over, the individual letters move to the right. Here's what I have accomplished so far in my pen: https://codepen.io/jenny0515/pen/wvpdKBz. Below is a snippet of code (please click on my code pen link):

.content1 :hover {
  text-align: right;
  content: "Hair Clips";
}
.content2 :hover {
  text-align: right;
  content: "Make-up";
}
.content3 :hover {
  text-align: right;
  content: "Jewelry";
}

My goal is to make it such that when hovering, instead of the letters appearing from above or below, the word remains at the center of the row (as shown in my code pen). Upon hover, the last letter of the word should move to the right first, followed by the other letters with a slight delay between each movement.

How can I achieve this effect using CSS?

Answer №1

To create animations for individual elements, it is necessary to break them down separately.

<div class="center">
 <span class="one">C</span>
 <span class="two">o</span>
 <span class="three">d</span>
 <span class="four">e</span>

In the provided CodePen demonstration, the letters have been separated into spans for manual animation. There are alternative methods available to streamline your CSS and manage delays, but this approach is a quick and effective solution.

Explore the animation example on CodePen

Answer №2

Understanding the basics is key before diving into more complex concepts. Here's a simple example that highlights fundamental ideas:

In this example:

  • Letters are represented using single span elements (an important point mentioned by others).
  • The styling for letter:hover is clearly defined.
  • The usage of &:nth-of-type(4) moves the 4th letter using translateX.

Additionally, there are two other crucial points to be aware of:

  • The transition property in the provided example is a shorthand approach (view details here).
  • To move each letter individually when hovering over a div would require the use of @keyframes.

Note: The current setup only enables moving the fourth letter on hover.

HTML

<div class="box">
    <span class="letter">H</span>
    <span class="letter">e</span>
    <span class="letter">r</span>
    <span class="letter">e</span>
</div>

CSS

$duration: 5s;

span {
    display: inline-block;
}

.letter:hover {
    &:nth-of-type(4) {
        transform: translateX(200px);
        transition-duration: $duration;
    }
}

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

Copy and paste content from Outlook, Word, or any Office software directly into the embedded browser

Our application is performing well, but there is an issue with some users copying text to Word before pasting it into the app. The HTML gets parsed out somewhat correctly, but it often includes tags from outlook or word that our XHTML engine doesn't r ...

Placing circular buttons below the navigation bar

I'm currently working on implementing a navigation bar on my master page. Here is the code snippet for the navigation bar: <nav class="navbar" style="background-color: #264653; position:absolute; top:0px; left:50%; transform:tran ...

Never forget to login to your HTML Cookies Frame!

Looking to create a website with the following features: Two frames The first frame containing an edit box and a button The second frame displaying a secure page requiring a username and password Is it possible for a specific user to always b ...

Move object smoothly off screen without any delay

I have been experimenting with Animate.CSS and basic Jquery to add animations to elements coming on and off the screen. However, a problem I have encountered is that there is noticeable lag, especially when there is a background slideshow running simultane ...

Having trouble determining why the design is not displaying correctly

I'm currently working on a calendar webpage, and everything looks perfect until I add the new JavaScript element. Suddenly, the numbers in the first row start behaving strangely. I've tried to troubleshoot but can't seem to figure out what&a ...

Using Vue JS to display information conditionally within a single component

I currently have a component that dynamically displays the data from an array of objects. However, I want the component to display only one object based on a specific condition. Is it possible to show either one object or another depending on the value o ...

Best methods for deleting an element's attribute or style attribute

Imagine this snippet of CSS code: .notif-icon { display: inline-block; } In my HTML, I have the following element which starts off hidden by overriding the display property of the notif-icon class: <span id="error" class="notif-icon& ...

Using JQuery with the latest version of Google Sites is a game-ch

My coding knowledge is very limited, especially beyond VBA. I've hit a roadblock and it seems like I'm overlooking something obvious. What I'm attempting to accomplish: I need this code to be integrated into a New Google Sites page (which h ...

Creating a Full-Width Form Using Bootstrap

I need help aligning 3 form boxes side by side with a total width of 100%. I want to be able to specify the width of each box, or have the last two boxes set to their default size while the first box fills up the remaining space. Here is my current code: ...

Text that is curving around a D3.js pie chart

I am currently working on creating a 3D-Js chart and I would like the pie text to wrap around the pie itself. This is the exact effect that I am trying to achieve: https://i.sstatic.net/YOLdo.png I am facing two main issues: I am currently printi ...

Prevent DIV from being filled by background color

Currently, I am working with a PNG image that has a transparent area resembling a torn checkout receipt with a zig-zag edge. This image is intended to function as the bottom part of a div that has a white background to mimic a till receipt. In my attempt t ...

Learn how to use onclick event in an anchor tag to send post values through Ajax!

Can I send post values via AJAX using an onclick event on an anchor tag? I've tried the following code but I'm not receiving any post values. How can I make it work? ............................................................................. ...

Ways to automatically refresh a page in Javascript after a short period of user inactivity

Similar Question: How Can I Modify This Code To Redirect Only When There Is No Mouse Movement I am looking to update a web page automatically if the user is inactive, specifically through key presses or mouse clicks using Javascript. ...

Leveraging HTML classnames for metadata purposes

Is it recommended to use descriptive class names in HTML that fully describe its content, such as navbar-static-8 for a fixed navbar with 8 items? Alternatively, should the metadata be separated into individual attributes like type="static" items="8"? I ...

Swap out the hyperlink text for a dropdown menu when clicked and then revert back

Is there a way to dynamically switch between a label/text and a Kendo Combobox in a div using JavaScript when clicking on the text? The desired functionality includes: Clicking on the text displays the combobox, clicking away from it hides the combobox a ...

Setting the default attribute to all tags in JavaScript using HTTP

I am currently learning HTML and trying to understand how things work... One thing I am struggling with is setting the target attribute for all links on my webpage so they open in a new tab... I have been experimenting with jQuery functions, but so far, ...

Embed the bootstrap menu within a customized div

I have a unique div containing a combination of CSS styles and a Bootstrap menu. My goal is to integrate the menu within the same div using CSS to achieve the following structure: ABOUT US - HISTORY QUALITY To accomplish this, I intend to insert the tex ...

Prevent anchor tags from halting click propagation

After some experimentation, I discovered that a tags can interrupt event propagation in the DOM. For example, clicking on the red button should log text, but if you click where they overlap, only the link behavior occurs. Is there a way to ensure the event ...

Ways to center-align this menu (Spanish language)

I am working on optimizing the alignment of the menu on my website: My goal is to center the menu, as the English version looks good. #site-navigation { width: 980px; margin: 0 auto; text-align: left; } After analyzing the code, we have iden ...

"Exciting Changes in Color According to the Active State of vue-route-link

I am trying to find a way to customize the CSS based on whether a link is exact or active. Essentially, when a user clicks on a menu item, I want the underline to change depending on whether the link is an active router-link. Although I was able to accompl ...