Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: correct way to make thick <a:hover> underline for fonts with big descenders

I'm a CSS-beginner. Basically I have the following html:

<ul>
<li><a href="news.html">О нас</a></li>
<li><a href="#">Галерея</a></li>
</ul>

I want to have a thick underline when hovering my a tags, but I use a custom font with big descenders, so if I use the common trick for this:

a:hover {
text-decoration: none;
border-bottom: 2px solid;
padding: 0;
margin: 0;
}

The underline is far below the base line:enter image description here But I want it to look like this: enter image description here

I tried to do it like this:

<ul>
    <li class="over"><a href="news.html">О нас</a></li>
    <li class="over"><a href="#">Галерея</a></li>
</ul>

.over{
    font-size: 30px;
    height:30px; // makes the text overlap this element
    overflow:visible;
}
.over:hover  {
    border-bottom: 2px solid #ec6713;
}

But the width of the underline is the same for all the strings now: enter image description here

Then I added display: inline-block; for .over. But I got this: enter image description here

Then I changed inline-block to table, but the underline is again far below:

I ended up adding an extra span, so now I have:

<ul>
    <li><span class="over"><a href="news.html">О нас</a></span></li>
    <li><span class="over"><a href="#">Галерея</a></span></li>
</ul>

.over{
    font-size: 30px;
    height:30px; // makes the text overlap this element
    overflow:visible;
    display:inline-block;
}
.over:hover  {
    border-bottom: 2px solid #ec6713;
}

And this gives me finally the desired behaviour (the underline width is adjusted to the string width, and it's positioned close to the baseline). But is it a good practice to add an extra span for this purpose? Doesn't it look hacky?

like image 404
netimen Avatar asked Nov 25 '13 07:11

netimen


People also ask

How do I increase the thickness of an underline in CSS?

To control the width, you just change the width from 100% to whatever value you wish.

How do you make text underlined in hover?

Using HTML, CSS create an animated underline effect when the user hovers over the text. Use display: inline-block to make the underline span just the width of the text content. Use the :after pseudo-element with width: 100% and position: absolute to place it below the content.

How do I increase the gap between text and underline?

You should first select the underlined space before the texts, and go to the “Font” dialog box again. Choose “Condensed” under “Spacing” options. And in the “By” box, enter a specific number you want.

How do I style an underline in CSS?

How to Underline a Title in CSS. To underline a title, you can use text-decoration: underline; but you can make it prettier if you use the border-bottom property. In the latter case, however, you need to add display: inline; so that the underline wouldn't be longer than the word itself.


1 Answers

A span is a meaningless tag, so it won't give extra 'weight' to your code. Therefor, imho, it is okay to use it (but better to avoid).

Alternatively you could do the following:

<ul>
    <li><a href="news.html">О нас</a></li>
    <li><a href="#">Галерея</a></li>
</ul>
a {
    font-size: 30px;
    text-decoration: none;
    position: relative;
}
a:hover:after {
    content: "";
    border-bottom: 2px solid #ec6713;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 3px;
}

And a DEMO.

Please note that the :after is overlapping the a. I've tried adding a z-index, but that didn't fix it.

OPTION 2

Add a background-image to your a.

like image 106
LinkinTED Avatar answered Nov 09 '22 23:11

LinkinTED