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:hoveranda:visitedrules 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
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
a:hover.a:hovera:visiteda:activea:link<nav> element, allowing users to move between pages. Typically created by styling an unordered list of anchor elements.text-decorationtext-decoration: none to remove the browser's default underline from links.list-style-typelist-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. LVHA is a safe conventional source order when equal-specificity link-state rules set overlapping properties. :link and :visited are mutually exclusive, so their relative order usually does not matter; placing :hover after them, and :active after :hover, lets those interaction states override earlier declarations through the cascade when required.
| Pseudo-class | When it applies | Typical browser convention |
|---|---|---|
a:link | Unvisited link β user has not clicked it yet | Typically blue and underlined |
a:visited | Visited link β user has clicked it before | Typically purple and underlined |
a:hover | User's mouse is currently over the link; this can overlap with :link or :visited | No universal hover colour; it changes only if a matching style applies |
a:active | Link is being clicked (mouse button held down); this can overlap with a base and hover state | Often red in conventional browser styles |
Browsers add their own default styles to links, and user styles can alter them. The colours above are common conventions rather than guaranteed values. Web designers often override them using CSS to match their site's design.
A link is either unvisited or visited; hover and active are interaction states that can overlap either base state
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, light 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. LVHA is a safe conventional order when equal-specificity rules set the same properties: later matching rules win through source order in the cascade. :link and :visited cannot apply together, while :hover should come after them, and :active after :hover, when you want those interaction states to override earlier declarations:
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
Compare unstyled nav links with styled nav links. The HTML structure is the same β only the CSS differs.
<ul> and <nav>.<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: Typically blue underlined links with bullet points, depending on the browser or user styles.
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; }
Write CSS to style links in all four states: unvisited = #185FA5 (blue); hover = #042C53 text on #E6F1FB background; visited = #9090b0 (grey); active = #1D9E75 (green).
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; }
Write CSS to style a <nav> containing a <ul> of links. Remove bullet points, remove underlines, display items horizontally, and add a hover background.
nav ul to remove bullets and default list padding.nav a to remove underlines and set the link colour.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; }
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 alinks appear dark blue (#042C53) with no underline - Add a light blue background (
#E6F1FB) onnav a:hover
nav ul { list-style-type: none; padding: 0; } nav a { color: #042C53; text-decoration: none; } nav a:hover { background-color: #E6F1FB; }
:hover after the base :link/:visited rules, and :active after :hover, when you want the interaction styles to win.list-style-type: none on every <li>. The property can be applied directly to each list item, but it is simpler to set it once on the parent <ul> or <ol>, because the value is inherited by its list items. nav ul { list-style-type: none; } removes all bullets from that list.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.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.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)."
Use LVHA β link, visited, hover, active β as a helpful coding mnemonic and safe conventional source order when equal-specificity state rules contain overlapping declarations.
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) while unvisited and not hovered; (b) while unvisited and hovered; (c) after it has been visited, both when not hovered and when hovered. (3 marks) TYPE 2
a:link { color: #185FA5; } a:hover { color: #ffffff; background-color: #042C53; } a:visited { color: #9090b0; }
a:link matches, so the link text is blue (#185FA5). There is no background declaration here, and the browser's usual underline remains unless another rule removes it.(b) Unvisited and hovered: Both
a:link and a:hover match. The later hover rule wins for color, so the text is white (#ffffff), and its background is dark navy (#042C53).(c) Visited: When not hovered, the text is grey (#9090b0). When the visited link is also hovered, both
a:hover and a:visited match. The later a:visited rule wins for color, so the text stays grey, while the dark navy hover background still applies because a:visited does not set a background colour.1 mark per part correctly described using source order and the cascade.
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
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
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. A matching
a:link or a:visited rule sets text-decoration to a different value. A state pseudo-class selector is more specific than the base a selector, so it overrides the base rule.1 mark for any valid, well-reasoned suggestion.
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". Explain that this is a safe convention for equal-specificity rules with overlapping declarations: :link and :visited are mutually exclusive, while :hover comes after them and :active after :hover when those interaction states should win through source order.
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.