Sooraj Chandran Sooraj Chandran - 3 months ago 15
CSS Question

Change content of div in keyframes animation CSS

I have following code.



#mf-loader-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 500px;
height: 30px;
}
.mf-loader-circle {
position: absolute;
height: 30px;
width: 30px;
border-radius: 50%;
border: 2px solid #03C9A9;
top: -15px;
background: white;
text-align: center;
line-height: 30px;
color: #03C9A9;
}
.mf-loader-text {
position: absolute;
width: 150px;
top: 20px;
}
#one-text {
left: -10px;
-webkit-animation: cl 3s;
}
#two-text {
left: 200px;
-webkit-animation: cl 3s;
-webkit-animation-delay: 2s;
-webkit-animation-fill-mode: forwards;
color: rgba(1, 1, 1, 0);
}
#three-text {
left: 480px;
-webkit-animation: cl 3s;
-webkit-animation-delay: 3s;
-webkit-animation-fill-mode: forwards;
color: rgba(1, 1, 1, 0);
}
@-webkit-keyframes cl {
from {
color: rgba(1, 1, 1, 0);
}
to {
color: rgba(1, 1, 1, 1);
}
}
#two {
left: 240px;
}
#three {
left: 490px;
}
#mf-loader {
width: 100%;
height: 3px;
background: #03C9A9;
position: absolute;
-webkit-animation: mymove 5s;
/* Chrome, Safari, Opera */
animation: mymove 5s;
border-radius: 3px;
}
/* Chrome, Safari, Opera */

@-webkit-keyframes mymove {
0% {
width: 0px;
}
50% {
width: 50%;
}
100% {
width: 100%;
}
}
/* Standard syntax */

@keyframes mymove {
0% {
width: 0px;
}
50% {
width: 50%;
}
100% {
width: 100%;
}
}

<div id="mf-loader-container">

<div id="mf-loader">
<div class="mf-loader-circle" id="one">
1
</div>
<div class="mf-loader-circle" id="two">
2
</div>
<div class="mf-loader-circle" id="three">
3
</div>
<div class="mf-loader-text" id="one-text">
Each day will be better than last.
<br>This one especially
</div>
<div class="mf-loader-text" id="two-text">
Subscribing .. Thank you for subscribing. We appreciate it!
</div>
<div class="mf-loader-text" id="three-text">
DONE
</div>
</div>
</div>





It is an animated checkout-like action which is done with CSS alone. I'm trying to change the content of the circle once the text appears to check mark
&#10003;
- is there any way to change the content using content tag in css.

Answer

Change the content (the 1, 2, 3 numbers) to :after pseudo-elements and set the content there. Then, apply that to each of the circles. With this, you can include them into your animation by changing content:""; Change "check" to whatever you need.

.mf-loader-circle:after {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
#one:after {
  content: "1";
  -webkit-animation: check1 3s;
  -webkit-animation-fill-mode: forwards;
}
#two:after {
  content: "2";
  -webkit-animation: check2 3s;
  -webkit-animation-delay: 2s;
  -webkit-animation-fill-mode: forwards;
}
#three:after {
  content: "3";
  -webkit-animation: check3 3s;
  -webkit-animation-delay: 3s;
  -webkit-animation-fill-mode: forwards;
}
@-webkit-keyframes check1 {
  from {
    content: "1";
  }
  to {
    content: "check";
  }
}
@-webkit-keyframes check2 {
  from {
    content: "2";
  }
  to {
    content: "check";
  }
}
@-webkit-keyframes check3 {
  from {
    content: "3";
  }
  to {
    content: "check";
  }
}
#mf-loader-container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 500px;
  height: 30px;
}
.mf-loader-circle {
  position: absolute;
  height: 30px;
  width: 30px;
  border-radius: 50%;
  border: 2px solid #03C9A9;
  top: -15px;
  background: white;
  text-align: center;
  line-height: 30px;
  color: #03C9A9;
}
.mf-loader-text {
  position: absolute;
  width: 150px;
  top: 20px;
}
#one-text {
  left: -10px;
  -webkit-animation: cl 3s;
}
#two-text {
  left: 200px;
  -webkit-animation: cl 3s;
  -webkit-animation-delay: 2s;
  -webkit-animation-fill-mode: forwards;
  color: rgba(1, 1, 1, 0);
}
#three-text {
  left: 480px;
  -webkit-animation: cl 3s;
  -webkit-animation-delay: 3s;
  -webkit-animation-fill-mode: forwards;
  color: rgba(1, 1, 1, 0);
}
@-webkit-keyframes cl {
  from {
    color: rgba(1, 1, 1, 0);
  }
  to {
    color: rgba(1, 1, 1, 1);
  }
}
#two {
  left: 240px;
}
#three {
  left: 490px;
}
#mf-loader {
  width: 100%;
  height: 3px;
  background: #03C9A9;
  position: absolute;
  -webkit-animation: mymove 5s;
  /* Chrome, Safari, Opera */
  animation: mymove 5s;
  border-radius: 3px;
}
/* Chrome, Safari, Opera */

@-webkit-keyframes mymove {
  0% {
    width: 0px;
  }
  50% {
    width: 50%;
  }
  100% {
    width: 100%;
  }
}
/* Standard syntax */

@keyframes mymove {
  0% {
    width: 0px;
  }
  50% {
    width: 50%;
  }
  100% {
    width: 100%;
  }
}
<div id="mf-loader-container">

  <div id="mf-loader">
    <div class="mf-loader-circle" id="one">
    </div>
    <div class="mf-loader-circle" id="two">
    </div>
    <div class="mf-loader-circle" id="three">
    </div>
    <div class="mf-loader-text" id="one-text">
      Each day will be better than last.
      <br>This one especially
    </div>
    <div class="mf-loader-text" id="two-text">
      Subscribing .. Thank you for subscribing. We appreciate it!
    </div>
    <div class="mf-loader-text" id="three-text">
      DONE
    </div>
  </div>
</div>

Here's a summary of the changes:


  • Removed the content from your circles in the HTML.

  • Created an :after pseudo-element, common to all your circles.

.mf-loader-circle:after {
  position:absolute;
  width:100%;
  height:100%;
  left:50%;
  top:50%;
  transform:translate(-50%, -50%);
}

Here, I used 2D transforms to position the number in the center with position:absolute;

  • Set each of the content:""; attributes to the necessary numbers using the elements' IDs.

  • Created an animation for the different numbers, for example:


#one:after {
  content:"1";
  -webkit-animation: check1 3s;
  -webkit-animation-fill-mode: forwards;
}
@-webkit-keyframes check1{
  from {
    content:"1";
  }
  to {
    content:"check";
  }
}

NOTE: add the corresponding vendor prefixes.

Comments