:visited

Term side of the Card.
Topic: Pseudo Classes
Unit title: :visited
Categories: Pseudo-class
Explanation side of the Card

Explanation: The pseudo-class that styles links that have been visited by the user. It allows for different visual cues for links that have been clicked before, such as changing the color to indicate the link's history.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
a:visited {
  font-weight: normal;
  opacity: 0.6;
}
Rightcss
a:visited {
  color: #6b3fa0;
}

Browsers allow only color changes on :visited, so sites can't detect browsing history through layout.

Common mistakes

  • Setting display, font-weight or a background image in :visited and seeing no change.
  • Using a transparent color in :visited, while the alpha part is ignored for privacy.

Key points

  • :visited rules only change colors, like color, background-color and border-color.
  • Code does not read :visited styles with getComputedStyle(), which reports unvisited values.
  • Visited links keep enough contrast against the background, like other text.

Interview questions

Why is :visited limited to colors?

To protect privacy. If layout could change, scripts could measure it and learn which sites the user has visited.

Can JavaScript detect visited links?

No. getComputedStyle() returns the unvisited style, so the visited state can't be read.

Where should :visited go in my link styles?

After :link and before :hover and :active, so hover still works on visited links.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy