If you style just the <a>, the styles will also apply to all the psuedo classes, and if any of these psuedo classes required different styling, only the changes in styling need to be defined.
The full order should be:
link, visited, focus, hover, active
I say that should be the order, it doesn't have to be, but you'll get unexpected behaviour otherwise.
Whenever you define hover, you should also define focus, because not all users navigate with the mouse, some tab through the links and without using :focus there's no visual clue as to where they're up to.
You don't need to use any pseudo-classes at all though, a{} will suffice if you don't want to do anything special.
When you do want to do something special though, one or all of these can be deployed:
:link - A link that has not been visited.
:visited - A link that has been visited.
:focus - When the link is selected, either by tabbing to it or otherwise.
:hover - When the mouse is over the link.
:active - When the link is actually being clicked on (alternatively if it is selected and the enter key is depressed).
:hover will also work for other elements (no need to use :focus with other elements though), however it won't in IE6 or lower. So you could use it to add a bit of extra eye candy for those who use browsers that support it, and if you feel like, you could also add in some JavaScript to cater for IE <=6.
The psuedo-classes were only added in CSS 2, pretty much all browsers support CSS 2 now, and the very few old ones still in use aren't really popular enough to worry about. However, whenever I write selectors I still include a plain anchor selector to. Generally this is how it looks for me:
yes you could have either a class or an id in a link however a class can be used more than once per page and could apply to several links and an id can only be used once per page.
However, how he has it as #some-id .some-class a:hover {} means any anchor inside an element of class "some-class" which is also inside the id of "some-id" will be affected. It is an order of specificity so that you can define specific things without affecting others.
Yeah, I was just using #some-id .some-class as an addition to the selector to demonstrate that you can also combine other selectors with the pseudo-class link selectors however you like.
Every fight is a food fight when you’re a cannibal.
really...? i didn't know that!
i'm sure i have used the same more than once on a page!!
i better go and check some of my code.
what happens if u do use the same id twice?
y is it wrong to do so?
i've been using them as if they were like a class!
Originally Posted by smyhre
However, how he has it as #some-id .some-class a:hover {} means any anchor inside an element of class "some-class" which is also inside the id of "some-id" will be affected. It is an order of specificity so that you can define specific things without affecting others.
erm... uve lost me with the above. can u explain further what u mean?
If you use an id of the same name more than once not only will it not varify at http://validator.w3.org/ unexpected things could happen it kinda throws the browser off and it might not display it right.
As for the other part say you have this:
HTML Code:
<div id="some-id"><p class="some-class">
Find out about our <a href="fine-wine.html">Fine Wine</a> selection and our <a href="wfamous-cheese.html">world famous cheese.</a></p><p>Get some <a href="info.html">information</a> about our other products</p></div><div id="footer"><p>Do you want to <a href="contact.html">contact us?</a></p></div>
Now for the CSS part:
If you wanted to define all links hovers on the page you could use:
Code:
a:hover {color:blue;}
But what if you only want to define the hovers of links inside the div id of "some-id" and leave the links in the footer alone? You would use something like this:
Code:
#some-id a:hover {color: blue;}
However, what if you only wanted to modify the links of anything within the div id of "some-id" with class of "some-class" and not the links of the footer or the paragraph under it? You would use something like:
Code:
#some-id .some-class a:hover {color:blue;}
By writing in the id, class, and the anchor and not just one or the other you can be very specific about which parts of the site are modified just in case you don't want that attribute to affect other parts of the site.
<div id="some-id"><p class="some-class">
Find out about our <a href="fine-wine.html">Fine Wine</a> selection and our <a href="wfamous-cheese.html">world famous cheese.</a></p><p>Get some <a href="info.html">information</a> about our other products</p></div><div id="footer"><p>Do you want to <a href="contact.html">contact us?</a></p></div>
Now for the CSS part:
If you wanted to define all links hovers on the page you could use:
Code:
a:hover {color:blue;}
But what if you only want to define the hovers of links inside the div id of "some-id" and leave the links in the footer alone? You would use something like this:
Code:
#some-id a:hover {color: blue;}
However, what if you only wanted to modify the links of anything within the div id of "some-id" with class of "some-class" and not the links of the footer or the paragraph under it? You would use something like:
Code:
#some-id .some-class a:hover {color:blue;}
By writing in the id, class, and the anchor and not just one or the other you can be very specific about which parts of the site are modified just in case you don't want that attribute to affect other parts of the site.
Did that help to clarify what I meant?
thats really useful and helps a lot.
i think i've gone too long writing badly formatted code.
thanks.
Bookmarks