DEV Community

Cover image for How to increase the gap between text and underlining in CSS
Uchechukwu Obasi
Uchechukwu Obasi

Posted on

2

How to increase the gap between text and underlining in CSS

Most frontend engineers just like me might have encountered this same problem where creating a gap between text and underline is a huge pain.

In this short article, I will share an effective CSS trick that can make this happen with no stress.

First, you create a div around that particular element you wish to place an underline.

Alt Text

After that, you proceed to style that particular div by giving it a padding and border-bottom value of your choice.

Alt Text

If you preview the output at this point, you will notice that the width of the underline spans across the width of it's parent container of which you wouldn't want that to happen. How do we make the underline the same width with the content? Boom! The Max-content value of the width property! Works like magic!

Alt Text

Although the CSS Working Group has published a draft for text decoration level 4 which would add a new property text-underline-offset (as well as text-decoration-thickness) to allow control over the exact placement of an underline.

As of this writing, it's an early-stage draft and has not been implemented by any browser. However, this trick is widely supported across all major browsers and also works perfectly with multiple text as well.

I hope you find this article helpful.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (1)

Collapse
 
itsjzt profile image
Saurabh Sharma

I used to add border-bottom inline elements

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video