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 Family | General Font Family | Supported Font Weight | Supported Font Style |
|---|---|---|---|
| AlibabaPuHuiTi | sans-serif | 100 300 400 500 700 900 | normal |
| Alibaba Sans KR | sans-serif | 100 300 400 500 700 900 | normal |
| Source Han Serif CN | serif | 200 300 400 500 600 700 900 | normal |
| Source Han Serif JP | serif | 200 300 400 500 600 700 900 | normal |
| Source Han Serif KR | serif | 200 300 400 500 600 700 900 | normal |
| Source Han Sans CN | sans-serif | 100 300 350 400 500 700 900 | normal |
| Source Han Sans TW | sans-serif | 100 300 350 400 500 700 900 | normal |
| Source Han Sans JP | sans-serif | 100 300 350 400 500 700 900 | normal |
| Source Han Sans KR | sans-serif | 100 300 350 400 500 700 900 | normal |
| Geist | sans-serif | 100 200 300 400 500 600 700 800 900 | normal oblique |
| Geist Mono | monospace | 100 200 300 400 500 600 700 800 900 | normal |
| Twemoji | emoji | N/A | N/A |
About font weights and font styles
Font weights:
100- Thin200- ExtraLight300- Light400- Regular500- Medium600- SemiBold700- Bold800- ExtraBold900- 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
obliquestyle of Geist is a faux italic created by mechanically slanting the upright glyphs - For an italic effect on Chinese text, use CSS
transform: skewX()orfont-style: oblique
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:
Hello- AlibabaPuHuiTi is tried first; it contains Latin letters, so it renders them directly世界(Chinese for "world") - AlibabaPuHuiTi contains Chinese characters, so it renders them directly🎉- 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 → TwemojiDefault 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-familyexplicitly
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 family | Fallback order (table order) |
|---|---|
sans-serif | AlibabaPuHuiTi → Alibaba Sans KR → Source Han Sans CN → Source Han Sans TW → Source Han Sans JP → Source Han Sans KR → Geist |
serif | Source Han Serif CN → Source Han Serif JP → Source Han Serif KR |
monospace | Geist Mono |
emoji | Twemoji |
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:
- Next font in the font list - If
font-familylists multiple fonts, they are tried in order - Other fonts in the same category - Built-in fonts with the same General Font Family are searched
- Fonts in other categories - Fonts from other categories are tried
- 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:
Hello- Geist contains Latin letters, so it renders them directly世界- Geist has no Chinese glyphs, so it falls back to AlibabaPuHuiTi (the first sans-serif font with Chinese glyphs)🎉- 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:
- Geist is used first for Latin letters
- Chinese characters fall back to Source Han Sans CN
- The system default sans-serif is used last
Best practices
- Specify a fallback font - Always end
font-familywith a generic font family name as a safety net - Consider each language - For mixed-language content, explicitly specify fallback fonts for each language
- Emoji handling - For precise control over emoji rendering, add
emojinear 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:
| Value | Collapses white space | Preserves line breaks | Wraps text | Description |
|---|---|---|---|---|
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:
| Value | Description |
|---|---|
wrap | Default. Text wraps at the end of a line, affected by properties such as word-break and overflow-wrap |
nowrap | Prevents automatic wrapping; whether explicit line breaks in the source are kept depends on the white-space rules |
balance | Balanced wrapping that adjusts break points so lines have similar lengths; suited to short text such as headings |
pretty | Prettier wrapping for paragraphs; actual break points depend on the text and available width |
balance vs pretty
balanceworks well for short text such as headings and card content, evening out line lengthsprettyworks 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: nowrapcollapses white space and source line breaks, and also prevents wrappingtext-wrap: nowraponly prevents wrapping and does not change white space handling; combined withwhite-space: pre-wraporpre, 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
| Value | Description |
|---|---|
clip | Default. No ellipsis is shown; whether overflowing content is clipped depends on overflow |
ellipsis | Shows 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.