-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?

6
  • but what about hover :hover?? Commented Jul 1, 2018 at 8:45
  • @לבנימלכה that's the problem Commented Jul 1, 2018 at 8:46
  • Also read: stackoverflow.com/questions/8284365/… Commented Jul 1, 2018 at 8:46
  • @Abhishek I mean why do it inside tag if you have css allready ??? Commented Jul 1, 2018 at 8:47
  • we don't have any method to use pseudo in inline css. stackoverflow.com/a/1033166/7512762 Commented Jul 1, 2018 at 8:48

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, its js but it helped
if you do not want use css you must use js/jquery to mouse hover event
@AnkitSharma ok bro, you did nice job

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.