search

Home  >  Q&A  >  body text

Create animated text underline: a step-by-step guide

I'm trying to make the <a> label element animate in underline on hover, similar to the first example on this page. It almost feels stupid to ask this question because the tutorial is there but what I'm doing isn't working and I don't know why.

This is my CSS content


#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;;
}

P粉366946380P粉366946380294 days ago366

reply all(1)I'll reply

  • P粉333186285

    P粉3331862852024-02-04 00:56:03

    I noticed some missing properties:

    1. content: '' on a::after is missing. For pseudo-elements, the content attribute can be empty "", but must be specified.
    2. position:relative needs to be on a because pseudo-element a::after uses position:relative.
    3. a::after is using background-color:inherit but there doesn't seem to be any value that can be inherited. I'm currently giving it a value of hotpink, but this can be customized to any color.
    4. I added cursor:pointer on a so it better matches your desired result.

    Hope this helps!

    Example: (see live demo with buttons 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;
    }

    reply
    0
  • Cancelreply