0

I know you can import a css file that contains, let's say:

div {
    color: black; 
}

div:hover {
    color: red;
}

but is there a way to get the same effect but in the html?

So something with

<div style="color: black;"></div>

I KNOW you can do it with js, but I just wanted to know if there were a hack for it.. And no the "duplicate" question did not contain the answer I was looking for, keep in mind that was 4 years ago.

Oussama el Bachiri
  • 18,687
  • 3
  • 15
  • 22

4 Answers4

3

Pseudo classes are not allowed to use as inline CSS, so the short answer is NO, you cannot do what you are trying to achieve.

Consider using <style> tags at document level, or you can use JavaScript if you want to..

<a href="#" onmouseover = "this.style.color = '#000'" 
            onmouseout  = "this.style.color = '#f00'">Hi</a>

Demo

And as you commented, still the answer is no, even using HTML5/CSS3 there's no way you can use pseudo classes inline.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
2
<div onmouseover="this.style.color='black'">I wouldn't suggest this</div>
1

There is no way as far I know. But you can still realize it with js.

<div
   onmouseover="this.style.color='#ff0000'"
   onmouseout="this.style.color='#000000'">
</div>
Aldi Unanto
  • 3,626
  • 1
  • 22
  • 30
0

Have you tried this?

<div style=":hover {color: red}">Testing inline hover</div>
Bernardao
  • 466
  • 6
  • 23
  • Write on comment please – Aldi Unanto Jul 31 '13 at 08:34
  • Testing inline hover – Bernardao Jul 31 '13 at 08:35
  • @AldiUnanto I don't think he has commenting rights – Mr. Alien Jul 31 '13 at 08:35
  • Don't understand the -1 I was refering to the link provided in the first answer. That possibility appear ins W3C http://www.w3.org/TR/2002/WD-css-style-attr-20020515 It doesn't work on browsers but it's there – Bernardao Jul 31 '13 at 08:42
  • The same answer I gave was given in the original question and got 7 positive votes. http://stackoverflow.com/questions/1033156/how-to-write-ahover-in-inline-css#comment9295011_1033166 In the questions says he knows it can be done with js, and the solutions given are in javascript... – Bernardao Jul 31 '13 at 09:43