Improperly Styling HTML Links

posted August 25th 2006 at 0244 EDT in All, Articles, CSS, Firefox, HTML, IE, Web

The part that always trips me up, is you can't just style all anchor tags the same, you really only want


a:link, a:visited {color:#00f;}
 

NOT


a{color:#00f;}
 

because you could have


<a name="anchorID">heading</a>
 

... which you would NOT want styled with all the other anchor elements because it is not a hyperlink.

When styled improperly you will see a link style in Firefox (and probably IE 7). IE6 of course makes up its own rules to how the style should be applied.

Why would you use the name attribute of anchor tag's anyway? It allows you to jump to a specific section of the page by appending the anchorID after a hash mark (#) in the url. For example http://jehiah.com/archive/styling-links#Comments will jump you to the comments section

An alternative instead of the name attribute is to use an id attribute instead. They have nearly the same effect, just remember that ID's have to be unique on an individual page.

5 Responses

  1. #1 Tony Petruzzi
    3 years, 6 months ago

    Your post is confusing me. I have no idea what your really trying to get across.

  2. #2 Sandra Clark
    3 years, 6 months ago

    What he seems to be trying to say (and correct me if I am wrong) is that when you try to style links with css, you do not want to style links as:

    a{ color: orange}

    When you do that, you not only style reference links <a href=”">, but you also end up styling named anchor links <a name=”">, which are normally used to drop people to another part of the page. Perhaps you want to style those differently.

    Using the pseudo selectors, :link and :visited, in CSS you can end up styling only those links which use the href=”" attribute.
    :link and :visited are only valid for those links. If you go back to HTML 3.2, they work in the same way that the link and vlink attributes in the body tag used to.

    So to style both the link and the visited states of a link, you could say

    :link{color:orange;}
    :visited{color:red;}

  3. #3 Mike
    3 years, 6 months ago

    perhaps a simple workaround is to not put anything inside your anchors. If they are empty, they still act as anchors but absorb no formatting. Put them NEXT to the phrase you want anchored.

    Used with much success on my own websites.

  4. #4 jehiah
    3 years, 6 months ago

    @Sandra: Thats exactly what i’m trying to say, thanks for clarifying =)

    @Mike: Yes that would be an effective workaround as well (I’ve also used that in the past, but I forgot about it)

  5. #5 Jen
    3 years, 3 months ago

    @Mike: It does work well, but if accessibility is a concern, you need something in your a name