Smithy Smithy - 17 days ago 6
CSS Question

Add a space between underline and a nav item on hover

I would like the underline effect that appears on hover to have a padding/margin of app. 5px from the text, just to have some space between.

Here's the code and link to Codepen:

http://codepen.io/anon/pen/MbmBjp

Thank you!



* {
background-color: blue;
}

nav ul li {
display: inline-block;
padding-left: 35px;
font-size: 12px;
font-weight: bold;
}


/*Underline animation*/

li > a {
position: relative;
color: white;
text-decoration: none;
}

li > a:hover {
color: yellow;
}

li > a:before {
content: "";
position: absolute;
width: 100%;
height: 1px;
bottom: 0;
background-color: yellow;
visibility: hidden;
transform: scaleX(0);
transition: all 0.1s ease-in-out 0s;
}

li > a:hover:before {
visibility: visible;
transform: scaleX(1);
}


<nav class="menu" id="menu">
<ul>
<li><a href="#">ABOUT</a></li>
<li><a href="#">SERVICE</a></li>
<li><a href="#">WORK</a></li>
<li><a href="#">BLOG</a></li>
<li><a href="#">CONTACT</a></li>
</ul>
</nav>




Answer

Variant #01:

Increase or decrease bottom value in the following selector:

li > a:before {
    bottom: -5px;  /* Change in this value will increase or decrease gap */
}

* {
  background-color: blue;
}

nav ul li {
  display: inline-block;
  padding-left: 35px;
  font-size: 12px;
  font-weight: bold;
}


/*Underline animation*/

li > a {
  position: relative;
  color: white;
  text-decoration: none;
}

li > a:hover {
  color: yellow;
}

li > a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 1px;
  bottom: -5px;
  background-color: yellow;
  visibility: hidden;
  transform: scaleX(0);
  transition: all 0.1s ease-in-out 0s;
}

li > a:hover:before {
  visibility: visible;
  transform: scaleX(1);
}
<nav class="menu" id="menu">
  <ul>
    <li><a href="#">ABOUT</a></li>
    <li><a href="#">SERVICE</a></li>
    <li><a href="#">WORK</a></li>
    <li><a href="#">BLOG</a></li>
    <li><a href="#">CONTACT</a></li>
  </ul>
</nav>

Variant #02:

Set following css on <a>:

li > a {
    display: inline-block;
    vertical-align: top;
    padding-bottom: 5px; /* Change in padding-bottom value will increase or decrease gap */
}

* {
  background-color: blue;
}

nav ul li {
  display: inline-block;
  padding-left: 35px;
  font-size: 12px;
  font-weight: bold;
}


/*Underline animation*/

li > a {
  display: inline-block;
  vertical-align: top;
  padding-bottom: 5px;
  position: relative;
  color: white;
  text-decoration: none;
}

li > a:hover {
  color: yellow;
}

li > a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 1px;
  bottom: 0;
  background-color: yellow;
  visibility: hidden;
  transform: scaleX(0);
  transition: all 0.1s ease-in-out 0s;
}

li > a:hover:before {
  visibility: visible;
  transform: scaleX(1);
}
<nav class="menu" id="menu">
  <ul>
    <li><a href="#">ABOUT</a></li>
    <li><a href="#">SERVICE</a></li>
    <li><a href="#">WORK</a></li>
    <li><a href="#">BLOG</a></li>
    <li><a href="#">CONTACT</a></li>
  </ul>
</nav>