-1

If i have applied a transition property to an element, i have to declare the properties in style tag, but how can i incorporate the whole declaration within the style attribute of the element?

div {
background-color: teal;
padding: 20px 20px 20px 20px;
transition: all 1s;
}
div:hover {
background-color: green;
}
<div></div>

In the above example, is it possible to simplify the code by including the transition property in style attribute of div element?

Community
  • 1
  • 1

1 Answers1

3

You can have all the declarations inside the style tag. However, the pseudo classes do not work inside the style attribute of the element.

You can possibly use the below snippet as a workaround or else define it in the stylesheet ofcourse.

<div style="width: 100%; height: 50px; background-color: red; transition: all 1s" onmouseover="this.style.backgroundColor='yellow';" onmouseout="this.style.backgroundColor='red';">
</div>
Ankit Sharma
  • 1,674
  • 8
  • 11