Let’s take a closer look at how you can use clamp() for different types of text elements.
1. Body Text
For body text, you’ll want a comfortable font size that’s easy to read on all screen sizes. Using clamp(), you can ensure it’s responsive:
body {
font-size: clamp(14px, 2vw + 1rem, 18px);
}
In this example:
- The minimum size is
14px, making sure the text doesn’t get too small.
- The preferred size is calculated as
2vw + 1rem, which makes the font size responsive while considering the viewport width and a base font size.
- The maximum size is
18px so that it doesn't get too large on wide screens.
2. Heading Sizes
For headings, you may want the font size to scale more significantly. Here's an example for a heading:
h1 {
font-size: clamp(32px, 6vw, 72px);
}
With this, the h1 tag will scale fluidly from 32px to 72px, with a preferred scaling factor of 6vw, so it looks great across all screen sizes.
3. Fine-tuning Font Sizes
You can fine-tune font sizes based on design needs. For example, a smaller heading might need less scaling:
h2 {
font-size: clamp(24px, 4vw, 48px);
}
This ensures that the h2 font size is always between 24px and 48px, depending on the screen size.