Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate a flashing text cursor in HTML

What's a good way to insert a flashing cursor (e.g. a flashing thick underscore in DOS or vertical bar in Linux) in HTML?

This character/image would be trailing the H1 header and should look good at different sizes.

like image 725
Knocks X Avatar asked Mar 20 '16 19:03

Knocks X


People also ask

How to create a blinking cursor in CSS?

To animate the cursor, first, we need to define a keyframe called cursor-blink that will reduce the opacity of the cursor to 0 when the keyframe is “0%”. Once done, we can now use this keyframe on the cursor using the animation shorthand like so.


1 Answers

You can do this with just CSS, here is vertical bar Fiddle

h1 {
  font-size: 20px;
  padding-right: 20px;
  display: inline-block;
}

h1:after {
  content: '';
  width: 20px;
  height: 2px;
  background: black;
  opacity: 0;
  display: inline-block;
  animation: blink 1s linear infinite alternate;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<h1>Lorem ipsum dolor sit amet</h1>

Or you can use JQuery and setInterval

setInterval(function() {
  $('span').animate({
    opacity: 1
  }, 600).animate({
    opacity: 0
  }, 600)
}, 1200);
h1 {
  font-size: 20px;
  padding-right: 20px;
  display: inline-block;
}

h1 span {
  width: 20px;
  margin-bottom: -1px;
  height: 2px;
  background: black;
  opacity: 0;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Lorem ipsum dolor sit amet <span></span></h1>
like image 122
Nenad Vracar Avatar answered Sep 27 '22 16:09

Nenad Vracar