Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Move the underline below the descenders

When setting the text-decoration:underline on an element, the line is shown on the baseline.

enter image description here

It is over the descender of letters. It makes reading difficult if the line contains many letters with descenders.

enter image description here

I want to move the underline below the descenders using text-underline-offset: 4px;.

enter image description here

It works.

On Chrome, it is exactly how I want. However, it is not below the descenders on Safari.

enter image description here

If I use, text-underline-offset: 5px;, It works on Safari. But on Chrome, It is close to the ascenders of the second line rather than being close to the descenders of the same line. I can solve it using a higher value line-height, but that I don't want.

Is there any way to caclualte the height of the descenders so that I offset the underline exaclty that much below the baseline?

Info:

The font I am using is called Basis Grotesque Pro regular

Links

  • Ascender
  • Descender
  • text-underline-offset
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using the ideas from https://stackoverflow.com/questions/10180148/how-to-calculate-descender-height-in-javascript#:~:text=This%20is%20possible%3A%20use%20this%20library%3A%20baseline%20ratio%2C,The%200%20px%20font%20lies%20on%20the%20baseline.

you can calculate the height of a descender by putting two spans next to each other.

The first has a font size of 0px. If you find its top position that will be the position of the baseline for the pair of spans.

The second span has whatever font size you are interested in.

Then you can get the height of the ascender by taking the top of the second span from the top of the first span. The height of the descender will be the height of the second span minus this amount.

const span1Params = document.querySelector('.span1').getBoundingClientRect();
const span2Params = document.querySelector('.span2').getBoundingClientRect();
document.querySelector('.text').style.textUnderlineOffset = (span2Params.height - (span1Params.top - span2Params.top)) + 'px';
.test {
  position: absolute;
  top: -1000px;
}

.span1 {
  font-size: 0px;
}

.span2 {
  font-size: 36px;
}

.text {
  text-decoration: underline;
  font-size: 36px;
}
<div class="test">
  <span class="span1">j</span>
  <span class="span2">j</span>
</div>
<div class="text">Atmospheric impacts of hydrogen as an energy carrier</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!