Menus in HTML are typically created using unordered lists (<ul>
) or ordered lists (<ol>
) with list items (<li>
). Each list item can contain a link (<a>
) to a different page or section of the website.
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
The :focus
pseudo - class in CSS is used to select an element that has received focus. Focus can be achieved by tabbing through elements using the keyboard or by clicking on an element. When an element is in focus, we can apply specific styles to it.
a:focus {
outline: 2px solid blue;
}
In the above example, when a link (<a>
) receives focus, it will have a 2px blue outline.
We can style the focused menu item by targeting the <a>
elements inside the menu’s list items.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 10px;
}
nav ul li a {
text-decoration: none;
color: black;
}
nav ul li a:focus {
background-color: yellow;
color: red;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
</html>
In this example, when a menu item link is focused, its background color changes to yellow and the text color changes to red.
We can also use JavaScript to add a class to the focused menu item and then style that class in CSS.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 10px;
}
nav ul li a {
text-decoration: none;
color: black;
}
.focused {
border-bottom: 2px solid green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<script>
const menuItems = document.querySelectorAll('nav ul li a');
menuItems.forEach(item => {
item.addEventListener('focus', () => {
item.classList.add('focused');
});
item.addEventListener('blur', () => {
item.classList.remove('focused');
});
});
</script>
</body>
</html>
In this code, when a menu item is focused, the focused
class is added to it, which adds a green bottom border. When the focus is removed, the class is removed.
Changing the background color, text color, or both is a common way to highlight a focused menu item. This makes it easy for the user to visually identify which item is currently in focus.
Adding a border to the focused menu item can also make it stand out. It can be a simple solid border or a more decorative one.
nav ul li a:focus {
border: 1px dashed purple;
}
Using CSS animations to animate the appearance of the focused state can add a more engaging effect.
nav ul li a {
transition: all 0.3s ease;
}
nav ul li a:focus {
transform: scale(1.1);
}
In this example, when a menu item is focused, it will scale up by 10% over 0.3 seconds.
Highlighting the focused menu item using CSS is an essential part of creating a user - friendly website. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can create menus that are both visually appealing and accessible. Whether using basic styling, classes, or animations, the goal is to make the focused menu item stand out clearly to enhance the user experience.