A navigation bar is an essential element of any website, providing users with a way to navigate to different pages. While a basic navigation bar can be functional, it can also be visually unappealing. One way to make your navigation bar more visually appealing is to add a hover effect.
A hover effect is a visual change that occurs when the user hovers over an element with their cursor. This can be anything from a simple change in color to a more complex animation.
First We create a project folder structure. We name the project folder – ”Nav”. Within this folder, we have 3 files. These files are:
- Nav.html
- Nav.css
HTML:
We begin with the HTML code. Copy the code below and paste it into your HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="Nav.css">
</head>
<body>
<nav>
<ul>
<li>
<a href="#">HOME</a>
</li>
<li>
<a href="#">SERVICES</a>
</li>
<li>
<a href="#">NEWS</a>
</li>
<li>
<a href="#">CONTACT</a>
</li>
<li>
<a href="#">ABOUT</a>
</li>
</ul>
</nav>
</body>
</html>
Code language: HTML, XML (xml)
CSS:
Next, we style our HTML using CSS. For this copy, the code provided to you below and paste it into your stylesheet.
body {
background-color: #5e0017;
padding: 0;
margin: 0;
height: 100vh;
width: 100vw;
display: flex;
align-items: center;
}
nav {
width: 100vw;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-around;
}
li {
position: relative;
padding: 0 20px;
height: 100%;
display: flex;
}
a {
z-index: 1;
color: white;
text-decoration: none;
font-family: 'Poppins',sans-serif;
font-weight: 500;
font-size: 18px;
padding: 10px 5px;
}
li::before {
content: "";
position: absolute;
height: 33.33%;
width: 0;
background-color: #0D7CFF;
right: 0;
z-index: 0;
top: 33.33%;
transition: all 0.5s;
}
li::after {
content: "";
position: absolute;
height: 33.33%;
width: 0;
background-color: #0D7CFF;
left: 0;
z-index: 0;
bottom: 0;
transition: all 0.5s;
}
a::before {
position: absolute;
content: "";
height: 33.33%;
width: 0;
background-color: #0D7CFF;
bottom: 66.66%;
left: 0;
transition: all 0.5s;
}
li:hover:before,
li:hover::after,
li:hover a::before{
width: 100%;
}
Code language: CSS (css)
Our Navigation Bar Hover Effect is now ready. You can go ahead and change Class Text to suit your likes and need.