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 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
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.
show example
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:show example
Pseudo-class: active
The pseudo-class:active
is used for links that are active.This example gives active links a yellow background color:
show example
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:
show example
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:show example
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:show example
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!
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.Alternatively, you can set
text-decoration
along with other properties for all four pseudo-classes. show example
Summary
In this lesson you have learned about pseudo-classes, using some of the properties from the previous lessons. This should give you an idea of some the possibilities CSS provides.In the next lesson we will teach you how to define properties for specific elements and groups of elements.