I was following a youtube tutorial, in which a CSS hover effect was used so that when you bring you cursor on the navbar items, a line emerged gradually from the center. How does the whole :hover::after thing work?
This is the CSS code they showed in the video :
nav{
display: flex;
padding: 2% 6%;
justify-content: space-between;
align-items: center;
position: sticky;
}
nav img{
width: 150px;
}
.nav-links{
flex: 1;
text-align: right;
gap: 2rem;
}
.nav-links ul li{
list-style: none;
display: inline-block;
padding: 8px 12px;
position: relative;
}
.nav-links ul li a{
color: #ffffff;
font-size: 13px;
text-decoration: none;
}
.nav-links ul li a::after{
content: '';
width: 0%;
height: 2px;
background: #f44336;
display: block;
margin: auto;
transition: 0.5s;
}
.nav-links ul li a:hover::after{
width: 100%;
}```
CodePudding user response:
When hovering on the element in your case <a> tag, you apply a style on the ::after pseudo-element. In the example code I see that you give the pseudo-element a with of 100% and that way you change the initial value of the width witch is 0.
CodePudding user response:
::after and :hover are pseudo-elements. If you have a:hover, the following styles will apply on the element when hovering over it. But, when you have a:hover::after, the following styles will apply on the pseudo-element ::after, when hovering over the element. It this case, the ::after pseudo-element represents the expanding line. And by the way to make it clear what is ::after, in CSS using the content attribute, you can also define the HTML content to be shown in the ::after pseudo-element, but in this case, you don't need to put any content there, it's just an empty shape.
