Lesson 6: Links
Ak Patel
---
You can apply what you already learned in the previous lessons to
links (i.e. change colors, fonts, underline, etc). The new thing is that
CSS allows you to define these properties differently depending on
whether the link is unvisited, visited, active, or whether the cursor is
on the link. This makes it possible to add fancy and useful effects to
your website. To control these effects you use so-called pseudo-classes.
Let's look at an example. As you know, links are specified in HTML with
A link can have different states. For example, it can be visited or
not visited. You can use pseudo-classes to assign different styles to
visited and unvisited links.
Use
We will now go through each of the four pseudo-classes with examples and further explanation.
In the code example below, unvisited links will be light blue.
This example gives active links a yellow background color:
This can be used to create interesting effects. For example, if we want our links to be orange and be italicized when the cursor is pointed at them, our CSS should look like this:
The two examples gives you an idea about the almost endless
possibilities for combining different properties. You can create your
own effects - give it a try!
You should consider carefully whether it is necessary to remove the underlining as it might decrease usability of your website significantly. People are used to blue underlined links on web pages and know that they can click on them. Even my mum knows that! If you change the underlining and color of links there is a good chance that users will get confused and therefore not get the full benefit of the content on your website.
That said, it is very simple to remove the underlining of links. As you will recall from lesson 5, the property
Alternatively, you can set
What is a pseudo-class?
A pseudo-class allows you to take into account different conditions or events when defining a property for an HTML tag.Let's look at an example. As you know, links are specified in HTML with
<a> tags. We can therefore use a as a selector in CSS:
a {
color: blue;
}
a:link {
color: blue;
}
a:visited {
color: red;
}
a:link and a:visited for unvisited and visited links respectively. Links that are active have the pseudo-class a:active and a:hover is when the cursor is on the link.We will now go through each of the four pseudo-classes with examples and further explanation.
Pseudo-class: link
The pseudo-class:link is used for links leading to pages that the user has not visited.In the code example below, unvisited links will be light blue.
a:link {
color: #6699CC;
}
Pseudo-class: visited
The pseudo-class:visited is used for links leading to
pages that the user has visited. For example, the code below would make
all visited links dark purple:
a:visited {
color: #660099;
}
Pseudo-class: active
The pseudo-class:active is used for links that are active.This example gives active links a yellow background color:
a:active {
background-color: #FFFF00;
}
Pseudo-class: hover
The pseudo-class:hover is used when the mouse pointer hovers over a link.This can be used to create interesting effects. For example, if we want our links to be orange and be italicized when the cursor is pointed at them, our CSS should look like this:
a:hover {
color: orange;
font-style: italic;
}
Example 1: Effect when the cursor is over a link
It is particular popular to create different effects when the cursor is over a link. We will therefore look at a few extra examples related to the pseudo-class:hover.Example 1a: Spacing between letters
As you will remember from lesson 5, the spacing between letters can be adjusted using the propertyletter-spacing. This can be applied to links for a special effect:
a:hover {
letter-spacing: 10px;
font-weight:bold;
color:red;
}
Example 1b: UPPERCASE and lowercase
In lesson 5 we looked at the propertytext-transform, which can switch between upper- and lowercase letters. This can also be used to create an effect for links:
a:hover {
text-transform: uppercase;
font-weight:bold;
color:blue;
background-color:yellow;
}
Example 2: Remove underline of links
A frequently asked question is how to remove the underlining of links?You should consider carefully whether it is necessary to remove the underlining as it might decrease usability of your website significantly. People are used to blue underlined links on web pages and know that they can click on them. Even my mum knows that! If you change the underlining and color of links there is a good chance that users will get confused and therefore not get the full benefit of the content on your website.
That said, it is very simple to remove the underlining of links. As you will recall from lesson 5, the property
text-decoration can be used to determine whether text is underlined or not. To remove underlining, simply set the value of text-decoration to none.
a {
text-decoration:none;
}
text-decoration along with other properties for all four pseudo-classes.
a:link {
color: blue;
text-decoration:none;
}
a:visited {
color: purple;
text-decoration:none;
}
a:active {
background-color: yellow;
text-decoration:none;
}
a:hover {
color:red;
text-decoration:none;
}