Mehdi Loukili Mehdi Loukili - 2 months ago 7
CSS Question

Line under navbar when hovering

I searched alot about this and couldn't find , how can I make something similar to the navbar here , when you hover on it a line under it will appear like a loading bar .
- example : http://www.thecrewgaming.com/
is it possible only with css or javascript is needed and how ?
thanks !

I tried this

li:hover {
background-color: rgba(255, 245, 255, 0.2);
border-bottom-color: #abcdef;
}
li {
font-family: 'Poppins', sans-serif;
font-weight: bold;
color: #F5F5F5;
font-size: 0.8889em;
text-decoration: none;
font-weight: normal;
text-transform: uppercase;
border-bottom: 3px solid transparent;
padding-bottom: 0.125em;``
transition: border-bottom-color 50ms linear 0s;
}


but it appears as a normal line and not like a loading bar in the page linked above .

Answer

It's quite simple.

You use a position:absolute; property on a ::after pseudo selector.

See the fiddle and the code below:

https://jsfiddle.net/cuv6bxs5/

li {
  font-family: 'Poppins', sans-serif;
  font-weight: bold;
  color: red;
  background-color: #404040;
  float: left;
  position: relative;
  padding: 10px 20px;
  overflow: hidden;
}

li::after {
  background-color: red;
  content: "";
  width: 0;
  height: 3px;
  left: 0;
  bottom: 0;
  transition: width 0.35s ease 0s;
  position: absolute;
}

li:hover::after {
  width: 100%;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
<ul>
  <li>ONE</li>
  <li>TWO</li>
  <li>THREE</li>
  <li>FOUR</li>
</ul>