CSS Tip – Using Link Anchor Pseudo Classes
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.
Popularity: 27% [?]
This is very helpful. Thank you.
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
This is a great post. Thank you for sharing this informative post.
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.
Thanks for such a nice post regarding anchor tag. It help me a lot.
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.
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.