How do I style an active link using CSS?
Ava W
ava w profile pic

To style an active link using CSS, you can use the:active pseudo-class selector. This selector targets elements while they are being activated, typically when a user clicks on them. Here's a step-by-step guide on how to achieve this: 1. HTML Markup: Start by creating the HTML markup for the links. For example, let's consider a navigation menu with multiple links:

1
2
3
4
5
6
7
8
9
  <nav>
     <ul>
       <li><a href="#">Home</a></li>
       <li><a href="#">About</a></li>
       <li><a href="#">Services</a></li>
       <!-- Add more links as needed -->
     </ul>
   </nav>
   

In this example, we have a navigation menu with an unordered list (

1
2
3
4
5
6
7
8
 nav ul li a {
     /* Styles for the links in their normal state */
   }

   nav ul li a:active {
     /* Styles for the active link */
   }
   
1
2
3
4
5
6
 nav ul li a:active {
     background-color: #f0f0f0;
     color: #ff0000;
     text-decoration: underline;
   }