skip to Main Content
    <div className={styles.container}>
  <ul className={styles.links}>
    <li className={styles.link}>
      <a href="/">Home</a>
    </li>
    <li className={styles.link}>
      <a href="/portfolio">Portfolio</a>
    </li>
    <li className={styles.link}>
      <a href="/skills">Skills</a>
    </li>
    <li className={styles.link}>
      <a href="/contact">Contact</a>
    </li>
  </ul>
</div>

    .link a {
  position: relative;
  display: inline;
}

.link a:hover {
  transition: all 0.3s ease-in-out;
  color: #f8b513;
}

.link a::after {
  content: "";
  position: absolute;
  width: 0;
  height: 2px;
  left: 50%; 
  bottom: -3px;
  transform: translateX(-50%) scaleX(0);
  transform-origin: 50% 50%; 
  transition: transform 0.3s ease, width 0.3s ease;
}

.link a:hover::after {
  width: 100%;
  transform: translateX(-50%) scaleX(1); 
}

What I’m trying to do is to make the underline the same size as the text with the effect applied when hover effect is applied on the links.

enter image description here

enter image description here

As seen in the images, the length of the line formed under all of them is 100%. However, I want the line under it to be as long as the word Portfolio, and I want the line under it to be as long as the word home.

2

Answers


  1. It will be solved your problem.
    width: min-content;

    a:hover {
          border-bottom: 1px solid red;
          width: min-content;
        }
    
    Login or Signup to reply.
  2. One option is to wrap the text in a span and then use the pseudo-element on that. .link a span::after

    ul {
      list-style: none;
      display: flex;
      margin: 0;
    }
    
    li {
      margin: 0 1em;
    }
    
    a {
      text-decoration: none;
      color: red;
    }
    
    span {
      position: relative;
      ;
    }
    
    span:after {
      content: "";
      position: absolute;
      bottom: -3px;
      left: 0;
      width: 0%;
      height: 5px;
      background: #000;
      transition: width .33s ease;
    }
    
    a:hover span:after {
      width: 100%;
    }
    <ul>
      <li><a href="/"><span>Lorem.</span></a></li>
      <li><a href="/"><span>Lorem, ipsum.</span></a></li>
      <li><a href="/"><span>Item 3</span></a></li>
      <li><a href="/"><span>Item 4</span></a></li>
      <li><a href="/"><span>Item 5</span></a></li>
    </ul>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search