Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to apply CSS transitions inline?

Tags:

html

css

Is it possible to apply CSS transitions inline (in the DOM), without using Pseudo Selector or using JS to add and change a property class on the dom? .

#target{
  width: 100px;
  height:100px;
  background-color:red;
}
<div id="target" style="transition: opacity 1s linear;"></div>
like image 597
Radex Avatar asked Nov 24 '16 11:11

Radex


2 Answers

Transitions need a changing value to produce any effect. This is usually achieved, as you mentioned, by toggling a class or using a pseudo selector.

If you want your "transition" to happen without any changing values, you need to use an animation:

#target{
  width: 100px;
  height:100px;
  background-color:red;
}

@keyframes fadeMe {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div id="target" style="animation: fadeMe 2s;"></div>

I'm not sure why you would need to do this inline though.

like image 64
Turnip Avatar answered Sep 18 '22 23:09

Turnip


Combining with inline javascript you can achieve that, try this one:

#target{
  width: 100px;
  height:100px;
  background-color:red;  
}
<div id="target" style="transition: all 4s linear;" onclick="this.style.opacity = '.3'">click me</div>
like image 23
RizkiDPrast Avatar answered Sep 19 '22 23:09

RizkiDPrast