imgrender

Typography and fonts

Font management and advanced text rendering

Fonts

imgrender comes with a set of commonly used fonts.

Built-in fonts

imgrender includes the following fonts, all of which are free for commercial use.

Font FamilyGeneral Font FamilySupported Font WeightSupported Font Style
AlibabaPuHuiTisans-serif100 300 400 500 700 900normal
Alibaba Sans KRsans-serif100 300 400 500 700 900normal
Source Han Serif CNserif200 300 400 500 600 700 900normal
Source Han Serif JPserif200 300 400 500 600 700 900normal
Source Han Serif KRserif200 300 400 500 600 700 900normal
Source Han Sans CNsans-serif100 300 350 400 500 700 900normal
Source Han Sans TWsans-serif100 300 350 400 500 700 900normal
Source Han Sans JPsans-serif100 300 350 400 500 700 900normal
Source Han Sans KRsans-serif100 300 350 400 500 700 900normal
Geistsans-serif100 200 300 400 500 600 700 800 900normal oblique
Geist Monomonospace100 200 300 400 500 600 700 800 900normal
TwemojiemojiN/AN/A

About font weights and font styles

Font weights:

  • 100 - Thin
  • 200 - ExtraLight
  • 300 - Light
  • 400 - Regular
  • 500 - Medium
  • 600 - SemiBold
  • 700 - Bold
  • 800 - ExtraBold
  • 900 - Black/Heavy

About italics:

  • CJK (Chinese, Japanese, Korean) fonts such as Source Han Serif, Source Han Sans, and AlibabaPuHuiTi usually do not include true italic glyphs
  • The oblique style of Geist is a faux italic created by mechanically slanting the upright glyphs
  • For an italic effect on Chinese text, use CSS transform: skewX() or font-style: oblique

Font sources

  • AlibabaPuHuiTi / Alibaba Sans KR - By the Alibaba design team, GitHub
  • Source Han Serif - Open-source font by Adobe, GitHub
  • Source Han Sans - Open-source font by Adobe, GitHub
  • Geist / Geist Mono - By Vercel, Website / GitHub
  • Twemoji - Open-source emoji font by Twitter, GitHub

Using fonts

Use a generic font family name:

<div style="font-family: sans-serif">Hello imgrender</div>

This uses a sans-serif font. Since there are several sans-serif fonts, the actual font follows the font fallback rules.

<div style="font-family: serif">Hello imgrender</div>

This uses a serif font. Since there are several serif fonts, the actual font follows the font fallback rules.

Use a specific font:

<div style="font-family: Source Han Serif CN">Hello imgrender</div>

This explicitly uses the Source Han Serif CN font.

Font fallback

Font fallback automatically picks an alternative font when the preferred font is unavailable or does not contain the required characters. imgrender handles font fallback according to the following rules:

Fallback when no font is specified

When font-family is not set explicitly, imgrender tries the fonts in the order of the built-in fonts table until it finds one that can render the current character:

<!-- No font-family specified -->
<div>Hello 世界 🎉</div>

Rendering process:

  1. Hello - AlibabaPuHuiTi is tried first; it contains Latin letters, so it renders them directly
  2. 世界 (Chinese for "world") - AlibabaPuHuiTi contains Chinese characters, so it renders them directly
  3. 🎉 - AlibabaPuHuiTi has no emoji, so imgrender continues down the table and Twemoji renders it

Full fallback order (top to bottom of the table):

AlibabaPuHuiTi → Alibaba Sans KR → Source Han Serif CN → Source Han Serif JP →
Source Han Serif KR → Source Han Sans CN → Source Han Sans TW → Source Han Sans JP →
Source Han Sans KR → Geist → Geist Mono → Twemoji

Default font selection

Because AlibabaPuHuiTi is first in the table and covers both CJK characters and Latin letters, most text without an explicit font is rendered with AlibabaPuHuiTi. This means:

  • Chinese, English, and digits use AlibabaPuHuiTi (a sans-serif style) by default
  • Emoji use Twemoji by default
  • For a serif font or a specific style, set font-family explicitly

Fallback with generic font family names

When you use a generic font family name (such as sans-serif, serif, monospace, or emoji), imgrender looks up the first available font in that category, top to bottom in the built-in fonts table:

Generic font familyFallback order (table order)
sans-serifAlibabaPuHuiTi → Alibaba Sans KR → Source Han Sans CN → Source Han Sans TW → Source Han Sans JP → Source Han Sans KR → Geist
serifSource Han Serif CN → Source Han Serif JP → Source Han Serif KR
monospaceGeist Mono
emojiTwemoji

Example:

The sample text 你好世界 below is Chinese for "Hello world".

<!-- With sans-serif, AlibabaPuHuiTi is used first -->
<div style="font-family: sans-serif">你好世界</div>

<!-- With serif, Source Han Serif CN is used first -->
<div style="font-family: serif">你好世界</div>

Fallback for missing characters

When the current font does not contain a glyph for a character, the following order is used:

  1. Next font in the font list - If font-family lists multiple fonts, they are tried in order
  2. Other fonts in the same category - Built-in fonts with the same General Font Family are searched
  3. Fonts in other categories - Fonts from other categories are tried
  4. Last-resort font - The system default fallback font is used

Example:

<!-- Fallback for mixed-script text -->
<div style="font-family: Geist">Hello 世界 🎉</div>

Rendering process:

  1. Hello - Geist contains Latin letters, so it renders them directly
  2. 世界 - Geist has no Chinese glyphs, so it falls back to AlibabaPuHuiTi (the first sans-serif font with Chinese glyphs)
  3. 🎉 - None of the text fonts contain emoji, so it falls back to Twemoji

Custom fallback list

You can list multiple fonts in font-family, separated by commas, to define your own fallback order:

<div style="font-family: Geist, Source Han Sans CN, sans-serif">Hello 世界</div>

Fallback order:

  1. Geist is used first for Latin letters
  2. Chinese characters fall back to Source Han Sans CN
  3. The system default sans-serif is used last

Best practices

  1. Specify a fallback font - Always end font-family with a generic font family name as a safety net
  2. Consider each language - For mixed-language content, explicitly specify fallback fonts for each language
  3. Emoji handling - For precise control over emoji rendering, add emoji near the end of the font list
<div style="font-family: Geist, Source Han Sans CN, Twemoji, sans-serif">
  Hello 世界 🎉
</div>

Typography

White space

By default, consecutive spaces, tabs, and line breaks collapse into a single space, and text wraps automatically to fit the available width. imgrender does not fully ignore leading and trailing white space: leading white space can cause a visible offset, and trailing white space can increase an element's width. Avoid unintended line breaks and indentation before or after text inside tags.

You can also use the white-space property to control how white space is rendered:

ValueCollapses white spacePreserves line breaksWraps textDescription
normal✅❌✅Default. Consecutive white space and line breaks collapse into one space; text wraps
nowrap✅❌❌Collapses white space and line breaks but prevents wrapping; text stays on one line
pre❌✅❌Preserves all white space and line breaks and prevents wrapping, like a <pre> element
pre-line✅✅✅Collapses white space but preserves line breaks; text wraps and source line breaks are kept
pre-wrap❌✅✅Preserves all white space and line breaks while allowing wrapping; good for formatted code

imgrender does not support white-space: break-spaces. Using this value causes a render error.

Examples

Preserve line breaks and indentation from the source:

<div style="white-space: pre-wrap">First line of text
  Second line is indented
Third line is not indented</div>

Prevent text from wrapping:

<div style="white-space: nowrap">This text will not wrap, even if the container is too narrow</div>

About line breaks

In HTML, you can type line breaks and indentation directly inside tags. With white-space: pre-wrap, pre, or pre-line, line breaks in the source are preserved; whether indentation is preserved depends on the white-space value.

Line wrapping

The CSS text-wrap property controls how text wraps inside an element. imgrender supports the following values:

ValueDescription
wrapDefault. Text wraps at the end of a line, affected by properties such as word-break and overflow-wrap
nowrapPrevents automatic wrapping; whether explicit line breaks in the source are kept depends on the white-space rules
balanceBalanced wrapping that adjusts break points so lines have similar lengths; suited to short text such as headings
prettyPrettier wrapping for paragraphs; actual break points depend on the text and available width

balance vs pretty

  • balance works well for short text such as headings and card content, evening out line lengths
  • pretty works well for paragraphs, adjusting where lines break
  • The effect of both depends on the text and available width; in some cases the result is the same as plain wrap

Examples

Balanced wrapping (for headings):

<h1 style="text-wrap: balance">
  This is a long title that will be wrapped evenly across lines
</h1>

Pretty wrapping (for paragraphs):

<p style="text-wrap: pretty">
  This is a long paragraph text that benefits from prettier line breaking with
  better typography considerations like preventing orphans.
</p>

Prevent automatic wrapping:

<div style="text-wrap: nowrap">This text will not wrap automatically</div>

Relationship with white-space

With default white space handling, both text-wrap: nowrap and white-space: nowrap keep text on a single line, but they are not fully equivalent:

  • white-space: nowrap collapses white space and source line breaks, and also prevents wrapping
  • text-wrap: nowrap only prevents wrapping and does not change white space handling; combined with white-space: pre-wrap or pre, source line breaks and consecutive spaces are still preserved

To preserve spaces and explicit line breaks while preventing wrapping, use white-space: pre.

Text overflow ellipsis

When a container is too small to show the full text, use the text-overflow property to control how overflowing text is displayed.

The text-overflow property

ValueDescription
clipDefault. No ellipsis is shown; whether overflowing content is clipped depends on overflow
ellipsisShows an ellipsis ... at the end of the overflowing text

Custom text-overflow strings are not supported as ellipsis markers. Use ellipsis.

When it takes effect

The ellipsis appears only when the text actually exceeds the available space. The element needs a clear width constraint, such as width or max-width, or a width constraint from its parent layout. flex-shrink alone does not define a width constraint.

Single-line ellipsis is usually combined with white-space: nowrap. In imgrender, text-overflow: ellipsis also works with overflow: visible, so overflow: hidden is not required. To clip content outside the element's bounds, set overflow: hidden or clip.

imgrender does not support overflow: scroll or auto. Using these values causes a render error.

Single-line ellipsis

<div
  style="width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis"
>
  This is a very, very long piece of text that shows an ellipsis when it exceeds the container width
</div>

Multi-line ellipsis

For multi-line text, use line-clamp to limit the number of lines, together with text-overflow: ellipsis to show an ellipsis where the text is cut off:

<div
  style="width: 300px; line-clamp: 3; text-overflow: ellipsis; overflow: hidden"
>
  This is a very, very long piece of text that is truncated with an ellipsis once it exceeds three lines.
  Multi-line ellipsis is useful for article summaries, product descriptions, and similar content. Add more text to see the line clamping and ellipsis in action.
</div>

Line clamping and ellipsis

Both line-clamp and -webkit-line-clamp limit the number of visible lines, but setting them alone does not show an ellipsis. To show an ellipsis, also set text-overflow: ellipsis.

imgrender does not support display: -webkit-box. Using this value causes a render error. You do not need to set it or -webkit-box-orient for line clamping.

On this page