Thick underline when hover

Tiffany picture Tiffany · Jul 9, 2014 · Viewed 28.2k times · Source

I'm trying to create a thick underline when the mouse hover over the word (For Navigation Bar). But I noticed that the underline is much longer than the word & there's huge gap between the word and the underline. How can make the underline the same length as the word (similar to text-decoration: underline) and also a small gap.

Below is my code and an image of the outcome.

nav > ul > li:hover > a { 
border-bottom: 2px solid #F117B4;
text-decoration: none; 
}

http://i735.photobucket.com/albums/ww358/roundeworld/Sale_zps6c9e169b.jpg

This is the whole code for the navigation bar.

nav { font-family: Open Sans Condensed; letter-spacing: 1px; }

nav { position: relative; border-bottom: 1px {{ settings.border_style }} {{ settings.border_color }}; /*border-top: 1px {{ settings.border_style }} {{ settings.border_color }};*/ }

nav > ul > li {  margin-bottom: 0; }

nav > ul > li > a { text-decoration: none; color: {{ settings.nav_link_color }}; display: block; padding: 0 15px; font-size: 17px/*font-size: {{ settings.nav_font_size }}*/; 
line-height: {{ settings.nav_line_height }}; height: {{ settings.nav_line_height }}; font-weight: {{ settings.nav_weight }}; text-transform: {{ settings.nav_font_style }}; }

nav > ul > li > a.current {  color: {{ settings.selected_nav_link_color }}; }

nav > ul > li:hover > a { display:inline; border-bottom: 2px solid #F117B4; color: {{ settings.nav_link_hover_color }}; text-decoration: none; }

nav > ul > li.dropdown { position:relative; }

nav > ul > li.dropdown > .dropdown { background: {{ settings.nav_dropdown_background_color }}; list-style: none outside none; padding: 5px 15px; display: none; position: absolute; min-width: 180px; z-index: 99999; top: 100%; left: 0%; margin-left: 15px; border: 1px solid {{ settings.border_color }}; }

nav > ul > li.dropdown:hover > .dropdown { display: block; z-index: 999999; -webkit-box-shadow: 0 0 3px rgba(0, 0, 0, 0.10); -moz-box-shadow: 0 0 3px rgba(0, 0, 0, 0.10); box-shadow:0 0 3px rgba(0, 0, 0, 0.10); }

nav > ul > li.dropdown li { list-style: none; font-size: 13px; line-height: 30px; }

Hi, anyone has a solution for this? :( Please help me. Thanks.

Answer

Himanshu Tyagi picture Himanshu Tyagi · Jul 9, 2014

UPDATE

  1. Underline exactly as the size of word.
  2. Distance of underline and word reduced. Achieved using pseudo-element.

Here's the jsfiddle : http://jsfiddle.net/e79gV/1/

css

  nav > ul > li > a {
    text-decoration: none;
}

.border{
    position:relative;
}
.border:hover::after{
    content:'';
    position:absolute;
    width: 100%;
    height: 0;    
    left:0;
    bottom:0px; /*Change this to increase/decrease distance*/
    border-bottom: 2px solid #000;  
}

html

 <nav>
    <ul>
        <li><a class="border" href="#">M</a>

        </li>
        <li><a class="border" href="#">Menu</a>

        </li>
        <li><a class="border" href="#">Menu Item</a>

        </li>
    </ul>
</nav>

Underlines are exactly of the same length as word.

Some Refs that might help :

https://stackoverflow.com/a/12804469/3603806

Changing Underline color