7

I have an animation like below:

@keyframes pulseColorGreen {
    0% {
        background-color: green;
    }
    97%{
        background-color: green;
    }
    100% {
      background-color: white;
    }
  }

My website also has a dark mode where the background goes black. Now in the animation, I want to display a darker green just like below:

@keyframes pulseColorGreen {
    0% {
        background-color: darkgreen;
    }
    97%{
        background-color: darkgreen;
    }
    100% {
      background-color: black;
    }
  }

For the dark mode, I add a class "lightsOff" in the body tag. I am managing all other color changes using the combination of the target element class and "lightsOff" class just like:

.lightsOff .someclass{
    color: white
}

But when defining an element in css file as:

@keyframes .lightsOff pulseColorGreen {
    0% {
        background-color: darkgreen;
    }
    97%{
        background-color: darkgreen;
    }
    100% {
      background-color: black;
    }
  }

It gives me error saying "identifier expected". I am adding the animation-name property to elements dynamically from javascript. Hence, I won't prefer changing the JS code to identify which theme is set currently and set a keyframe accordingly. How can I achieve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kashyap Kotak
  • 1,888
  • 2
  • 19
  • 38
  • Hi Kashyap. I notice your posts are what we'd call "chatty". That means they contain pleasantries and niceties for the purpose of pleading for assistance. We prefer posts without that here, and you may find that sometimes if a reader believes a post is too noisy, they will downvote and move on. Thus, if you can aim for brevity, we'd appreciate it. Reference discussions on _Meta Stack Overflow_ are available on request. – halfer Aug 04 '18 at 18:02

1 Answers1

11

You can use CSS variable and you will need only one keyframes that you don't have to change. Simply change the main class that define the color:

.light {
  --c: yellow;
}

.dark {
  --c: darkblue;
}

.box {
  height: 100px;
  margin: 5px;
  box-shadow: 0 0 5px var(--c);
  color:#fff;
  animation: pulseColorGreenMkt 1s infinite linear alternate;
}

@keyframes pulseColorGreenMkt {
  0% {
    background-color: var(--c);
  }
  100% {
    background-color: black;
  }
}
<div class="box light">
  Class defined on the element
</div>
<div class="dark">
  <div class="box">
    Class defined on a parent element
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • I explored that option earlier, but CSS variables are not supported in IE (correct me if wrong). I would like to have a cross browser solution with atleast 2-3 years old browsers supported – Kashyap Kotak Jul 29 '18 at 13:53
  • @KashyapKotak Ah! if you are looking for IE support then I think the only way is to do like you said ... change your JS code to identify each case – Temani Afif Jul 29 '18 at 13:58
  • I did not test my website with IE. after commenting here, I opened it first time in IE and found that many things did not work :P So I am ok to leave IE. But I see on https://caniuse.com/#feat=css-variables page saying "In Edge 15 animations with css variables may cause the webpage to crash". I would definitely want to support EDGE though. Please let me know if I need to worry on the issue given on that link – Kashyap Kotak Jul 29 '18 at 14:05
  • @KashyapKotak Yes this link is pretty reliable, so if you find something there you should consider it but you should also follow the link of the bug to know exactly what is the bug and its current state ... probably it's a particular case that you are not facing – Temani Afif Jul 29 '18 at 14:07