-1

I just need to blink the background color only, not the Text. And I am unable to do that. I need your help.

.blink{
        width:200px;
        height: 50px;
            background-color: magenta;
        padding: 15px;  
        text-align: center;
        line-height: 50px;
    }
.text{
        font-size: 25px;
        font-family: cursive;
        color: white;
        animation: blink 1s linear infinite;
    }
@keyframes blink{
0%{opacity: 0;}
25%{opacity: .5;}
50%{opacity: 1;}
75%{opacity: .5;}
100%{opacity: 0;}
}
<div class="blink">
  <span class="text">blinking text</span></div>
Ahmad Shah
  • 75
  • 5

2 Answers2

5

You actually added the animation property in the text class instead of the blink. Just shift it.

Also, if you're animating the opacity then the content inside the div will also fade. Considering that you only wish to animate the background, animate the background-color instead.

.blink{
        width:200px;
        height: 50px;
        background-color: magenta;
        padding: 15px;  
        text-align: center;
        line-height: 50px;
        animation: blink 1s linear infinite;
    }
.text {
        font-size: 25px;
        font-family: cursive;
        color: black;
}
    
@keyframes blink {
  0%{background-color: magenta;}
  50%{background-color: white;}
  100%{background-color: magenta;}
}
<div class="blink">
  <span class="text">blinking text</span>
</div>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Vaibhav
  • 562
  • 5
  • 12
1

Instead of this:

.blink{
        width:200px;
        height: 50px;
            background-color: magenta;
        padding: 15px;  
        text-align: center;
        line-height: 50px;
    }
.text{
        font-size: 25px;
        font-family: cursive;
        color: white;
        animation: blink 1s linear infinite;
    }
@keyframes blink{
0%{opacity: 0;}
25%{opacity: .5;}
50%{opacity: 1;}
75%{opacity: .5;}
100%{opacity: 0;}
}
<div class="blink">
  <span class="text">blinking text</span></div>

Do this:

.blink{
        width:200px;
        height: 50px;
            background-color: magenta;
        padding: 15px;  
        text-align: center;
        line-height: 50px;
        animation: blink 1s linear infinite;
    }
.text{
        font-size: 25px;
        font-family: cursive;
        color: white;
    }
@keyframes blink{
0%{opacity: 0;}
25%{opacity: .5;}
50%{opacity: 1;}
75%{opacity: .5;}
100%{opacity: 0;}
}
<div class="blink">
  <span class="text">blinking text</span></div>
D-Waqas
  • 84
  • 9