I have a case where I must write inline CSS code, and I want to apply a hover style on an anchor.
How can I use a:hover
in inline CSS inside the HTML style attribute?
E.g. you can't reliably use CSS classes in HTML emails.
I have a case where I must write inline CSS code, and I want to apply a hover style on an anchor.
How can I use a:hover
in inline CSS inside the HTML style attribute?
E.g. you can't reliably use CSS classes in HTML emails.
Inline pseudoclass declarations aren't supported in the current iteration of CSS (though, from what I understand, it may come in a future version).
For now, your best bet is probably to just define a style block directly above the link you want to style:
Short answer: you can't.
Long answer: you shouldn't.
Give it a class name or an id and use stylesheets to apply the style.
:hover
is a pseudo-selector and, for CSS, only has meaning within the style sheet. There isn't any inline-style equivalent (as it isn't defining the selection criteria).Response to the OP's comments:
See Totally Pwn CSS with Javascript for a good script on adding CSS rules dynamically. Also see Change style sheet for some of the theory on the subject.
Also, don't forget, you can add links to external stylesheets if that's an option. For example,
Caution: the above assumes there is a head section.
More accurate to say, you could do it at some point in the past. But now (according to the latest revision of the same standard, which is Candidate Recommendation) you can't ...
using Javascript:
a) Adding inline style
b) or a bit harder method - adding "mouseover"
Note: multi-word styles (i.e.
font-size
) in Javascript are written together:element.style.fontSize="12px"
You can't do exactly what you're describing, since
a:hover
is part of the selector, not the CSS rules. A stylesheet has two components:Inline styles only have rules; the selector is implicit to be the current element.
The selector is an expressive language that describes a set of criteria to match elements in an XML-like document.
However, you can get close, because a
style
set can technically go most anywhere:I agree with shadow. You could use the
onmouseover
andonmouseout
event to change the CSS via JavaScript.And don't say people need to have JavaScript activated. It's only a style issue, so it doesn't matter if there are some visitors without JavaScript ;) Although most of Web 2.0 works with JavaScript. See Facebook for example (lots of JavaScript) or Myspace.