
How to Create CSS Tooltips
09/08/2023
CSS line-clamp: Limit Text to a Set Number of Lines
04/05/2024
Text that adapts to the screen size is known as fluid typography or responsive typography. On the web, fluid typography helps us communicate clearly with readers. Text needs to remain legible across devices. In this guide, you’ll learn to use CSS clamp() to make text adapt without defining a separate font size at several breakpoints.
1. Fluid typography and media queries
Web designers have traditionally used media queries to adjust font size to the width of the device. With fixed sizes at successive breakpoints, the text can jump abruptly when the viewport crosses a breakpoint. Fluid typography solves that problem by changing the font size gradually as the viewport gets wider or narrower, making the transition more consistent for readers.
2. Relative units for responsive text
To create responsive typography, use relative units such as em and vh and vw where the size needs to follow its context. Font-relative units follow font size, while viewport units follow the available viewport. A value in pixels stays fixed unless another rule changes it.
For example, you can use the relative unit vw (viewport width) with clamp(). A declaration of font-size: 5vw; sets the font size to 5% of the viewport width.
3. Use CSS clamp() to adapt text to the viewport
The CSS clamp() function lets us set a range of acceptable font sizes. It takes a minimum, a preferred value that can vary with the viewport, and a maximum.
Syntax: The three arguments are separated by commas.
property: clamp(minimum, preferred, maximum);
- Property: The property that receives the result of
clamp(). For typography, usefont-size. - Minimum: The lower limit. If the preferred size falls below it, the browser uses the minimum.
- Preferred: The value the browser uses when it falls within the range. It can use relative units such as
vwor expressions that include font-relative units such asem. That lets the font size respond to the viewport or its typographic context. - Maximum: The upper limit. If the preferred value exceeds it, the browser uses the maximum.
Example
The example keeps the font size between 1em and 2.5em and computes its preferred size from the viewport width. The middle argument uses the viewport unit vw.
A value of 100vw is the full viewport width. At a viewport width of 1,000px, 3vw is 30px. At 700px, it is 21px. The rendered font uses that preferred value when it stays within the lower and upper limits.
CSS
h2 {
font-size: clamp(1em, 3vw, 2.5em);
}
CodePen example
4. Improve fluid typography with CSS calc()
Another way to build fluid typography is to use calc() so the browser calculates the size from multiple values. For example, combine a base size with a relative unit so part of the resulting size follows the viewport.
In the example, the font size starts from a base of 16px and adds 0.5% of the viewport’s width.
font-size: calc(16px + 0.5vw);
5. Use clamp() with calc() to set limits
Combining calc() and clamp() gives you finer control over CSS values. You might calculate a width, margin, padding, or font size from several inputs and then limit the result to your design’s usable range.
/* Combine calc() and clamp() to size text. */
.text {
font-size: clamp(12px, calc(1em + 3vw), 36px);
}
Here the preferred value is 1em + 3vw, and the result stays between 12px and 36px. The font grows as the viewport widens while the preferred value is within those limits. Once the preferred value reaches 36px, the size stays at that maximum.
6. Browser support for CSS clamp()
Check whether the browsers your visitors use support the CSS features you choose. Can I Use lists support for clamp(), including older browser versions.
A feature query tests whether the browser supports a CSS declaration before applying rules that rely on it. Start with a fallback so the text also has a usable size in browsers that do not support the function.
h2 {
font-size: 1.5em;
}
@supports (font-size: clamp(1em, 3vw, 2.5em)) {
h2 {
font-size: clamp(1em, 3vw, 2.5em);
}
}
In this example, the heading uses the fallback size unless the @supports test passes. You can also express the same bounds with max(1em, min(3vw, 2.5em)) when both functions are supported.
7. Online tools for fluid typography
These tools let you experiment with the minimum, preferred, and maximum values before committing them to a stylesheet.
- Fluidity Typography Editor by Adrian Bece: Set the minimum and maximum font sizes and adjust the fluid and relative values. The graphs and tables show how the font size changes at different viewport widths. The editor also generates CSS that you can copy into your stylesheet.
- Min-Max Value Interpolation by James Gilyead and Trys Mudford: Enter minimum and maximum font sizes and viewport widths. The tool shows a live text sample, which you can test by resizing the browser.
Fluid typography helps text scale smoothly across devices. Use relative units with calc() and clamp() to reduce the need for font-size breakpoints, then check how the result behaves on narrow screens and at higher zoom levels.