Styling Links with CSS: Hover, Active, and Visited States

In CSS, links can be styled to look visually appealing and interactive. CSS allows you to change the appearance of links in different states such as normal, hover, active, and visited. By customizing the look of links in these states, you can improve user experience and give clear feedback when users interact with links. In this guide, we will explore how to style links using CSS pseudo-classes like :hover, :active, and :visited.


1. The Basics of Styling Links

By default, a link (<a> element) in HTML looks like underlined blue text. However, CSS allows you to style it however you like. Links typically go through various states:

  • Normal: The default appearance of a link.
  • Hover: When the user moves the cursor over the link.
  • Active: When the link is being clicked.
  • Visited: After the link has been clicked by the user and leads to a visited URL.

Basic CSS Link Styling

You can style links using CSS just like any other HTML element. Here is an example of basic link styling:

a {
  color: blue;
  text-decoration: none;
}

This makes all links (<a> elements) blue with no underline.


2. Hover State (:hover)

What is the Hover State?

The hover state occurs when the user places the cursor over the link. You can use the :hover pseudo-class to change the appearance of the link when it is hovered over. This state is typically used to provide visual feedback, indicating that the link is interactive.

Syntax:

a:hover {
  /* CSS properties */
}

Example:

a {
  color: blue;
  text-decoration: none;
}

a:hover {
  color: red;
  text-decoration: underline;
}

In this example, the link will change to red and underline when the user hovers over it.

Pros of Hover Effects:

  • Provides clear feedback to the user that the link is interactive.
  • Enhances the user experience by adding visual cues.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Link Hover Example</title>
  <style>
    a {
      color: blue;
      text-decoration: none;
    }

    a:hover {
      color: red;
      text-decoration: underline;
    }
  </style>
</head>
<body>
  <p>This is a <a href="#">link</a> with hover effect.</p>
</body>
</html>

In this example, the link will change to red and become underlined when the user hovers over it.


3. Active State (:active)

What is the Active State?

The active state applies when the link is being clicked, but before the user releases the mouse button or touch input. You can use the :active pseudo-class to style the link during this brief interaction. This gives immediate feedback during the clicking action.

Syntax:

a:active {
  /* CSS properties */
}

Example:

a {
  color: blue;
}

a:active {
  color: green;
  text-decoration: underline;
}

In this example, when the user clicks on the link, it will turn green and underline while it’s being clicked.

Why Use the Active State?

The active state adds visual feedback during the click, making the link feel more responsive and interactive. This feedback helps assure users that their click is being registered.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Link Active State Example</title>
  <style>
    a {
      color: blue;
      text-decoration: none;
    }

    a:hover {
      color: red;
    }

    a:active {
      color: green;
    }
  </style>
</head>
<body>
  <p>Click this <a href="#">link</a> to see the active state.</p>
</body>
</html>

In this example, the link changes to green while it’s being clicked.


4. Visited State (:visited)

What is the Visited State?

The visited state applies to links that the user has previously clicked on and visited. Once a link has been visited, it is styled differently to indicate to the user that they have already interacted with that link.

Syntax:

a:visited {
  /* CSS properties */
}

Example:

a {
  color: blue;
  text-decoration: none;
}

a:visited {
  color: purple;
}

In this example, links that have already been visited by the user will turn purple.

Why Use the Visited State?

The visited state is useful for providing feedback to users, helping them keep track of which links they have already clicked on. This is particularly helpful on content-heavy websites like blogs or e-commerce sites where users may browse through many links.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Link Visited Example</title>
  <style>
    a {
      color: blue;
      text-decoration: none;
    }

    a:visited {
      color: purple;
    }
  </style>
</head>
<body>
  <p>Visit this <a href="https://www.example.com" target="_blank">link</a> and come back to see the visited state.</p>
</body>
</html>

In this example, the link will turn purple after it has been visited.


5. Link Styling Best Practices

When styling links, it’s important to maintain consistency and usability. Here are a few best practices:

  1. Ensure Link Visibility: Links should stand out from regular text. Use contrasting colors or underline the text to make it clear that the text is a link.
  2. Provide Clear Feedback: Make sure the hover and active states provide clear visual feedback, such as color changes or underlines, to indicate that the link is clickable.
  3. Use Accessible Colors: Ensure that the colors used for links and visited states have enough contrast so that they are readable for all users, including those with visual impairments.
  4. Order of Link Styles: When styling links, the order of pseudo-classes is important. You should follow this specific order: a:link, a:visited, a:hover, and a:active (also known as the LoVe HAte rule). This ensures that styles are applied correctly in all states.

Example of Correct Order:

a:link {
  color: blue;
}

a:visited {
  color: purple;
}

a:hover {
  color: red;
  text-decoration: underline;
}

a:active {
  color: green;
}

6. Example Combining All Link States

Here’s a complete example that demonstrates how to style a link in all states: normal, hover, active, and visited.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Styling Links Example</title>
  <style>
    /* Normal state (link) */
    a:link {
      color: blue;
      text-decoration: none;
    }

    /* Visited state */
    a:visited {
      color: purple;
    }

    /* Hover state */
    a:hover {
      color: red;
      text-decoration: underline;
    }

    /* Active state */
    a:active {
      color: green;
    }
  </style>
</head>
<body>
  <p>
    This is a <a href="https://www.example.com" target="_blank">link</a> that demonstrates link styling in various states.
  </p>
</body>
</html>

Explanation:

  • The normal state shows the link in blue without an underline.
  • When the link has been visited, it turns purple.
  • On hover, the link turns red and is underlined.
  • While the link is active (being clicked), it turns green.

Conclusion

Styling links with CSS can significantly improve the usability and design of your website. By applying different styles to links in their various states—hover, active, and visited—you provide clear feedback to users, enhance their interaction with your site, and improve navigation.