Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make text-decoration: underline with 2px padding?

Tags:

html

css

I like an obedient frotend developer must create underline with 2px padding instead of 1px by default. Is exist simple solution for this?

PS Yeahh guys, I know about div with black backgrond color and 1px * Npx and position: relative, but It's so slowly...

like image 927
NiLL Avatar asked Nov 07 '11 14:11

NiLL


4 Answers

You could wrap the text in a span and give it a border-bottom with padding-bottom:2px;.

Like so

span{
    display:inline-block;
    border-bottom:1px solid black;
    padding-bottom:2px;
}

Example: http://jsfiddle.net/uSMGU/

like image 96
Jason Gennaro Avatar answered Oct 18 '22 00:10

Jason Gennaro


For cross-browsing it is better to use text-underline-offset over the text-underline-position, because text-underline-position isn't supported by iOS Safari

So use this answer: https://stackoverflow.com/a/63607426/1894907

#line{
    text-decoration-line: underline;
    text-underline-offset: 2px;
}
like image 39
Ruslan Korkin Avatar answered Oct 18 '22 00:10

Ruslan Korkin


A great way to do this without adding and extra spans and having more control is using the :after selector.

Useful especially for navigation menus:

.active a:after {
    content: '';
    height: 1px;
    background: black; 
    display:block;
}

If you want more or less space between the text and the underline, add a margin-top.

If you want a thicker underline, add more height:

.active a:after {
    content: '';
    height: 2px;
    background: black; 
    display:block;
    margin-top: 2px;
}
like image 16
jake Avatar answered Oct 18 '22 00:10

jake


Simply use:

text-decoration: underline;
text-underline-position: under;

image

like image 12
Mirza_Umair Avatar answered Oct 17 '22 23:10

Mirza_Umair