Liking cljdoc? Tell your friends :D

x-typography

A themeable text styling wrapper that applies consistent typographic styles via a variant attribute. Supports headings, body text, captions, code, keyboard shortcuts, blockquotes, and more. Includes text alignment, single-line truncation, multi-line clamping, and gradient text.


Tag

<x-typography></x-typography>

Attributes

AttributeTypeDefaultDescription
variantenum"body1"Typographic variant (see Variants below)
alignenum"left"Text alignment: left center right justify
truncatebooleanfalseSingle-line ellipsis truncation
line-clampnumber—Multi-line truncation (positive integer, number of lines)
gradientbooleanfalseFills the text with --x-typography-gradient (see Gradient text below)

Attribute values are case-insensitive (e.g. variant="H1" normalises to h1).

When both truncate and line-clamp are set, truncate takes precedence (single-line).

Invalid line-clamp values (zero, negative, non-numeric, floats) are ignored — the attribute is treated as absent.


Variants

VariantSizeWeightLine HeightLetter SpacingNotes
h12.5rem7001.2-0.02em
h22rem7001.25-0.015em
h31.75rem6001.3-0.01em
h41.5rem6001.35-0.005em
h51.25rem6001.4normal
h61.125rem6001.4normal
subtitle11.125rem5001.40.005em
subtitle20.875rem5001.40.01em
body11rem4001.5normalDefault
body20.875rem4001.5normal
caption0.75rem4001.40.02em
overline0.625rem6001.50.1emUppercase
small0.8125rem4001.4normal
blockquote1.125rem4001.6normalItalic, left border
code0.875rem4001.5—Monospace, background
kbd0.875rem4001.5—Monospace, background, border

Properties

PropertyTypeReflects attributeDefault
variantstringvariant"body1"
alignstringalign"left"
truncatebooleantruncatefalse
lineClampnumber | nullline-clampnull
gradientbooleangradientfalse

Note: lineClamp (camelCase) maps to the line-clamp (kebab-case) HTML attribute. Reading a property before its attribute is set returns the default listed above.


Events

None.


Slots

SlotDescription
(default)Text content or child elements to style

Parts

PartDescription
containerInner <div> wrapper that receives typographic styles
textInline <span> around the text. It holds the gradient under gradient

CSS Custom Properties

Typography

VariableDefaultDescription
--x-typography-font-familyvar(--x-font-family, inherit)Base font family. The theme's font, or else the font of the page
--x-typography-mono-font-familyvar(--x-font-family-mono, ui-monospace, "SFMono-Regular", ...)Monospace font for code/kbd
--x-typography-colorvar(--x-color-text,inherit)Text color

Gradient

VariableDefault (light)Default (dark)Description
--x-typography-gradientlinear-gradient(90deg,var(--x-color-primary,#2563eb),var(--x-color-secondary,#64748b),var(--x-color-tertiary,#94a3b8))linear-gradient(90deg,var(--x-color-primary,#3b82f6),var(--x-color-secondary,#94a3b8),var(--x-color-tertiary,#64748b))Any CSS image. Fills the text under gradient

Code variant

VariableLight defaultDark defaultDescription
--x-typography-code-bgrgba(0,0,0,0.06)rgba(255,255,255,0.1)Background
--x-typography-code-radiusvar(--x-radius-sm,4px)—Border radius
--x-typography-code-padding0.15em 0.35em—Padding

Kbd variant

VariableLight defaultDark defaultDescription
--x-typography-kbd-bgrgba(0,0,0,0.06)rgba(255,255,255,0.1)Background
--x-typography-kbd-borderrgba(0,0,0,0.15)rgba(255,255,255,0.2)Border color
--x-typography-kbd-radiusvar(--x-radius-sm,4px)—Border radius
--x-typography-kbd-padding0.15em 0.4em—Padding

Blockquote variant

VariableLight defaultDark defaultDescription
--x-typography-blockquote-border-colorrgba(0,0,0,0.2)rgba(255,255,255,0.25)Left border
--x-typography-blockquote-padding0 0 0 1em—Padding

Accessibility

This component is purely presentational. It does not add ARIA roles, landmarks, or keyboard behavior. For semantic document structure, use native HTML heading elements (<h1>–<h6>), <p>, <blockquote>, <code>, etc. alongside or instead of this component.

Gradient text:

  • Under forced-colors: active the gradient is removed and the text takes a solid colour, so it stays visible.
  • A browser without background-clip: text shows the text in its normal colour.
  • Selected text turns solid on a tint of --x-color-primary, so a selection stays readable. A slotted element gets this in browsers that pass selection styles down, such as Chrome 134 and later.
  • The contrast of a gradient changes along the text. Check the contrast of each colour stop against the background.

Examples

Basic variants

<x-typography variant="h1">Main Heading</x-typography>
<x-typography variant="body1">Body text paragraph.</x-typography>
<x-typography variant="caption">A small caption.</x-typography>
<x-typography variant="overline">Overline label</x-typography>

Alignment

<x-typography variant="body1" align="center">Centred text</x-typography>
<x-typography variant="body1" align="right">Right-aligned</x-typography>

Truncation

<x-typography variant="body1" truncate>
  This very long text will be truncated with an ellipsis on a single line.
</x-typography>

Line clamping

<x-typography variant="body1" line-clamp="3">
  This multi-line text will be clamped to three visible lines.
  Any overflow beyond the third line is hidden with an ellipsis.
</x-typography>

Code and Kbd

<x-typography variant="code">const x = 42;</x-typography>
<x-typography variant="kbd">Ctrl + C</x-typography>

Blockquote

<x-typography variant="blockquote">
  The only way to do great work is to love what you do.
</x-typography>

Gradient text

gradient fills the text with a gradient. The gradient spans the text, not the width of the element, and runs on across wrapped lines.

With no other setting, the gradient follows the theme: it runs from --x-color-primary to --x-color-secondary and --x-color-tertiary. A change of x-theme preset changes it.

<x-theme preset="sunset">
  <x-typography variant="h1" gradient>Gradient heading</x-typography>
</x-theme>

Set --x-typography-gradient to use your own colours. It wins over the theme.

x-typography.brand {
  --x-typography-gradient: linear-gradient(90deg, #e2477e, #ff8a65, #ffd54f, #a5f3f5);
}
  • A slotted element with its own color keeps that colour.
  • A slotted block (p, div, h1 to h6, ul, ol, li, blockquote) gets the gradient on its own text. It spans the width of the block.
  • The code and kbd variants keep their background.
  • Under truncate and line-clamp, the gradient spans the whole text, so the hidden part takes the last colours. The ellipsis takes the solid text colour.

The text part holds the gradient, so it can move. Set it with longhand properties: a background shorthand on ::part(text) resets the clip to the text.

x-typography.shimmer::part(text) {
  background-size: 200% 100%;
  animation: shimmer 4s linear infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  x-typography.shimmer::part(text) { animation: none; }
}

Programmatic (JavaScript)

const el = document.createElement('x-typography');
el.variant = 'h2';
el.align = 'center';
el.textContent = 'Dynamic heading';
document.body.appendChild(el);

ClojureScript / hiccup

[:x-typography {:variant "h1"} "Page Title"]
[:x-typography {:variant "body1" :truncate true} "Long text..."]
[:x-typography {:variant "body1" :line-clamp "3"} "Multi-line clamped text..."]
[:x-typography {:variant "code"} "(defn hello [] \"world\")"]
[:x-typography {:variant "h1" :gradient true} "Gradient heading"]

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close