Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

A revised method of defining link pseudo-classes

Rated 4.36 (Ratings: 13) (Add your rating)

Log in to add a comment
(12 comments so far)

Want more?

 
Picture of bmason

Bill Mason

Member info | Full bio

User since: October 10, 2000

Last login: April 15, 2007

Articles written: 5

Defining link pseudo-classes in CSS

In the classic case of defining your link pseudo-class styles, this has been the traditional way to do it:

a:link { /*styles*/ }
a:visited { /*styles*/ }
a:hover { /*styles*/ }
a:active { /*styles*/ }

As the CSS2 specification observes, the order of the pseudo-classes is important so that the cascading rules present the correct styles to the browser.

Recently, I began using another method derived from some notes by David Baron. The rules are as follows:

:link:focus, :visited:focus { /*styles*/ }
:link { /*styles*/ }
:visited { /*styles*/ }
:link:hover, :visited:hover { /*styles*/ }
:link:active, :visited:active { /*styles*/ }

I like this style for several reasons:

  • Since <a> is not specified, it avoids the problem of having named anchors (i.e. <a name="anchor" id="anchor">this is an anchor<a>) be matched, especially by an a:hover definition. Since Mozilla corrected itself and began matching a:hover to named anchors, there have been multiple bug reports from individuals who do not understand that this is actually correct behavior.
  • I finally have a good idea on how to put :focus into my styles and have it cascade properly.
  • The system degrades satisfactorily in browsers that don't fully understand this type of style definition. My testing showed that browser support worked as follows:
Does the browser display styles correctly using the new pseudo-class rule method?
Browser (platform) :link :visited :focus :hover :active
IE4+ 1 (PC) Yes Yes No; treated as :active Yes Yes
IE 5.x (Mac) Yes Yes Yes Yes Yes
IE 4.x (Mac) Yes Yes No; treated as :active Yes Yes
IE 3.x (PC and Mac) 2 No No No No No
Mozilla 1+ (PC) 3 Yes Yes Yes Yes Yes
Netscape 6+ (PC) 3 Yes Yes Yes Yes Yes
Netscape 4.x (PC and Mac) Yes Yes No No No
Opera 7.x (PC) Yes Yes No Yes Yes
Opera 6.x (PC), 5.0 (Mac) Yes Yes No No No
Opera 3.62 (PC) 4 No No No No No
iCab (Mac) Yes Yes No No No
MSN TV (formerly WebTV) 5 Yes Yes No No No

Table notes:

  1. IE 4 (PC): I do not have a local install handy to test this with. This result is from David Baron's original page of notes.
  2. IE 3: The links work, but the body of the text will take the styles of the :focus definition.
  3. Mozilla 1+, Netscape 6+ (PC): I presume these behave the same on all platforms, but do not have installs on other platforms to test with.
  4. Opera 3.62: This version only tries to support :link and :visited in any event, but suffers from bugs where a link's text-decoration or border changes color but the link text does not.
  5. MSN TV: These results are based on testing with their emulator software, version 2.6. In the MSN TV CSS documentation, it claims that :link, :visited, :active, and :hover are supported. I have never seen the emulator support anything but :link and :visited. The Style Checker function of the TopStyle CSS editor agrees that only :link and :visited are supported.

Wrapup

As the table shows, only Opera 5/6 actually lose functionality that they otherwise have using the traditional link pseudo-class style. In the new system, they no longer understand :active and :hover. In other browsers, any functionality that doesn't work is something that didn't work under the traditional system anyway. Given that I can live with the Opera problem, I personally have begun using this system on various sites.

Anyone with corrections or additions, please feel free to comment. Just offhand, I would be interested in hearing first-hand how this system works in Konqueror and an actual MSN TV setup. David Baron maintains a test page that you can use.

Thank you to David Baron for permission to use his original work as the basis for this article.

Front-end web development (mostly) and an interest/focus in web accessibility. I also recently co-authored my first book, The Web Professional's Handbook, published by the late great glasshaus.

I've been out of work since October 2001. Be kind and glance over my portfolio....

Related article

Submitted by molotov on August 27, 2002 - 16:33.

Good article. Some more code for thought: A:hover and named anchors This is also based on David Baron's notes, focused on the issue with named anchors affected by pseudo classes.

login or register to post comments

Re: Related article

Submitted by bmason on August 27, 2002 - 17:37.

I actually ran across that one shortly after finishing my article. I'm not big on his solution only because name has been dropped going forward from XHTML 1.1. So eventually you would have to revisit the issue again in your code. I'm content to let Opera not get it for now and wait for it to catch up.

login or register to post comments

Re: Related article

Submitted by timrivera on September 2, 2002 - 08:30.

Bill, you say that name has been dropped, I assume being replaced by id. Therefore, if name is not used, then you should have no problem whatsoever using A:dynamic_pseudo-class. There is no need for an id anchor tag as just about any tag can be ID'd.

The only reason to use anything other than A:dynamic_pseudo-class is to avoid having the effect rendered for name anchors. Any workaround should be used only when using named anchors.

A:dynamic_pseudo-class and named anchors

login or register to post comments

No problem?

Submitted by bmason on September 2, 2002 - 09:18.

On the contrary, of course I have a problem with that.

  • All the sites/developers out there are not going to switch to XHTML 1.1 overnight:
    • People still want/need the many deprecated items (besides name) dropped from 1.1
    • XHTML 1.1 has to be served as application/xhtml+xml, and the server/browser universe is not ready to make that move en masse
  • HTML 4.01 remains a valid specification, and it includes name

So name is here to stay for awhile. Which is why I like this method.

(David Baron also observes in his original notes that this method has advantages when doing XML as well.)

login or register to post comments

Re: No problem?

Submitted by timrivera on September 7, 2002 - 21:42.

Okay, you agree that the name attribute is here to stay, and the main reason for this method is to avoid unwanted effects with named achors.

Since the unwanted effects occur only in the newer browsers, it is logical to me to use a method involving CSS2 that deals with only these newer browsers, leaving the older browsers alone.

By using the CSS2 attribute selector [name] with simply inherited values to counter-declare any declarations made for the link when named anchors are present, the problem is fixed and no browser suffers any consequences. I see no reason to use a method that works in only some browsers and causes more problems in other browsers, while a simple solution exists that corrects the problem for every browser and does not cause any more problems in any browser.

login or register to post comments

Re: No problem

Submitted by bmason on September 7, 2002 - 22:34.

Well, that's why I cleverly titled this "A" method and not "The" method. I prefer not to write an extra a[name] rule into my CSS when I don't need it.

I like the Opera browser, but whether or not an Opera user can see a hover effect or not isn't something I'm going to toss and turn over.

Besides which, it may (or may not, admittedly) be useful going into XHTML 2.0 to not have to go back and untie my hyperlink CSS from the A tag, since anything can be a link in the current XHTML 2.0 draft and will need styling that reflects that.

login or register to post comments

More on using CSS2 selectors

Submitted by bmason on September 7, 2002 - 22:42.

Besides which, what do you in those cases where your <A> tag is both a hyperlink and carries a name to be an anchor (which I've had cause to do from time to time)? Write yet another rule? I prefer this method for its simplicity.

login or register to post comments

Re: No problem

Submitted by timrivera on September 7, 2002 - 23:45.

I prefer not to write an extra a[name] rule into my CSS when I don't need it.
The [name] rule would be used only in the presence of named anchors, which aren't very common in comparison with regular links.
whether or not an Opera user can see a hover effect or not isn't something I'm going to toss and turn over
The issue isn't about not seeing the hover effect. The issue is the hover effect being rendered on the links at all times (only declarations not already made for :link.)
...have to go back and untie my hyperlink CSS from the A tag
If that is a concern, :pseudo-class works just fine in the browsers on my system without specifying an element.
what do you in those cases where your tag is both a hyperlink and carries a name to be an anchor? Write yet another rule?
Okay, so there is one complication to this method. Some people don't care about Opera users because they are a small minority compared to IE users, just as I'm not too concerned over this complication because named anchors aren't used very abundantly, and links with a name are especially infrequent.

In my opinion, named anchors aren't very common (compared to href links.) When I want to use named anchors, it would cause very little effort to write an extra [name] rule. In contrast, you would rather combine pseudo-classes to be used for every link. Both of our suggested methods may have its advantages and disadvantages, and there may be occasions where it would be best to use one method instead of the other. In my opinion, it would be best to define links the classic way, and worry about a workaround only when necessary.

login or register to post comments

Re: No problem

Submitted by bmason on September 8, 2002 - 00:05.

  • I run into named anchors every day. I guess we surf different places.
  • I don't see how you can say Opera is not the issue (or not the only issue) when you just finished arguing that one reason for not doing CSS this way is "no browser suffers any consequences&quot.
  • "...worry about a workaround only when necessary.": My only point is the way I code (and coding can be as much a matter of personal style as anything, I admit), I write it and it's done. No workarounds (at least I haven't found a reason to need any yet). And clearer code for those that follow me, if I'm not going to be maintaining it going forward.
  • Since for some reason you have <a> tags around your last few paragraphs, I now see a meaningless hover effect on them in Mozilla. I wouldn't see that in my method.

login or register to post comments

Re: no problem

Submitted by glazou on November 18, 2002 - 04:51.

Saying that "named anchors aren't very common" shows that you should View the Source more often...

login or register to post comments

Opera 7 update

Submitted by bmason on November 18, 2002 - 09:22.

Tested out beta 1 of Opera 7 and found no :focus support however the CSS was written, old method or new. But it does otherwise support this newer method of link CSS.

login or register to post comments

I know it's late in the day

Submitted by meganox on June 15, 2004 - 12:55.

but I'd like to clarify for any newcomers to css (and there's plenty, thank god) that this is the optimal method for defining link styles, and that if you want the same behaviour in IE and Moz you should be sure to make your definition for :link:focus, :visited:focus at the top exactly the same as your definition for :link:active, :visited:active at the bottom.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.