WDD  Β·  Web Design & Development

CSS: Navigation and Pseudo-classes

Lesson 9 of 13 Approx 1 hr (single period)

Learning intentions

  • Know what a CSS pseudo-class is and why it is used
  • Know and use the four link pseudo-classes: a:link, a:visited, a:hover, a:active
  • Know how to style a <nav> bar by removing bullet points, underlines, and adding hover effects
  • Understand why changing background colour on hover improves usability

Success criteria

  • I can explain what a pseudo-class is in CSS
  • I can write a:hover and a:visited rules to style links in different states
  • I can write CSS to remove underlines from links and bullet points from a nav list
  • I can explain why a hover colour change improves the usability of a navigation bar
Warm up β€” what do you already know?

Answer before the lesson begins.

1. Which CSS pseudo-class applies when the mouse pointer is over an element?

2. Which CSS property removes bullet points from a list?

3. Which CSS property removes the default underline from a link?

Key vocabulary

pseudo-class
A CSS selector that targets an element in a specific state β€” for example, when the mouse is hovering over it. Written with a colon: a:hover.
a:hover
A pseudo-class that applies CSS when the mouse pointer is over a link. Used to create visual feedback on hover β€” a colour or background change.
a:visited
A pseudo-class that applies CSS to links the user has already clicked and visited. Browsers show visited links as purple by default.
a:active
A pseudo-class that applies CSS at the exact moment a link is being clicked (while the mouse button is held down).
a:link
A pseudo-class that applies CSS to unvisited links only. Browsers show unvisited links as blue underlined by default.
navigation bar
A styled set of links, usually in a <nav> element, allowing users to move between pages. Typically created by styling an unordered list of anchor elements.
text-decoration
CSS property that adds or removes text decoration β€” most commonly used as text-decoration: none to remove the browser's default underline from links.
list-style-type
CSS property that controls the bullet point type in a list. list-style-type: none removes the bullets entirely, which is essential when using a <ul> for navigation.

CSS: Navigation and Pseudo-classes

What is a pseudo-class?

A pseudo-class is a special type of CSS selector that targets an element based on its current state β€” not just what it is, but what is happening to it right now. The colon (:) separates the element selector from the pseudo-class name:

a:hover {
  /* applies when mouse is over the link */
}
a:visited {
  /* applies to links the user has already clicked */
}

Pseudo-classes let you style elements differently depending on how users are interacting with them β€” making interfaces more responsive and easier to use.

The four link pseudo-classes

Links (<a> elements) have four state pseudo-classes. These are often referred to as LVHA β€” the recommended order to write them in, to avoid conflicts:

Pseudo-classWhen it appliesBrowser default
a:linkUnvisited link β€” user has not clicked it yetBlue, underlined
a:visitedVisited link β€” user has clicked it beforePurple, underlined
a:hoverUser's mouse is currently over the linkNo change (stays same as :link)
a:activeLink is being clicked (mouse button held down)Red

Browsers add their own default styles to links. In most cases, web designers override these completely using CSS to match their site's design.

a:link Unvisited link User has not clicked it yet colour: #185FA5 β†’ a:visited After clicking User has been to this page colour: #7F77DD β†’ a:hover Mouse over Cursor is above the link colour: #1D9E75 β†’ a:active While clicking Mouse button held down colour: #D85A30 Remember: LVHA β€” Link, Visited, Hover, Active

The four pseudo-class states of a link β€” define them in LVHA order to avoid specificity conflicts

Default browser nav (before CSS)

Without any CSS, a <nav> with a <ul> looks like this:

The links are blue, underlined, and close together β€” not ideal for navigation.

Styled nav bar (after CSS)

With CSS, the same links can look like this β€” clean, dark text on a dark background with a highlight on hover:

The CSS that achieves the styled version:

/* Style the nav container */
nav {
  background-color: #042C53;
  padding: 0.5rem;
}

/* Remove bullets if using ul/li for nav links */
nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  display: flex;
}

/* Style the links */
nav a {
  color: #ffffff;
  text-decoration: none;
  padding: 0.4rem 0.9rem;
  border-radius: 4px;
}

/* Hover state β€” visual feedback */
nav a:hover {
  background-color: #185FA5;
}

Styling all four link states

You can write separate rules for each link state. Remember the LVHA order β€” if you write :hover before :link, the hover effect may not work due to specificity:

a:link    { color: #185FA5; }            /* unvisited β€” blue */
a:visited { color: #9090b0; }            /* visited β€” grey */
a:hover   { color: #042C53; background-color: #E6F1FB; }  /* hover */
a:active  { color: #1D9E75; }            /* being clicked */

Why hover effects improve usability

A usability principle states that interactive elements should give feedback when they are interacted with. When a user moves their mouse over a navigation link, a background colour change:

  • Makes it clear the element is clickable (users know it is interactive)
  • Helps users track which link they are about to click
  • Reduces the chance of clicking the wrong link

Without any hover effect, all links look the same before and during hovering, which reduces clarity and usability.

Worked examples

Example 1 β€” Before and after: unstyled vs styled nav

Compare unstyled nav links with styled nav links. The HTML structure is the same β€” only the CSS differs.

HTML (same in both cases):

<nav>
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="gallery.html">Gallery</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</nav>

Without CSS: Blue underlined links with bullet points.

With CSS (styled version):

nav { background-color: #042C53; padding: 0.6rem 1rem; }
nav ul { list-style-type: none; padding: 0; display: flex; gap: 0.25rem; }
nav a { color: #fff; text-decoration: none; padding: 0.4rem 0.9rem; border-radius: 4px; }
nav a:hover { background-color: #185FA5; }
Example 2 β€” Writing all four link state rules

Write CSS to style links in all four states: unvisited = #185FA5 (blue); hover = #042C53 text on #E6F1FB background; visited = #9090b0 (grey); active = #1D9E75 (green).

1
Write the rules in LVHA order: link, visited, hover, active.
2
For hover, add both a text colour and a background-color to create a clear highlight effect.
3
Remove the default underline with text-decoration: none on the base a rule.
a { text-decoration: none; }

a:link    { color: #185FA5; }
a:visited { color: #9090b0; }
a:hover   {
  color: #042C53;
  background-color: #E6F1FB;
}
a:active  { color: #1D9E75; }
Example 3 β€” Styling a nav with ul/li links

Write CSS to style a <nav> containing a <ul> of links. Remove bullet points, remove underlines, display items horizontally, and add a hover background.

1
Target nav ul to remove bullets and default list padding.
2
Target nav a to remove underlines and set the link colour.
3
Target nav a:hover to change the background colour when hovering β€” giving users clear feedback.
nav ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

nav a {
  text-decoration: none;
  color: #042C53;
  padding: 0.5rem 1rem;
  display: block;
}

nav a:hover {
  background-color: #E6F1FB;
}
Now you try

An existing page has a <nav> with a <ul> of three links. The links currently appear as a bulleted list of blue underlined text. Write CSS rules to:

  • Remove the bullet points from the nav ul
  • Make the nav a links appear dark blue (#042C53) with no underline
  • Add a light blue background (#E6F1FB) on nav a:hover
nav ul {
  list-style-type: none;
  padding: 0;
}

nav a {
  color: #042C53;
  text-decoration: none;
}

nav a:hover {
  background-color: #E6F1FB;
}
Common mistakes
βœ•
Writing hover before link or visited (ignoring LVHA order). If you write a:hover before a:visited, the visited state may override hover because it comes later and has the same specificity. Always write link states in LVHA order: link, visited, hover, active.
βœ•
Applying list-style-type: none to the <li> instead of the <ul>. The list-style-type property is set on the list container (ul or ol), not on individual list items. nav ul { list-style-type: none; } removes all bullets from that list.
βœ•
Confusing text-decoration: none with list-style-type: none. text-decoration: none removes underlines from text (links). list-style-type: none removes bullet points from lists. They do very different things β€” both are needed for a clean nav bar.
βœ•
Forgetting that a:hover can change the background colour as well as text colour. A hover effect that only changes text colour is less visible. Changing the background-color creates a clear, unmissable highlight that significantly improves usability.
Exam tip

Pseudo-class questions at N5 often appear in two ways: (1) "Write CSS to style links so they change colour when hovered" β€” expect to write an a:hover rule with at least one property. (2) "Describe the effect of this CSS code" β€” read the pseudo-class and describe what triggers it.

For usability questions: "explain why changing background colour on hover is good for usability" β€” a strong 2-mark answer is: "It shows users that the link is interactive (1 mark) and makes it clear which link they are about to click, reducing the risk of clicking the wrong one (1 mark)."

Remember LVHA β€” it appears in exams as "put these link pseudo-classes in the correct order". The answer is always: link, visited, hover, active.

Task Set

Questions 1–3 are auto-checked. Questions 4–8 are self-marked β€” write your answer, then reveal the model answer to check your work.

1. Which pseudo-class applies when a user's mouse is over a link? TYPE 1

2. What does list-style-type: none do? TYPE 1

3. Which CSS property removes the underline from links? TYPE 1

4. A link has these three CSS rules. Predict its appearance: (a) before hovering; (b) when hovering; (c) after being clicked (visited). (3 marks) TYPE 2

a:link    { color: #185FA5; }
a:hover   { color: #ffffff; background-color: #042C53; }
a:visited { color: #9090b0; }
(a) Before hovering (a:link): The link text is blue (#185FA5). No background change. Default browser underline unless a:link also removes text-decoration.

(b) When hovering (a:hover): The link text turns white (#ffffff) and the background becomes dark navy (#042C53). This creates a strong highlight effect.

(c) After being clicked (a:visited): The link text turns grey (#9090b0) to indicate it has been visited.

1 mark per state correctly described (must match the CSS colour values).

5. Write CSS to style a navigation bar: remove underlines from nav links; change their colour to #185FA5; add a background-color of #E6F1FB on hover; remove bullet points from the nav ul. (3 marks) TYPE 2

nav ul {
  list-style-type: none;
  padding: 0;
}

nav a {
  text-decoration: none;
  color: #185FA5;
}

nav a:hover {
  background-color: #E6F1FB;
}
Marks: nav ul with list-style-type: none (1), nav a with text-decoration: none and correct colour (1), nav a:hover with correct background-color (1).

6. Explain why changing the background colour of a navigation link on hover is considered good for usability. (2 marks) TYPE 2

1. A background colour change on hover provides visual feedback to the user that the link is interactive and clickable (1 mark).

2. It makes it immediately clear which link the user is about to click, reducing the chance of clicking the wrong link β€” especially important on mobile-like layouts where links are close together (1 mark).

Accept any two distinct usability points. Strong answers reference: interactivity feedback, clarity of selection, reduced error rate.

7. Write a:hover and a:visited rules to give this appearance: visited links show in grey (#9090b0) with no underline; hovering turns the background to #E1F5EE and the text to #085041. (2 marks) TYPE 2

a:visited {
  color: #9090b0;
  text-decoration: none;
}

a:hover {
  background-color: #E1F5EE;
  color: #085041;
}
1 mark per rule (correct pseudo-class selector + correct properties and values).

8. A pupil writes a { text-decoration: none; } for a nav bar but the underlines still appear on the links. Suggest one reason why this might not be working. (1 mark) TYPE 2

Accept any one of the following:

1. The CSS rule may be overridden by a higher-specificity rule elsewhere in the stylesheet (e.g. a class selector or inline style targeting the same links).
2. The CSS file may not be linked correctly to the HTML page, so none of the stylesheet rules are being applied.
3. There may be a typo in the CSS property name β€” e.g. text-deceration instead of text-decoration β€” causing the browser to ignore the rule.
4. The CSS rule is written in an a:link or a:visited rule with a different value that overrides the base a rule (due to LVHA specificity issues).

1 mark for any valid, well-reasoned suggestion.
Teacher notes β€” Shift+T to hide

Suggested timing: Single 1-hour period. Lighter content than WDD8. Spend 15 minutes on the concept of pseudo-classes and LVHA order (open a real website and hover over nav links on the projector first β€” see the effect before explaining it). 10 minutes on now-you-try. 35 minutes on task set Q1–Q6 in class. Q7–Q8 as extension.

Opening demo: Before any CSS explanation, open a well-known website (BBC, SQA, etc.) on the projector. Hover slowly over the navigation links. Watch the highlight effect appear. Ask pupils: "What changed? Why does it do that?" This motivates the lesson better than a definition.

LVHA order: Write the mnemonic on the board β€” "Love Visits High Altitude" or simply "LVHA". The order matters because same-specificity rules follow the cascade (later wins), so :hover must come after :link and :visited to override them correctly.

Q4 (predict appearance) is an excellent exam-practice question. Pupils must read code and visualise the result β€” exactly what the SQA asks in the question paper.