Typography
Overview
Typography utilities help you control text styling including font families, sizes, weights, colors, alignment, decoration, and text overflow behavior. This is one of the most comprehensive utility categories.
Why Use Typography Utilities?
Typography utilities help you:
- Maintain consistency: Apply the same type styles across your application
- Integrate with design tokens: Reference your typography scale with
ref() - Handle text overflow: Control how long text is displayed and truncated
- Support accessibility: Ensure readable text with proper sizing and contrast
useFontSizeUtility
The useFontSizeUtility() function creates utility classes for setting font sizes.
import { styleframe } from "styleframe";
import { useFontSize } from "@styleframe/theme";
import { useFontSizeUtility } from "@styleframe/theme";
const s = styleframe();
const { ref } = s;
const { fontSizeXs, fontSizeSm, fontSizeBase, fontSizeLg, fontSizeXl, fontSize2xl, fontSize3xl } = useFontSize(s, {
xs: '0.75rem',
sm: '0.875rem',
base: '1rem',
lg: '1.125rem',
xl: '1.25rem',
'2xl': '1.5rem',
'3xl': '1.875rem',
} as const);
useFontSizeUtility(s, {
xs: ref(fontSizeXs),
sm: ref(fontSizeSm),
base: ref(fontSizeBase),
lg: ref(fontSizeLg),
xl: ref(fontSizeXl),
'2xl': ref(fontSize2xl),
'3xl': ref(fontSize3xl),
});
export default s;
:root {
--font-size--xs: 0.75rem;
--font-size--sm: 0.875rem;
--font-size--base: 1rem;
--font-size--lg: 1.125rem;
--font-size--xl: 1.25rem;
--font-size--2xl: 1.5rem;
--font-size--3xl: 1.875rem;
}
._font-size\:xs { font-size: var(--font-size--xs); }
._font-size\:sm { font-size: var(--font-size--sm); }
._font-size\:base { font-size: var(--font-size--base); }
._font-size\:lg { font-size: var(--font-size--lg); }
._font-size\:xl { font-size: var(--font-size--xl); }
._font-size\:2xl { font-size: var(--font-size--2xl); }
._font-size\:3xl { font-size: var(--font-size--3xl); }
<p class="_font-size:sm">Small text</p>
<p class="_font-size:base">Default body text</p>
<h2 class="_font-size:2xl">Large heading</h2>
useFontFamilyUtility
The useFontFamilyUtility() function creates utility classes for setting font families.
import { styleframe } from "styleframe";
import { useFontFamilyUtility } from "@styleframe/theme";
const s = styleframe();
useFontFamilyUtility(s, {
sans: 'ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"',
serif: 'ui-serif, Georgia, Cambria, "Times New Roman", Times, serif',
mono: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
});
export default s;
._font-family\:sans { font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; }
._font-family\:serif { font-family: ui-serif, Georgia, Cambria, "Times New Roman", Times, serif; }
._font-family\:mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
<p class="_font-family:sans">Sans-serif body text</p>
<blockquote class="_font-family:serif">Serif quote</blockquote>
<code class="_font-family:mono">Monospace code</code>
useFontWeightUtility
The useFontWeightUtility() function creates utility classes for setting font weights.
import { styleframe } from "styleframe";
import { useFontWeightUtility } from "@styleframe/theme";
const s = styleframe();
useFontWeightUtility(s, {
thin: '100',
extralight: '200',
light: '300',
normal: '400',
medium: '500',
semibold: '600',
bold: '700',
extrabold: '800',
black: '900',
});
export default s;
._font-weight\:thin { font-weight: 100; }
._font-weight\:extralight { font-weight: 200; }
._font-weight\:light { font-weight: 300; }
._font-weight\:normal { font-weight: 400; }
._font-weight\:medium { font-weight: 500; }
._font-weight\:semibold { font-weight: 600; }
._font-weight\:bold { font-weight: 700; }
._font-weight\:extrabold { font-weight: 800; }
._font-weight\:black { font-weight: 900; }
<p class="_font-weight:normal">Normal weight text</p>
<strong class="_font-weight:semibold">Semibold emphasis</strong>
<h1 class="_font-weight:bold">Bold heading</h1>
useTextColorUtility
The useTextColorUtility() function creates utility classes for setting text colors.
import { styleframe } from "styleframe";
import { useColor } from "@styleframe/theme";
import { useTextColorUtility } from "@styleframe/theme";
const s = styleframe();
const { ref } = s;
const { colorGray900, colorGray500, colorPrimary } = useColor(s, {
gray900: '#111827',
gray500: '#6b7280',
primary: '#006cff',
} as const);
useTextColorUtility(s, {
inherit: 'inherit',
current: 'currentColor',
transparent: 'transparent',
default: ref(colorGray900),
muted: ref(colorGray500),
primary: ref(colorPrimary),
white: '#fff',
black: '#000',
});
export default s;
:root {
--color--gray-900: oklch(0.2107 0.0181 264.36 / 1);
--color--gray-500: oklch(0.5513 0.0169 264.36 / 1);
--color--primary: oklch(0.5749 0.233917 259.9541 / 1);
}
._text-color\:inherit { color: inherit; }
._text-color\:current { color: currentColor; }
._text-color\:transparent { color: transparent; }
._text-color\:default { color: var(--color--gray-900); }
._text-color\:muted { color: var(--color--gray-500); }
._text-color\:primary { color: var(--color--primary); }
._text-color\:white { color: #fff; }
._text-color\:black { color: #000; }
<p class="_text-color:default">Default text color</p>
<p class="_text-color:muted">Muted secondary text</p>
<a class="_text-color:primary">Primary colored link</a>
useTextAlignUtility
Control text alignment.
useTextAlignUtility(s, {
left: 'left',
center: 'center',
right: 'right',
justify: 'justify',
start: 'start',
end: 'end',
});
useLineHeightUtility
Control line height for better readability.
useLineHeightUtility(s, {
none: '1',
tight: '1.25',
snug: '1.375',
normal: '1.5',
relaxed: '1.625',
loose: '2',
});
useLetterSpacingUtility
Control letter spacing (tracking).
useLetterSpacingUtility(s, {
tighter: '-0.05em',
tight: '-0.025em',
normal: '0em',
wide: '0.025em',
wider: '0.05em',
widest: '0.1em',
});
useTextDecorationUtility
Add text decorations like underline and strikethrough.
useTextDecorationLineUtility(s, {
underline: 'underline',
overline: 'overline',
'line-through': 'line-through',
none: 'none',
});
useLineClampUtility
Truncate text after a specific number of lines with ellipsis.
import { styleframe } from "styleframe";
import { useLineClampUtility } from "@styleframe/theme";
const s = styleframe();
useLineClampUtility(s, {
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
none: 'unset',
});
export default s;
._line-clamp\:1 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
}
._line-clamp\:2 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
._line-clamp\:3 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
._line-clamp\:none {
overflow: visible;
display: block;
-webkit-box-orient: horizontal;
-webkit-line-clamp: unset;
}
/* ... more values */
<p class="_line-clamp:2">Text truncated after 2 lines with ellipsis...</p>
<p class="_line-clamp:3">Text truncated after 3 lines...</p>
<p class="_line-clamp:none">Remove line clamping</p>
More Typography Utilities
| Utility | CSS Property | Description |
|---|---|---|
useFontStyleUtility | font-style | Italic, normal, oblique |
useFontStretchUtility | font-stretch | Condensed, expanded |
useFontSmoothingUtility | -webkit-font-smoothing | Anti-aliasing control |
useFontVariantNumericUtility | font-variant-numeric | OpenType numeric features |
useTextTransformUtility | text-transform | Uppercase, lowercase, capitalize |
useTextOverflowUtility | text-overflow | Ellipsis, clip |
useTextIndentUtility | text-indent | First line indent |
useTextUnderlineOffsetUtility | text-underline-offset | Underline position |
useVerticalAlignUtility | vertical-align | Inline element alignment |
useWhitespaceUtility | white-space | Wrapping and whitespace handling |
useWordBreakUtility | word-break | Word breaking rules |
useOverflowWrapUtility | overflow-wrap | Word wrapping |
useHyphensUtility | hyphens | Hyphenation |
useListStyleUtility | list-style-type | List markers |
useContentUtility | content | Generated content |
Examples
Text Truncation
import { styleframe } from "styleframe";
import { useLineClampUtility, useTextOverflowUtility } from "@styleframe/theme";
import { useOverflowUtility, useWhitespaceUtility } from "@styleframe/theme";
const s = styleframe();
// Multi-line truncation
useLineClampUtility(s, {
'2': '2',
'3': '3',
});
// Single-line truncation
useTextOverflowUtility(s, { ellipsis: 'ellipsis' });
useOverflowUtility(s, { hidden: 'hidden' });
useWhitespaceUtility(s, { nowrap: 'nowrap' });
export default s;
._line-clamp\:2 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
._line-clamp\:3 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
._text-overflow\:ellipsis { text-overflow: ellipsis; }
._overflow\:hidden { overflow: hidden; }
._whitespace\:nowrap { white-space: nowrap; }
Usage in HTML:
<!-- Multi-line truncation (3 lines) -->
<p class="_line-clamp:3">
Long text that will be truncated after three lines with an ellipsis...
</p>
<!-- Single-line truncation -->
<p class="_overflow:hidden _whitespace:nowrap _text-overflow:ellipsis">
Long text on a single line that will be truncated with an ellipsis...
</p>
Heading Hierarchy
import { styleframe } from "styleframe";
import { useFontSizeUtility, useFontWeightUtility, useLineHeightUtility, useLetterSpacingUtility } from "@styleframe/theme";
const s = styleframe();
useFontSizeUtility(s, {
base: '1rem',
lg: '1.125rem',
xl: '1.25rem',
'2xl': '1.5rem',
'3xl': '1.875rem',
'4xl': '2.25rem',
});
useFontWeightUtility(s, {
normal: '400',
medium: '500',
semibold: '600',
bold: '700',
});
useLineHeightUtility(s, {
tight: '1.25',
normal: '1.5',
});
useLetterSpacingUtility(s, {
tight: '-0.025em',
normal: '0',
});
export default s;
._font-size\:base { font-size: 1rem; }
._font-size\:lg { font-size: 1.125rem; }
._font-size\:xl { font-size: 1.25rem; }
._font-size\:2xl { font-size: 1.5rem; }
._font-size\:3xl { font-size: 1.875rem; }
._font-size\:4xl { font-size: 2.25rem; }
._font-weight\:normal { font-weight: 400; }
._font-weight\:medium { font-weight: 500; }
._font-weight\:semibold { font-weight: 600; }
._font-weight\:bold { font-weight: 700; }
._line-height\:tight { line-height: 1.25; }
._line-height\:normal { line-height: 1.5; }
._letter-spacing\:tight { letter-spacing: -0.025em; }
._letter-spacing\:normal { letter-spacing: 0; }
Usage in HTML:
<h1 class="_font-size:4xl _font-weight:bold _line-height:tight _letter-spacing:tight">
Page Title
</h1>
<h2 class="_font-size:2xl _font-weight:semibold _line-height:tight">
Section Heading
</h2>
<p class="_font-size:base _font-weight:normal _line-height:normal">
Body text with comfortable reading line height.
</p>
Best Practices
- Use a consistent type scale: Define a modular scale for font sizes
- Pair font size with line height: Larger text needs tighter line height
- Consider accessibility: Ensure minimum font sizes and sufficient contrast
- Use relative units: Prefer rem over px for better accessibility
- Limit font weights: Most designs need only 3-4 weights (normal, medium, semibold, bold)
- Test line clamping: Truncation with ellipsis needs testing with various content lengths
FAQ
rem respects user browser settings for font size. Users who need larger text can adjust their browser's base font size, and rem-based sizes scale accordingly. This improves accessibility.overflow: hidden, white-space: nowrap, and text-overflow: ellipsis. For multi-line truncation, use the line-clamp utilities which handle the complex WebKit box display properties.