Creating blinking or flashing text using CSS 3 is a fun way to add eye-catching animation

Here's the current code I'm using:

@-webkit-keyframes blinker {
  from { opacity: 1.0; }
  to { opacity: 0.0; }
}

.waitingForConnection {
  -webkit-animation-name: blinker;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: cubic-bezier(.5, 0, 1, 1);
  -webkit-animation-duration: 1.7s;
}

Currently, the animation only fades out and then reappears with opacity: 1.0, repeating in that cycle. What I would like is for it to fade out and then smoothly transition back to opacity: 1.0. Do you think this is achievable?

Answer №1

You can achieve the blinking effect by setting the opacity to 0 at 50% using CSS animations. This will make the element start from 0% opacity and end at 100%, creating a blinking effect without the need to set it twice.

Check out the demo here

.blink_me {
  animation: blinker 1s linear infinite;
}

@keyframes blinker {
  50% {
    opacity: 0;
  }
}
<div class="blink_me">BLINK ME</div>

In this example, the animation duration is set to 1 second, with a linear timing function for consistent blinking. The animation also repeats indefinitely thanks to the infinite value.

For older versions of Internet Explorer, consider using jQuery or JavaScript as an alternative solution. You may also need to add browser prefixes like -webkit and -moz for compatibility with different browsers. Check out the detailed code here


If you encounter issues with CSS animations, the blinking effect can be achieved using jQuery as well:

(function blink() {
  $('.blink_me').fadeOut(500).fadeIn(500, blink);
})();

Credits to Alnitak for suggesting an improved approach.

See the blinking effect in action using jQuery (Blinker using jQuery)

Answer №2

If you're looking to achieve a classic "100% on, 100% off" blink effect similar to the old <blink> tag, consider using this CSS animation code:

.blinking-text {
  animation: blink-animation 1s step-start infinite;
}

@keyframes blink-animation {
  50% {
    opacity: 0;
  }
}
<div class="blinking-text">YOUR TEXT HERE</div>

Answer №3

To achieve the desired effect, utilize the alternate value for the animation-direction property without the need to define additional keyframes.

alternate

With this setting, the animation will reverse direction with each cycle. When played in reverse, the animation steps are executed backward. Furthermore, timing functions are also reversed; for instance, an ease-in animation becomes an ease-out animation when played in reverse. The iteration count starts at one to determine if it is an even or odd cycle.

CSS:

.waitingForConnection {
  animation: blinker 1.7s cubic-bezier(.5, 0, 1, 1) infinite alternate;  
}
@keyframes blinker { to { opacity: 0; } }

The from keyframe has been omitted deliberately. Its absence results in it being generated from the specified value of the animated property (opacity in this case) on the element. If no value is set (as is the case here), it defaults to 1 for opacity.

Please ensure that both the WebKit version and the unprefixed version are included. For brevity, consider utilizing the shorthand notation.

.waitingForConnection {
  animation: blinker 1.7s cubic-bezier(.5, 0, 1, 1) infinite alternate;  
}
@keyframes blinker { to { opacity: 0; } }

.waitingForConnection2 {
  animation: blinker2 0.6s cubic-bezier(1, 0, 0, 1) infinite alternate;  
}
@keyframes blinker2 { to { opacity: 0; } }

.waitingForConnection3 {
  animation: blinker3 1s ease-in-out infinite alternate;  
}
@keyframes blinker3 { to { opacity: 0; } }
<div class="waitingForConnection">X</div>
<div class="waitingForConnection2">Y</div>
<div class="waitingForConnection3">Z</div>

Answer №4

If you're looking for a way to create smooth animations, give this a try.

    .twinkle {
        animation: twinkleTwinkle 1s infinite;
    }
      
    @keyframes twinkleTwinkle {
        from { opacity: 1.0; }
        50% { opacity: 0.5; }
        to { opacity: 1.0; }
    }
    <span class="twinkle">I am twinkling</span>

Answer №5

If you prefer an instant transition between show and hide instead of a gradual one (like a blinking text cursor), you can achieve that effect with the following code snippet:

/* Make sure to use prefixes with @keyframes and animation for browser support */
@keyframes blinker {  
  from { visibility: visible }
  to { visibility: hidden }

  /* Another approach:
  0% { visibility: visible; }
  50% { visibility: hidden; }
  100% { visibility: visible; }
  if `alternate` is not desired */
}
.cursor {
  animation: blinker steps(1) 500ms infinite alternate;
}

With this code, every 1s, the .cursor will toggle between being visible and hidden.

In cases where CSS animation is not supported (such as some Safari versions), you can rely on a simple JavaScript interval as a fallback option:

(function(){
  var show = 'visible'; // variable to track state toggled by interval
  var time = 500; // time in milliseconds between each interval

  setInterval(function() {
    // Toggle visibility state on each interval
    show = (show === 'hidden') ? 'visible' : 'hidden';

    // Select all cursor elements
    var cursors = document.getElementsByClassName('cursor');

    // Loop through cursor elements and update their visibility based on current state
    for (var i = 0; i < cursors.length; i++) {
      cursors[i].style.visibility = show;
    }
  }, time);
})()

This straightforward JavaScript solution may actually perform faster than CSS animations in certain scenarios. It's important to note that excessive DOM calls are what typically slow down JS animations (e.g. JQuery's $.animate()).

Additionally, a benefit of using this JavaScript method is that any new .cursor elements added later will still animate simultaneously with existing ones since they share the same state - something not achievable with CSS alone.

Answer №6

@-webkit-keyframes blinker {  
  0% { opacity: 1.0; }
  50% { opacity: 0.0; }
  100% { opacity: 1.0; }
}

@-webkit-keyframes blinker {  
  0% { opacity: 1.0; }
  50% { opacity: 0.0; }
  100% { opacity: 1.0; }
}

.blink {
  width: 10px;
  height: 10px;
  border-radius: 10px;
  animation: blinker 2s linear infinite;
  background-color: red;
  margin-right: 5px;
}

.content {
  display: flex;
  flex-direction: row;
  align-items: center;
}
<div class="content">
  <i class="blink"></i>
  LIVE
</div>

Answer №7

For some reason, I've been struggling to get the visibility property to animate properly across all browsers.

One workaround that has worked for me is animating the opacity property in a way that prevents the browser from smoothly fading the text in and out.

Here's an example:

span {
  opacity: 0;
  animation: blinking 1s linear infinite;
}

@keyframes blinking {
  from,
  49.9% {
    opacity: 0;
  }
  50%,
  to {
    opacity: 1;
  }
}
<span>I'm blinking text</span>

Answer №8

Here is my solution to create a blinking effect:

.blink {
 animation: blinkMe 2s linear infinite;
}
@keyframes blinkMe {
 0% {
  opacity: 0;
 }
 50% {
  opacity: 1;
 }
 100% {
  opacity: 0;
 }
}
<p class="blink">Blink</p>

I named the animation 'blinkMe', set the duration to 2 seconds, used a linear timing function, and made it repeat infinitely.

To support older browsers that don't fully support animations and @keyframes, JavaScript and jQuery can be used:

$(document).ready(function() {
 window.setInterval(function() {
  $(".blink").fadeIn(1000).fadeOut(1000);
 }, 2000)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<p class="blink">Blink</p>

If you prefer a blink effect similar to the deprecated blink tag, this code will achieve that:

.blink {
 animation: blink 0.5s step-start infinite;
}
@keyframes blink {
 0% {
  opacity: 1;
 }
 50% {
  opacity: 0;
 }
 100% {
  opacity: 1;
 }
}
<p class="blink">Blink</p>

You can adjust the speed by changing the durations in the CSS animation properties.

Answer №9

Adjust duration and opacity to your liking.

.blink_text { 
    -webkit-animation-name: blinker;
    -webkit-animation-duration: 3s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
    -moz-animation-name: blinker;
    -moz-animation-duration: 3s;
    -moz-animation-timing-function: linear;
    -moz-animation-iteration-count: infinite;
    animation-name: blinker;
    animation-duration: 3s;
    animation-timing-function: linear; 
    animation-iteration-count: infinite; color: red; 
} 

@-moz-keyframes blinker {
    0% { opacity: 1.0; }
    50% { opacity: 0.3; }
    100% { opacity: 1.0; } 
}

@-webkit-keyframes blinker { 
    0% { opacity: 1.0; }
    50% { opacity: 0.3; }
    100% { opacity: 1.0; } 
} 

@keyframes blinker { 
    0% { opacity: 1.0; } 
    50% { opacity: 0.3; } 
    100% { opacity: 1.0; } 
}

Answer №10

This is a great example for everyone to try out at least once.

.blinking_live {
    height: 15px;
    width: 15px;
    border-radius: 15px;
    background: #58C03D;
    animation: blink-live 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
}

@keyframes blink-live{

    0% { opacity: 1.0; }
    50% { opacity: 0.0; }
    100% { opacity: 1.0; }
}


<div class="blinking_live"></div>

Answer №11

Apologies for the delay, but I wanted to share a new example with additional keyframes. You can view the code on CodePen since there was an issue with the embedded code snippets: example on CodePen

.block{
  display:inline-block;
  padding:30px 50px;
  background:#000;
}
.flash-me {
  color:#fff;
  font-size:40px;
  -webkit-animation: flash linear 1.7s infinite;
  animation: flash linear 1.7s infinite;
}

@-webkit-keyframes flash {
  0% { opacity: 0; } 
  80% { opacity: 1; color:#fff; } 
  83% { opacity: 0; color:#fff; } 
  86% { opacity: 1; color:#fff;}  
  89% { opacity: 0} 
  92% { opacity: 1; color:#fff;} 
  95% { opacity: 0; color:#fff;}
  100% { opacity: 1; color:#fff;}
}
@keyframes flash {
  0% { opacity: 0; } 
  80% { opacity: 1; color:#fff; } 
  83% { opacity: 0; color:#fff; } 
  86% { opacity: 1; color:#fff;}  
  89% { opacity: 0} 
  92% { opacity: 1; color:#fff;} 
  95% { opacity: 0; color:#fff;}
  100% { opacity: 1; color:#fff;}
}
<span class="block">
  <span class="flash-me">Flash Me Hard</span>
</span>

Answer №12

https://i.sstatic.net/S1K11.gif

.glow {
  font-size: 20px;
  color: #fff;
  text-shadow: 0 0 8px yellow;
  animation: flicker 6s;
  animation-iteration-count: 1;
}
@keyframes flicker {
  0% {
    opacity: 0.2;
  }
  19% {
    opacity: 0.2;
  }
  20% {
    opacity: 1;
  }
  21% {
    opacity: 1;
  }
  22% {
    opacity: 0.2;
  }
  23% {
    opacity: 0.2;
  }
  36% {
    opacity: 0.2;
  }
  40% {
    opacity: 1;
  }
  41% {
    opacity: 0;
  }
  42% {
    opacity: 1;
  }
  43% {
    opacity: 0.5;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 1;
  }
}

I specified the use of

font-family: "Quicksand", sans-serif;

This is how to incorporate the font (to be included at the top of style.css)

@import url("https://fonts.googleapis.com/css2?family=Quicksand:wght@300&display=swap");

Answer №13

<style>
    .class1{
        height:100px;
        line-height:100px;
        color:white;
        font-family:Bauhaus 93;
        padding:25px;
        background-color:#2a9fd4;
        border:outset blue;
        border-radius:25px;
        box-shadow:10px 10px green;
        font-size:45px;
    }
     .class2{
        height:100px;
        line-height:100px;
        color:white;
        font-family:Bauhaus 93;
        padding:25px;
        background-color:green;
        border:outset blue;
        border-radius:25px;
        box-shadow:10px 10px green;
        font-size:65px;
    }
</style>
<script src="jquery-3.js"></script>
<script>
    $(document).ready(function () {
        $('#div1').addClass('class1');
        var flag = true;

        function blink() {
            if(flag)
            {
                $("#div1").addClass('class2');
                flag = false;
            }
            else
            { 
                if ($('#div1').hasClass('class2'))
                    $('#div1').removeClass('class2').addClass('class1');
                flag = true;
            }
        }
        window.setInterval(blink, 1000);
    });
</script>

Answer №14

My solution involves utilizing class=blink for the specific element(s) to achieve the desired effect.

Straightforward JavaScript Code

// Blink
      setInterval(function()
        {

        setTimeout(function()
        {

        //$(".blink").css("color","rgba(0,0,0,0.1)"); // You can opt for a simple black/white blink effect of text
        $(".blink").css("visibility","hidden"); // Adjusting visibility of the element  

        },900);

        //$(".blink").css("color","rgba(0,0,0,1)");  // You can choose a simple black/white blink effect of text
        $(".blink").css("visibility","visible");  // Tweaking visibility of the element

        },1000);

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

I am unable to make any changes to the CSS in the WordPress code that is already integrated

I have created a static web page to serve as my primary homepage. The page is built using HTML and CSS, with some PHP code pulled from My WordPress integrated into it. One of the functionalities I've added is to display the three most recent posts on ...

What is the best way to ensure that the div from the header file remains fixed directly above the fixed div from another file?

This is the header section that I want to keep fixed within the "header" div. <div id="header" style="display:block;"> <table style="width:100%"> <tr> <td class="col-sm-6" style="background-color:lavender;"><a href ...

Utilizing jQuery for displaying or hiding list elements

To view all the code, click on this link: http://jsfiddle.net/yrgK8/ A section titled "news" is included in the code snippet below: <ul id="news"> <li><p>asfdsadfdsafdsafdsafdsafdsafdsafdsa</p></li> <li>&l ...

Distribute divs of equal width evenly within a grid

I'm facing a unique challenge at the moment. I'm attempting to create a grid system where all elements have a fixed width of 200px each. What I envision is a clever grid setup using only CSS, where each "row" will strive to accommodate as many el ...

loading dynamic content into an appended div in HTML using Angular

Here is the HTML code from my app.component.html file: <button mat-raised-button color="primary" mat-button class="nextButton" (click)="calculatePremium()"> Calculate </button> <div id="calcul ...

"Clicking on the hamburger menu causes the webpage to reset its scroll

I recently integrated a mobile hamburger menu into my website, which is primarily built around a single page. However, I noticed that whenever I open the menu, it automatically scrolls to the top of the page regardless of where you were previously scrollin ...

Creating a grid layout by designing boxes using CSS and HTML

I'm in the process of creating a homepage and I'd like it to resemble this design: (not the best quality, but the software I used was subpar (I miss mspaint)) Originally, I used buttons which made linking and customization easy, but I encounter ...

What steps are involved in creating a webpage cleaner similar to Arc90's Readability or Instapaper?

Curious about cleaning up an HTML page to present it in a more polished manner - eliminating clutter and restructuring the main text for better readability, similar to resources like or Instapaper. Would the process involve basic page parsing and filteri ...

Custom Wikipedia HTML template for a unique and immersive user experience

I am looking to develop a wiki website similar to Wikipedia using .Net, but I am having difficulty finding an HTML template that resembles it. Can anyone provide information on the template or framework used by Wikipedia and where I can get it? ...

What could be causing jQuery to overlook the content within my div element?

I have a basic toggle feature for a div connected to a checkbox. $('#step2').hide(); $('#toggle-check').click(function(){ $('#step2').fadeToggle(); }) Why is .hide() not working to hide the contents of #step2? The content ...

Hide overflow for images with unique shapes

Here are the two image layers: One shows a hand holding a phone, and the other is a black hole. This is how you can achieve it using HTML markup: <div class="wrapper"> <img class="wrapper__image" src="path/to/image.png"> </div> And ...

What is the best way to create a layout with two images positioned in the center?

Is it possible to align the two pictures to the center of the page horizontally using only HTML and CSS? I've tried using this code but it doesn't seem to work: #product .container { display: flex; justify-content: space-between; flex-w ...

Issue encountered while trying to display images next to each other using only HTML and CSS

I am facing an issue with the vertical space between the top and bottom rows of my portfolio. There seems to be some unwanted space in the rows that I cannot account for. It doesn't seem to be a margin or padding within the box, so I suspect the probl ...

Achieving a full width vertical navigation on the left side using Bootstrap's grid system: a step-by

I am looking to design an admin panel using bootstrap. To start off, I want to create a navigation menu on the left side. However, I encountered an issue where my navigation menu with a red background does not stretch the full width of the left column. Her ...

Is this considered a table?

In the realm of web development, there is a well-known mantra: "Only use tables for tabular data." This advice stems from a time when tables were misused for all sorts of layout purposes. While I generally adhere to this guideline, there are moments when ...

Is it possible to implement the 'opacity' feature on the homepage of my HTML website?

I am currently working on my website and I would like to display a notice to members when they open the site. This notice should only appear once, right above the HTML homepage, as the homepage content may vary for different members. I plan to use 'op ...

The images on the carousel fail to load unless I adjust the window size

After investing countless hours into finding a solution, I am still unable to resolve this issue. The problem lies within my use of a Carousel and setting the images through a javascript file. Strangely enough, upon loading the page, only the first image ...

Astro Project experiencing issues with loading SRC folder and style tags

After setting up a brand new astro repository with the following commands: npm create astro@latest npm run dev I encountered an issue where the default project template failed to display correctly on my computer. Here is how the page appeared: https://i. ...

What is the best way to eliminate the left margin entirely in CSS?

I am attempting to create an image view that fully covers the window, without any margins. I have tried various solutions such as setting the body margin and padding to 0, but they do not seem to work. body { margin: 0px; padding: 0px; } or *, html { ...

Utilizing Bootstrap 4 for a responsive layout: positioning a <div> element adjacent to a centered image

I am currently working on a project involving an image gallery. My goal is to showcase the images in a specific layout: https://i.sstatic.net/cXcql.png Essentially, the image should always be centered. If there is sufficient space to the right, I want to ...