Learn the technique of smoothly transitioning and swapping words using CSS animations

I'm attempting to create a CSS animation that involves fading out one word, replacing it with another word, and then fading in the new word. I'm having trouble getting it to work correctly. Here is my code:

HTML

<span class="words"></span>

CSS

.words:before {
    content: "one";
    animation-name: replacement;
    animation-duration: 2s;
    animation-timing-function: ease-out;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}

@keyframes replacement {
    0% {
        opacity: 0;
    }

    100% {
        opacity: 1;
        content: "two";
    }
}

https://jsfiddle.net/fp2h6q4L/

Answer №1

Give this CSS a shot

.text:before {
      opacity:0;
      content: "first";
      animation-name: changeText;
      animation-duration: 4s;
      animation-timing-function: ease-out;
      animation-iteration-count: infinite;
      animation-direction: absolute;
    }

    @keyframes changeText {
      0%{
        opacity: 1;
      }
      50%{
        opacity:0;
      }
      100%{
        content:"second";
        opacity:1;
      }
    }
<span class="text"></span>

Answer №2

To achieve this using only CSS, it might be necessary to create a separate animation specifically for altering the text and then adjust the timing delay so it executes when the opacity is set to 0, effectively concealing any abrupt transitions.

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

Flexbox not achieving equal height

Having trouble making flexbox work with Susy? Here's what I've tried so far. I've experimented with various solutions found here, but couldn't get it to work consistently across different screen sizes. The heights of text containers an ...

Hidden input for Vue form submission

I need to include two Id's with a user's input by adding them in a hidden input field. Since v-model doesn't work with hidden inputs, I have set up my hidden input like this: <input type="hidden" ref="property_id" :nam ...

Obtain the database username, password, database name, and host information from the user in order to update a specific column in the database

I am a newcomer here and inexperienced in programming. I have developed a script that can modify a database column, but now I aim to acquire the database login information from users. Once users provide all required info correctly, the script will alter th ...

Prevent improper text wrapping of certain words in RTL languages like Farsi and Arabic

In languages like Farsi and Arabic, as well as other RTL languages, letters are connected to each other (e.g. سیب), but not always (e.g. می شود). This can sometimes cause issues with the flow of text when half a word wraps over to the next line due ...

Steps to implement a text border in Raphael.js

I have a website dedicated to creating customized football jersey designs. I'm utilizing Raphael.js for customization, and I am looking for a way to add an outline to text using Raphael. My research led me to the suggestion of using the stroke proper ...

Is there a way to lock an element's position on a webpage once it reaches a specific point?

Excuse me if this query seems trivial. I am currently in the process of designing a webpage with a header that has two distinct sections (upper and lower). My aim is for the lower portion of the header to remain fixed on the page as I scroll down. Is the ...

Why is there a CSS reference failure in Express and how can it be resolved?

Below, you'll find a simple express server I've put together: // check out my basic Express server var express = require("express"); var app = express(); var bodyparser = require("body-parser"); app.use("/public/", express.static("./public/")); ...

Ensure the background image within the ::before selector spans across the entire screen

My CSS code snippet is as follows: div#bodyContainer:before { content: url(/image/colorbar.png); width: 100%; display: block; } Below is the corresponding HTML: <body> <div id="bodyContainer"> ...

Preventing the <img> element from being selectable and draggable simultaneously is effective in Firefox, but not in Chrome or Opera

I have a picture that I want to render both unselectable and undraggable. (This is not an attempt to prevent users from copying) To accomplish this, I can: Add a .noselect class to the <img> tag. As recommended here. Disable pointer-events. (I am ...

Menu design with child element creatively integrated into parent element

For my mobile project, I am working on creating a window blind effect menu. The process is quite simple: Clicking on the menu icon moves the bar to the top while the menu drops down Upon selecting an item, the menu smoothly "rolls" up in a window blind s ...

Is it possible to design and implement Materialize CSS inputs without relying on Materialize CSS?'

Is there a method to implement Materialize CSS input elements that include placeholders and icons without directly using the Materialize CSS framework? I prefer not to mix frameworks in order to avoid potential conflicts, as I am already utilizing anothe ...

What steps can I take to address a 500 internal server error when attempting to execute a Python script within a CGI file?

Looking to execute a python script on a Strato-hosted website (.nl) using a CGI file. The setup includes my code: a cgi file, python file, and dependencies file for adding something to the PATH. Check out the files here. Here's what the folders look ...

Ways to minimize CSS animation when hovering?

Hey there! I've got a question that's pretty broad, so no code to share at the moment. I spent some time yesterday experimenting with different CSS animations, but ran into an issue trying to use the :hover pseudo-class to control them. My go ...

Creating a single div to influence the height of surrounding divs

Here is a fiddle you can check out: http://jsfiddle.net/thiswolf/RGe24/3/ to see the image div and the orange and middle divs stretch to match its height without padding the orange box towards the bottom. Here is the HTML code: <section class="les_hea ...

JavaScript fails to function properly on FireFox

I'm currently troubleshooting a script that works in Chrome but not in FireFox. I suspect it's due to the webkit syntax, so I tried converting it to a standard gradient without success. Can you help me identify what's causing the issue? Web ...

Get the large data file in sections

I ran a test script that looks like this: async function testDownload() { try{ var urls = ['https://localhost:54373/analyzer/test1','https://localhost:54373/analyzer/test2'] var fullFile = new Blob(); for (le ...

Unseen related model fields

I am struggling to figure out what is causing an issue in my code. Can anyone explain why the fields in locations = Location.objects.filter(user=add_profile.user) are not showing up on my html page? models.py class Location(models.Model): user = mode ...

Enhance the functionality of your website by implementing a zoom feature

I am looking to dynamically change the CSS of a div using jQuery when it is clicked. The challenge I am facing is that there are multiple divs with the same class and I am unable to modify their classes as they are created dynamically using PHP. <div ...

I need the table to have a customized sorting order when it first loads, utilizing the tablesorter functionality

When my page loads, I have a table that looks like this: https://i.sstatic.net/Z4tli.png However, I want the subjects to be in a specific order (math, history, science, and physics), while sorting the professor names in ascending order. https://i.sstatic. ...

When using my webrtc technology, I configure my sdp to establish a recvonly direction

While attempting to make a video call using WebRTC, I encountered bugs during testing. My goal is to integrate this project into a webview for my Android app. I conducted testing using phone-pc and phone-phone scenarios. Scenario A: When the PC initialize ...