I'm trying to make an <a>
tag element animates in an underline on hover, similar to the first example on this page. Asking this almost feels stupid since the tutorial is RIGHT THERE, but something I'm doing isn't working and I'm not sure why.
Here's what I've got in my CSS
#about-text-box a {
text-decoration: none;
font-size: 17pt;
font-family: 'Silkscreen', cursive;
color: #ECE0E7;
text-align: center;
width: 95%;
text-shadow: 2px 2px 2px #101400;
transition: 0.5s;
}
#about-text-box a:hover::after, #about-text-box a:focus::after {
opacity: 1;
transform: translate3d(0, 0.2em, 0);
}
#about-text-box a::after {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0.1em;
background-color: inherit;
opacity: 0;
transition: opacity 300ms, transform 300ms;;
}
CodePudding user response:
A few missing properties I noticed:
content: ''
ona::after
is missing. For a pseudo-element, thecontent
property can be empty""
, but must be specified.position: relative
need to be ona
because pseudo-elementa::after
is usingposition: relative
.a::after
is usingbackground-color: inherit
but does not seems to have any value that can be inherited. I gave it a valuehotpink
for now, but this can be customized to any color.- I added
cursor: pointer
ona
so it is more matching your desired result.
Hope this will help!
Example: (see live demo with button below)
#about-text-box {
display: flex;
}
#about-text-box a {
display: block;
position: relative;
padding: 0.2em 0;
cursor: pointer;
text-decoration: none;
font-size: 17pt;
font-family: "Silkscreen", cursive;
color: #ece0e7;
text-align: center;
text-shadow: 2px 2px 2px #101400;
transition: 0.5s;
}
#about-text-box a:hover::after,
#about-text-box a:focus::after {
opacity: 1;
transform: translate3d(0, 0.2em, 0);
}
#about-text-box a::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0.1em;
background-color: hotpink;
opacity: 0;
transition: opacity 300ms, transform 300ms;
}
<div id="about-text-box">
<a>Hover this link</a>
</div>