break free by learning
  • Home
  • About
  • Free Stuff

CSS Tip – Using Link Anchor Pseudo Classes

Apr 28, 2008 | css | fyd

Let's take a look at styling anchor tags, or links, using CSS. We will be going over the CSS pseudo-classes that are used with links – link, visited, hover, and active. First, let's take a look at the link pseudo-class in CSS.

:link – this pseudo-class is used to style a normal unvisited link. When you are styling links you should put this pseudo-class first.

a:link
{
color: #339966;
}

Notice that we use the HTML tag, or selector, a because that is how a link is represented in HTML. The :link represents the pseudo-class. Then you set your properties between the curly braces. This CSS will change all the unvisited links to a green color.

:visited – this pseudo-class is used to style a visited link. When you are styling links you should put this pseudo-class second.

a:visited
{
color: #00ffff;
}

You will see that most sites will set the visited link color to the same color as the normal a:link color.

:hover – this pseudo-class is used to style the mouse over effect of a link. When you are styling links you MUST put this after a:link and a:visited.

a:hover
{
color: #FF0000;
}

:active – this pseudo-class is used to style an activated link element. When you are styling links you MUST put this after a:hover.

a:active
{
color: #FF00FF;
}

This effect is seen while clicking the link. You will find the a lot of sites will style a:hover and a:active the same.

Putting it together in the HTML.

<html>
<head>

<title>Link and pseudo-classes</title>

<!-- Embedded Styles -->
<style type="text/css">
a:link
{
color: #339966;
}

a:visited
{
color: #00FFFF;
}

a:hover
{
color: #FF0000;
}

a:active
{
color: #FF00FF;
}

</style>

</head>

<body>
<p>
<a href="#">A Link</a> Lorem ipsum dolor sit amet,
consectetuer adipiscing elit, sed <a href="#">diam nonummy
nibh</a> euismod tincidunt ut laoreet dolore magna aliquam
erat volutpat.
</p>

A Link Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

You can see how the styles effect the links. You can also combine these anchor CSS pseudo-classes with regular CSS classes. This can help you style links differently throughout the page.

a.className:link, a.className:visited
{
color: #ff0000;
text-decoration: none;
}

a.className:hover, a.className:active
{
color: #00ff00;
text-decoration: underline;
}

The above CSS shows you how to add a class name to the pseudo-classes and how to style multiple elements at the same time. className can be whatever you want to name your class. You can put the :link and :visited pseudo-classes together by adding a comma between declarations. The HTML would look like this:

<p>
<a class="className" href="#">A Link</a> Lorem ipsum
dolor sit amet, consectetuer adipiscing elit,
sed <a class="className" href="#">diam nonummy nibh</a> 
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</p>

All you need to do is add class="className" to the anchor tag.

A Link Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Styling links using CSS is easy – just remember to put the pseudo-classes in the correct order – :link, :visited, :hover, :active.

The HTML

Popularity: 27% [?]

Related Post:

  • CSS Tip – Different Color Borders (0)
  • CSS Custom Search Field or Textfield (38)
  • CSS Gradient Background Fade (19)

7 Responses to “CSS Tip – Using Link Anchor Pseudo Classes”

  1. Alice Audrey says:
    May 10, 2008 at 8:40 pm

    This is very helpful. Thank you.

    Reply
  2. css says:
    November 13, 2008 at 5:48 am

    Cascading Style Sheets (CSS) web design lessons
    Css link Properties Attributes – examles

    http://css-lessons.ucoz.com/link-css-examples-1.htm
    http://css-lessons.ucoz.com/link-css-examples-2.htm

    Reply
  3. Jeremy D. Ayala says:
    April 21, 2010 at 12:23 pm

    This is a great post. Thank you for sharing this informative post.

    Reply
  4. Pansy Cowsert says:
    May 28, 2010 at 6:16 am

    When I design a sight I design first and code second. I find that using pure CSS layout , like an obnoxious backseat driver, often ends up stearing the design a little bit in the end. This is most often a problem when floating elements inside a centered div – the darn footer elements colapse upwards; don't float use positining and margins? Crossbrowser catastrophe! Usually one simple table takes care of so many things.

    Reply
  5. Waqas Bukhari says:
    January 11, 2011 at 12:38 am

    Thanks for such a nice post regarding anchor tag. It help me a lot.

    Reply
  6. plr says:
    June 5, 2011 at 1:37 pm

    Hi, is there away to combine a:link and others?

    like

    .ph a:link, a:visited {color:#414141;}
    .ph a:active, a:hover {color:#ffa82a;background-color:#fff;}

    Please help, its not working.

    Reply
  7. M says:
    July 14, 2011 at 7:55 am

    plr – try this maybe?

    .ph a:link, .ph a:visited {color:#414141;}
    .ph a:active, .ph a:hover {color:#ffa82a;background-color:#fff;}

    you need to add the class in front of the a:visited and a:hover like .ph a:visited.

    Reply

Leave a Reply

Click here to cancel reply.

Categories

  • css (8)
  • freelance (2)
  • illustrator (1)
  • inspiration (4)
  • jquery (1)
  • photoshop (9)
  • resources (8)
  • site news (6)
  • the basics (4)
  • web design (1)
  • whats good (1)
  • wordpress (1)

Recent Posts

    Archives

    • April 2010 (1)
    • April 2009 (1)
    • May 2008 (3)
    • April 2008 (14)
    • March 2008 (8)
    • February 2008 (8)
    • January 2008 (11)

    ©2012 freeyourdesign | Designed by Michael Hargis | | twitter icons