I want to try and underline the text of a navbar but the underline animation doesn’t work.
this is my css for the underlining
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
display: block;
color: #dd5527;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
.navbar a:after {
content: "";
position: absolute;
background-color: #dd5527;
height: 3px;
width: 0;
left: 0;
bottom: -10px;
transition: 0.3s;
}
.navbar a:hover::after {
width: 100%;
}
this is my html for the navbar
<div class="navbar">
<a class="nav-link nav-link-ltr" href="#">Home</a>
<a class="nav-link nav-link-ltr" href="#">About Us</a>
<a class="nav-link nav-link-ltr" href="#">Services</a>
</div>
2
Answers
It is working: your element is just rendered at the wrong place. You need to:
position: relative
to the<a>
element, and0
or a positive pixel value for the pseudo-element’sbottom
property so that it is not cut-off byoverflow: hidden
set on the parent containerUse the
transform:scale(0)
to set the initial scale and then settransform-origin:left;
to transform from left to right. then use thetransition:transform 350ms ease-in;
to get the transition animation and finally set thetransform:scale(1)
ona:hover::after
to scale back to original size. I have also set some styling for the underline and a tag. I would suggest you usedisplay:flex;
on navbar instead offloat:left
on a tag.