Creating an underline animation for navbar links in CSS using transitions involves using the transition property to smoothly animate the width or other properties of a pseudo-element, such as ::after or ::before, that represents the underline effect. Here's an overview of the steps involved:
Here's a step-by-step overview of how you can create an underline animation for navbar links using CSS:
<div class="container"> <ul> <li><a href="#">HOME</a></li> <li><a href="#">PAGE</a></li> <li><a href="#">ABOUT US</a></li> <li><a href="#">CONTACT US</a></li> </ul> </div>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap'); *{ font-family: 'Poppins', sans-serif; margin: 0; padding: 0; box-sizing: border-box; } a{ text-decoration: none; color:#333; font-weight: 500; } body{ height: 100vh; width: 100vw; background-color: #dfe4ea; display: flex; justify-content: center; align-items: center; } ul li{ list-style-type: none; display: inline-block; margin-right: 10px; padding-bottom:3px ; }
li:after{ content: ''; margin: auto; display: block; height: 2px; width: 0%; background-color: transparent; /* transition: width 0.5s ease,background-color 0.5s ease; */ transition: all 0.5s ease; }
li:hover:after{ width: 100%; background-color: red; }
With the above steps, you can create an animated underline effect for navbar links using CSS transitions. When users hover over the links, the width of the pseudo-element representing the underline will smoothly transition from 0 to the specified width, creating a smooth and visually appealing animation effect for your navbar links.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap'); *{ font-family: 'Poppins', sans-serif; margin: 0; padding: 0; box-sizing: border-box; } a{ text-decoration: none; color:#333; font-weight: 500; } body{ height: 100vh; width: 100vw; background-color: #dfe4ea; display: flex; justify-content: center; align-items: center; } ul li{ list-style-type: none; display: inline-block; margin-right: 10px; padding-bottom:3px ; } li:after{ content: ''; margin: auto; display: block; height: 2px; width: 0%; background-color: transparent; /* transition: width 0.5s ease,background-color 0.5s ease; */ transition: all 0.5s ease; } li:hover:after{ width: 100%; background-color: red; } </style> </head> <body> <div class="container"> <ul> <li><a href="#">HOME</a></li> <li><a href="#">PAGE</a></li> <li><a href="#">ABOUT US</a></li> <li><a href="#">CONTACT US</a></li> </ul> </div> </body> </html>Live Preview
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions