Languages

CSS - text-decoration underline add padding

0 points
Asked by:
James-Z
767

How can I add 3px padding to the text-decoration: underline in CSS?

My code:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .text {
        text-decoration: underline;
    }

  </style>
</head>
<body>
  <div class="text">Example text...</div>
</body>
</html>

I want make greater offset between the text and the line.

1 answer
0 points
Answered by:
James-Z
767

You can use text-underline-offset: 3px but be careful, this solution was released in major browsers around 2020.

As an alternative that will work in the most modern web browsers, you can use border-bottom property with some padding.

Practical example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .text {
        text-decoration: underline;
        text-underline-offset: 3px; /*  <-----  */
    }

  </style>
</head>
<body>
  <div class="text">Example text...</div>
</body>
</html>

Alternative solution

Change the div element display to inline and use border-bottom property. Then add some padding-bottom to the element for more space between the text and underline.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .text {
        display: inline;
        border-bottom: 1px solid;
        padding-bottom: 3px;
    }

  </style>
</head>
<body>
  <div class="text">Example text...</div>
</body>
</html>

 

References

  1. text-underline-offset - CSS: Cascading Style Sheets | MDN
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join