Nothing is for sale here. Freewill tips keep the site running. Want to help? → Tip via Paypal

CSS Pseudo-classes

In addition to named CSS classes, generic classes, and ID classes, there are also pseudo-classes. A pseudo-class allows you to define a change in the display for certain states of specific elements. If you rest your cursor on some links you will see a change of font color or some other visual change, that’s an example of a pseudo-class rule.

Pseudo-class rules are coded like a regular class rule, with the following two exceptions:

  • They have predefined names; you can’t make up your own class name.
  • Instead of separating the element and class name with a period, they are separated with a colon.

The four pseudo-classes below are ones you can create rules for to create a change of state for links:

a:link
a:visited
a:hover
a:active

In the preceding pseudo-classes, the letter “a” before the semicolon stands for anchor because some sailor made it up.

OK, not really, I made that up, but I used to be a sailor so it’s OK. The truth is that “a” is short for anchor because it represents the HTML anchor tag. An anchor tag is a link to another page on your website, to another website, to a file, or to a specific place within the current web page. In other words, it’s the HTML element for a link.

After each anchor pseudo-class you’d add the CSS property and value as you would with any other style declaration (rule). Here’s an example:

a:link {color: blue;}

Simple! Just like with the other classes, we add a property and value to the selector and that is all that is needed. Here’s a brief description for those four pseudo-classes:

a:link
This sets the properties and values of a link before the link has been visited.
a:visited
This sets the properties and values of a link that the browser has already visited.
a:hover
This sets the properties and values of a link when the cursor is resting on it. This is commonly used to change the color of the link text.
a:active
This sets the properties and values of a link when it has been activated (clicked) until it is processed. With the advent of broadband Internet connections and faster servers and personal computers, many times this change of state isn’t even noticeable.

While most of the time webmasters just use the above pseudo-classes to change the colors of the link text for each state, other properties and values can be added or changed as well, such as; font weight, text decoration, font style, background color, borders and more.

When creating a style sheet using the “a” element pseudo-classes, they must be in this order:

a:link
a:visited
a:hover
a:active

If they are not in that order, things can break down and changes of state won’t work properly in some browsers.

Below is a list of all the CSS pseudo-classes:

Selector
Example
Description

:active
a:active
Affects the active link

:checked
input:checked
Affects all checked input elements

:disabled
input:disabled
Affects all disabled input elements

:empty
div:empty
Affects all division elements that have no children

:enabled
input:enabled
Affects all enabled input elements

:first-child
p:first-child
Affects any paragraph element that’s the first child of its parent

:first-of-type
p:first-of-type
Affects any paragraph element that’s the first paragraph element of its parent

:focus
input:focus
Affects the input element that has the current focus

:hover
a:hover
Affects a link when the cursor is placed over it

:in-range
input:in-range
Affects input elements with a value within a specified range

:invalid
input:invalid
Affects all input elements with an invalid value

:lang(language)
p:lang(en)
Affects all paragraph elements with an “sv” lang attribute value (sv = Swedish)

:last-child
p:last-child
Affects all paragraph elements that are the last child of its parent

:last-of-type
p:last-of-type
Affects all paragraph elements that are the last paragraph element of its parent

:link
a:link
Affects all unvisited links

:not(selector)
:not(p)
Affects any element that is not a paragraph element

:nth-child(n)
tr:nth-child(even)
Affects every other table row (that’s how we colored every even row in this table)

:nth-last-child(n)
p:nth-last-child(2)
Affects any paragraph element that is the second child of its parent, counting from the last child

:nth-last-of-type(n)
p:nth-last-of-type(2)
Affects any paragraph element that is the second paragraph element of its parent, counting from the last child

:nth-of-type(n)
p:nth-of-type(3)
Affects an paragraph element that is the third paragraph element of its parent

:only-of-type
p:only-of-type
Affects any paragraph element that is the only paragraph element of its parent

:only-child
p:only-child
Affects any paragraph element that is the only child of its parent

:optional
input:optional
Affects all input elements with no “required” HTML attribute

:out-of-range
input:out-of-range
Affects input elements with a value outside a specified range

:read-only
input:read-only
Affects input elements with a “readonly” attribute specified

:read-write
input:read-write
Affects input elements with no “readonly” attribute

:required
input:required
Affects all input elements with a “required” attribute specified

:root
root
Affects the document root element

:target
#news:target
Affects the current active #news element (clicked URL containing that anchor name)

:valid
input:valid
Affects all input elements with a valid value

:visited
a:visited
Affects all visited links

If that list looks intimidating, relax. I SAID RELAX! You won’t use or even need to know most of them. You’ll find tutorials for the ones we feel are most useful in our Web Design tutorials list, and we may add tutorials for all of them over time.