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.

5
  • stackoverflow.com/questions/1033156/… Commented Jul 31, 2013 at 8:31
  • stackoverflow.com/questions/131653/… Commented Jul 31, 2013 at 8:33
  • 1
    There was no answer there... this is 4 years later and it might be able now we have html5 and css3 Commented Jul 31, 2013 at 8:33
  • @OussamaDooby +1 for the thought, but researching a little bit would have helped to not open another duplicate question. Commented Jul 31, 2013 at 8:37
  • There just might be someone out(here/there) who has a little hack for it... We all know css is FULL of hacks Commented Jul 31, 2013 at 9:11

4 Answers 4

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.

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

Comments

2
<div onmouseover="this.style.color='black'">I wouldn't suggest this</div>

Comments

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>

Comments

0

Have you tried this?

<div style=":hover {color: red}">Testing inline hover</div>

5 Comments

Write on comment please
<div style=":hover {color: red}">Testing inline hover</div>
@AldiUnanto I don't think he has commenting rights
Don't understand the -1 I was refering to the link provided in the first answer. That possibility appear ins W3C w3.org/TR/2002/WD-css-style-attr-20020515 It doesn't work on browsers but it's there
The same answer I gave was given in the original question and got 7 positive votes. stackoverflow.com/questions/1033156/… In the questions says he knows it can be done with js, and the solutions given are in javascript...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.