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: But I want it to look like this:
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:
Then I added display: inline-block;
for .over
. But I got this:
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?
To control the width, you just change the width from 100% to whatever value you wish.
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.
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 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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With