Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space below the text baseline with CSS

Tags:

html

css

fonts

cjk

Lately I've been working with Japanese text, and I've found a rather annoying property. In Japanese, unlike English, glyphs do not extend below the text baseline. This example should show what I mean:

div {
  font-size: 72pt;
  display: inline-block;
  text-decoration: underline;
  border: 1px solid red;
  margin: 10px;
  text-align: center;
}
<div lang="ja">日本語</div>
<div lang="en">English</div>

Notice how the "g" in "English" extends below the underline, but none of the characters in 日本語 do. This is typical of Japanese text. Despite this, space is still reserved below the underline, and in fact on my screen the Japanese text reserves more space than the English text does. My question is this:

Is there a way to remove this space with CSS which is reliable across changing fonts and font sizes? I can see at least two possible approaches:

  • Remove the space below the baseline directly.
  • Move the baseline to be at the bottom of the containing box.
like image 385
Rose Kunkel Avatar asked Aug 10 '16 14:08

Rose Kunkel


2 Answers

You need to reset the line-height so it's not bigger than 1. The initial value is normal which depends on the User Agent of the browser, on the font-family and on the font-size, but it's some number between 1 and 1.2 in general. Here's more information if you're interested.

div {
  font-size: 72pt;
  display: inline-block;
  text-decoration: underline;
  border: 1px solid red;
  margin: 10px;
  text-align: center;
  line-height: 1;
}
<div lang="ja">日本語</div>
<div lang="en">English</div>
like image 57
Juan Ferreras Avatar answered Sep 21 '22 17:09

Juan Ferreras


Just set the line height to the same size as the font size: line-height: 72pt. This normalizes the space that's taken for the font height. Of course you can take every value for the line height that you like, to adjust that space. More information to line-height at MDN.

div {
  font-size: 72pt;
  line-height: 72pt;
  display: inline-block;
  text-decoration: underline;
  border: 1px solid red;
  margin: 10px;
  text-align: center;
}
<div lang="ja">日本語</div>
<div lang="en">English</div>
like image 43
andreas Avatar answered Sep 21 '22 17:09

andreas