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: 32% [?]