How can I efficiently invert text and background colors using CSS animation?

My objective is to create an animation that causes text to invert color between its background and its own color. Currently, the animation successfully flips the colors the first time, but struggles to transition back to the default colors, resulting in a muddled color in between. Here is the CSS code I am using for the animation:

body {
  background: #3866af;
  color: #ffebb5;
}

.blink {
  animation: blink-animation 1s steps(2, start) infinite;
}

@keyframes blink-animation {
  from {
    background: #3866af;
    color: #ffebb5;
  }
  to {
    background: #ffebb5;
    color: #3866af;
  }
}
This is how text would normally look 
<br>
<span class="blink">and this is what blinking text looks like right now</span>

My goal is for the text to turn blue and have a beige background.

I am striving to achieve this effect using CSS alone, without resorting to JavaScript.

Thanks!

EDIT:

The solution I have decided to implement is based on Farhad Bagherlo's answer:

body {
  background: #3866af;
  color: #ffebb5;
}

.blink {
  animation: blink-animation 1s steps(2, start) infinite;
}

@keyframes blink-animation {
    0% {
       background-color:#3866af;
       color: #ffebb5;
    }
    50% {
       background-color:#3866af;
       color: #ffebb5;
    }
    51% {
       background-color: #ffebb5;
       color:#3866af;
    }
    100% {
       background-color: #ffebb5;
       color:#3866af;
    }
}
This is how text would normally look 
<br>
<span class="blink">and this is what blinking text looks like right now</span>

Answer №1

body {
  background: #3866af;
  color: #ffebb5;
}

.blink {
  animation: blink-animation 1s steps(4, start) infinite;

}

@keyframes blink-animation {
0% {
       background-color:#3866af;
    color: #ffebb5;
  }
  50% {
      background-color:#3866af;
    color: #ffebb5;
  }
  75% {
  background-color: #ffebb5;
    color:#3866af;
  }
  100% {
    background-color: #ffebb5;
    color:#3866af;
  }
}
This example demonstrates how text would typically appear 
<br>
<span class="blink">as well as showcasing the current appearance of blinking text</span>

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

Incorporating and designing an external document

Another website has a file that I would like to include in one of my own pages. The URL format is as follows: http://example.com/path/with/some/variables I could use an iframe to accomplish this, but I also want to customize the CSS of elements within th ...

Picture not adapting correctly to various screen sizes

I'm looking to adjust the size of an image based on the user's browser window. Here is the code I have so far: .image { max-height: 15%; width: auto; } The image class is used in this section of my code: <ul> <li> <img c ...

unable to render the JavaScript code

Utilizing JavaScript post an ajax request to showcase the data in a div using new JS content. The example code is provided below: //ajax call from a.jsp var goUrl = "/testMethod/getTestMethod; var httpRequest=null; var refreshCont ...

Remove the outline on a particular input and retain only its border

Even after trying to use "outline: 0", nothing seems to change. #input_field { outline: 0; width: fit-content; margin: auto; margin-top: 20%; border-radius: 30px; border: 2px solid turquoise; padding: 6px; } <div id="input_field"> ...

The navigation bar is not showing up at the top of my webpage as expected, instead, it is displaying

1 I attempted to build a model Google site to enhance my HTML and CSS skills, but I encountered an unusual issue where my navigation menu is showing up inside my image div, even though it was created before that. I have given backgrounds to the navs and i ...

Discovering the compiled CSS file in React when using Sass: A step-by-step guide

In my React project, I have incorporated SASS. Now I am looking to find the final compiled CSS file from that SASS code. Whenever I navigate to: While there is the SCSS file visible, where can I locate the CSS version? I am referring to the compiled outp ...

Iterate through each field with getElementsByName to retrieve values triggering the onChange event

I am working on a contact form that includes inputs, selectors and radio buttons. Doubt 1: I am trying to figure out how to retrieve the index of a field using the getElementsByName method with the onChange event. Would a For Loop be the appropriate solut ...

Applying box-shadow to tables and collapsing borders with border-collapse property in IE!

The box-shadow property does not function properly in Internet Explorer when the table has a border-collapse: collapse style applied to it. ...

Issue with Bootstrap4: organizing columns correctly within and outside a container

I've hit a roadblock while working on a project and can't seem to find the right solution. I've already created a design in Photoshop to illustrate what I'm trying to achieve: Photoshop Design. Basically, I want the main content inside ...

Develop a game timer using CreateJS

Looking for advice on the most effective method to create a timer clock using Createjs. I've attempted to reference HTML elements with DOMElement in the past, but encountered difficulties. Essentially, I need to display a timer within a container so p ...

Mudblazor - Tap or click within the designated area to trigger the drag and drop functionality

I have incorporated the Mudblazor CSS framework into my Blazor WebAssembly project. Within the drag and drop zone, I have included a button that is supposed to remove each uploaded image. However, when I click on the remove button, it only opens up the fi ...

What is the best way to make hover text that also copies when you highlight the main text?

Currently, I am utilizing a basic bootstrap tooltip for creating tables with jQuery. However, I require a specific functionality that the standard bootstrap tooltip does not provide. I need the ability to copy the base text and have it appear as "base tex ...

What is the best way to eliminate the last dropdown divider class from a list of items in VueJS

I'm currently facing an issue with a dropdown divider in my Vue.js bootstrap application. I have a list of items and I want to add a line between each item using the dropdown-divider class. While this is working fine, I now need to remove the last lin ...

What is the best way to create an inline div that includes a background image and text

<div class="header_section"> <div class="icon"></div> example </div>​ .header_section{ background: #C2E1FF; height: 24px; line-height: 24px; padding: 5px; } .icon{ background: url(http://mcgrefer.com/ ...

Removing lines of text from the output of a JavaScript retrieval

Currently, I have a working script that retrieves a complete address block to be inserted into a selected field. However, I am now attempting to remove unnecessary parts of the generated text block for better display. This is the code snippet: <script ...

Clicking on a row in Rails bootstrap does not produce the desired result

I'm trying to make a row clickable in my Rails app using Bootstrap 4.1.3 to create the table. Rails version: 5.2.1 <tbody> <% @animals.each do |animal| %> <tr data-link="<%= animal_path(animal) %>"> <td&g ...

Sliding panels with Jquery

I've created some basic panels that slide in and out horizontally. My goal is to hide all content except the first panel when the page loads. When a link to other panels is clicked, the current content will slide left to be hidden and new content will ...

The functionality of reordering columns, virtual scrolling, and resizing the grid in jqgrid are not functioning properly

Implementing jqgrid with Symfony to display a datagrid has been a challenging task for me. Thanks to Oleg's insightful response, many of the major issues have been resolved. Below is a snippet of my code: <link rel="stylesheet" type="text/css" ...

Connecting to particular sections on other pages

My website setup includes a page titled "news.html" that contains an iframe with fixed size. The iframe is linked to "innernews.html", which is the actual content to be displayed. I structured it this way for consistent page sizing, as the iframe prevents ...

What is the process for implementing document.ondrop with Firefox?

I'm experiencing an issue where the document.ondrop function seems to be working in Chrome, but not in Firefox. Here's a link to an example demonstrating the problem: In the example, if you try to drop a file onto the page, it should trigger an ...