日本語

Tightening Spacing Between Japanese Characters with CSS palt

​​CSS​Fonts,Japanese typesetting,Playwright

To tighten the spacing between Japanese characters, set font-feature-settings: "palt" in CSS. The browser uses the spacing information stored in the font (the OpenType feature palt), so kana, brackets, and punctuation are set at widths that fit each glyph. If the font does not have this feature, the setting changes nothing.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I used Noto Sans CJK JP, IPA Gothic, and IPA P Gothic. I opened a test page with Playwright and measured the width of each character. I did not test Firefox, Safari, fonts installed on Windows or macOS, or fonts loaded as web fonts. The browser support versions are the values from the compatibility data (@mdn/browser-compat-data 8.1.8).

Two headings stacked vertically. In the top one, "Not set," the heading "I'd like to borrow a cat's paw." has gaps around the brackets and the comma. Its underline is 13.00 characters wide. In the bottom one, "font-feature-settings: "palt"," the same heading has tighter spacing between characters, and its underline is 10.51 characters wide
Top: no setting. Bottom: the heading with palt. The light blue underline shows the width of the heading

Apply palt to the element you want to tighten

Apply font-feature-settings: "palt" to the element you want to tighten.

css
h1,
h2 {
  font-feature-settings: "palt";
}

If you write only the feature name (a four-character tag) such as "palt", the feature is turned on. According to the MDN description, adding on or 1 after the name has the same effect, and adding off or 0 turns the feature off. font-feature-settings is inherited by child elements (CSS Fonts Level 4 specification).

Which characters are tightened, and by how much

In the test environment, I measured the 20px characters in Noto Sans CJK JP one at a time. The unit is em (a ratio to the character size), where 1.00 is a full-width character.

Character Not set palt
「 」 、 。 1.00 0.50
こ 1.00 0.87
り 1.00 0.83
ね 1.00 1.01
タ 1.00 0.88
手 借 1.00 1.00
A 0.61 0.61

Brackets and punctuation were reduced to half width. Hiragana and katakana narrowed to about 0.83 to 0.96, depending on the shape of each character. A few characters, such as ね, became slightly wider. Kanji stayed at full width, and the width of Latin letters did not change.

For the whole line 「ねこの手」を、借りたい。カタカナとABC, the width was 19.90em without a setting and 16.95em with palt, so it became about three characters shorter.

To tighten only brackets and punctuation, use halt

Noto Sans CJK JP also has the halt feature. When I set font-feature-settings: "halt" on the same line, only brackets and punctuation became 0.50, and kana stayed at 1.00. The width of the whole line was 17.90em.

Brackets in a row did not get narrower than half width

Gaps between consecutive brackets, such as 」「, can also be tightened with the text-spacing-trim property. In Chromium and Noto Sans CJK JP in the test environment, the 「 in 」「 became 0.50 without any setting. (MDN describes this property as experimental, and it has no effect unless the font has the halt or chws feature.) I checked whether combining it with palt would tighten too much, using the line 「東」「東」(東)、東。.

With palt, all brackets and punctuation became 0.50, and the whole line was 8.00em. Consecutive brackets did not become narrower than 0.50. Adding text-spacing-trim: space-all (which turns off the trimming) to palt still gave 8.00em.

Fonts without palt show no change

palt uses information stored in the font. When I checked with the Python library fontTools, Noto Sans CJK JP had palt, but IPA Gothic did not. The following code checks the IPA Gothic file (ipag.ttf) and prints False.

python
from fontTools.ttLib import TTFont

font = TTFont("ipag.ttf")
tags = set()
if "GPOS" in font:
    tags = {r.FeatureTag for r in font["GPOS"].table.FeatureList.FeatureRecord}
print("palt" in tags)

When I set palt on the same line in IPA Gothic, every character had the same width as without a setting, and the whole line stayed at 19.50em. If setting a value does not change the appearance, check whether the font being displayed has this feature.

Some fonts, such as IPA P Gothic, are proportional, meaning each character has its own width from the start. IPA P Gothic does not have palt, but its width without a setting was 16.67em for the whole line, which was close to the width of Noto Sans CJK JP with palt.

font-variant-east-asian: proportional-width does not tighten

The MDN page for font-feature-settings recommends using individual properties such as font-variant-east-asian whenever possible. However, proportional-width in font-variant-east-asian corresponds to a different feature, pwid, not to palt.

In the test environment, font-variant-east-asian: proportional-width did not change the widths of kana, brackets, or punctuation, which stayed at 1.00. What changed was the full-width Latin letters and digits.

Character Not set proportional-width palt
A 1.00 0.61 0.78
1 1.00 0.56 0.76
ね 1.00 1.00 1.01
、 1.00 1.00 0.50

The full-width A became 0.61, the same as the half-width A. To tighten the spacing between characters, use palt in font-feature-settings.

The horizontal kerning procedure in the CSS Fonts Level 4 specification says that when kerning is enabled with font-kerning, the font's palt is applied if it has one. In Chromium 153 in the test environment, setting font-kerning: normal did not tighten the line, which stayed at 19.90em.

Setting font-feature-settings on a child element removes palt

Settings from a parent and a child element are not added together for font-feature-settings. The font-feature-settings section of the specification also says that the value is set all at once, and there is no way to add or remove one feature from an inherited value. If you set a different feature on a child element, palt inherited from the parent is lost.

In the test environment, I set palt on body, applied one of three settings to a span inside it, and measured the width of the six characters in 「ねこの手」.

css
body {
  font-feature-settings: "palt";
}

/* 1. palt is lost */
span {
  font-feature-settings: "tnum";
}

/* 2. Also list palt */
span {
  font-feature-settings: "palt", "tnum";
}

/* 3. Set the digit width with a separate property */
span {
  font-variant-numeric: tabular-nums;
}
span setting Computed font-feature-settings Width of 「ねこの手」
None "palt" 4.84em
1. "tnum" only "tnum" 6.00em
2. "palt", "tnum" "palt", "tnum" 4.84em
3. font-variant-numeric "palt" 4.84em

In the span that had only "tnum", the width returned to 6.00em, the same as with no palt. When you add another feature on a child element, either list palt as well or use an individual property such as font-variant-numeric.

In body text, characters per line and the right edge change

Because palt changes the width of each character, the number of characters per line also changes from line to line.

In the test environment, I put the same text in a paragraph 15 characters wide and measured the number of characters and the right edge of each line.

Line Not set palt palt + justify
Line 1 15 characters, 15.00em 15 characters, 14.11em 15 characters, 15.00em
Line 2 15 characters, 15.00em 16 characters, 14.74em 16 characters, 15.00em
Line 3 15 characters, 14.95em 16 characters, 14.97em 16 characters, 15.00em
Line 4 14 characters, 13.90em 17 characters, 14.93em 17 characters, 15.00em
Line 5 12 characters, 11.77em 7 characters, 5.80em 7 characters, 5.80em

Without a setting, lines 1 to 3 each had 15 characters, and their right edges were at 15.00em and 14.95em. Line 4 had 14 characters, with its right edge at 13.90em. With palt, the number of characters per line varied from 15 to 17. Except for the last line, the right edges ranged from 14.11em to 14.97em and were uneven. When I added text-align: justify to the paragraph, the right edges of all lines except the last were aligned at 15.00em.

Use vpal for vertical text

The kerning procedure in the specification uses palt for horizontal text and vpal for vertical text. In the test environment, setting palt on a paragraph in vertical text (writing-mode: vertical-rl) did not change the height of the characters. To tighten vertical text, use vpal.

Three vertical columns side by side. The left column, "Not set," and the middle column, ""palt"," show the characters of "I'd like to borrow a cat's paw." spaced at the same intervals. Both are 13.00 characters tall. The right column, ""vpal"," has tighter spacing between characters and is 10.55 characters tall
Vertical text comparison. From left: no setting, palt, vpal. The light blue line to the right of each column shows the column height
css
.vertical {
  writing-mode: vertical-rl;
  font-feature-settings: "vpal";
}

Noto Sans CJK JP also has vpal. With vpal, brackets and punctuation became 0.50, and kana became 0.87 to 0.96. The column height of 「ねこの手」を、借りたい。 changed from 13.00em to 10.55em.

Support in major browsers

According to the compatibility data, font-feature-settings is available in Chrome 48, Edge 12, Firefox 34, Safari 9.1, Safari on iOS 9.3, and Chrome on Android 48. However, how much the text tightens depends on the font. On a device where the font does not have palt, the text will not tighten.

​Posted by 86