Responsive Layout with rem and clamp

Published on
May 13, 2025
Topic
Development

Modern front-end development demands flexibility. Designers often provide just three layouts for screen resolutions — for example, 1440px (desktop), 768px (tablet), and 375px (mobile). And that’s completely fine — it saves time on design, and it’s up to the developer to ensure the site looks great across all devices.

This is where CSS rem units and the clamp() function shine. They allow you to create a fluid layout that smoothly scales between breakpoints, while still maintaining control at both the low and high ends of the screen width spectrum.

Why rem + clamp() Makes Sense

Using rem and clamp() offers multiple advantages:

  • Fluid scaling between breakpoints without sudden jumps
  • Controlled behavior on large screens, e.g., lock styles after 1920px
  • Minimal design assets needed — just 3 breakpoints can be enough
  • Accessibility-friendly: rem-based values respect browser font settings

Why This Is Better Than Other Units

Pixels (px)
  • Completely static — doesn’t respond to screen size
  • Poor accessibility — doesn’t scale well with user preferences
  • Requires lots of media queries to adapt layouts
Ephemeral unit (em)
  • Relative to the parent element — can lead to unexpected results in nested components
  • Harder to maintain at scale
Viewport units (vw)
  • Scales well, but can stretch or shrink excessively on very small or large screens
  • Doesn’t respect browser text size settings — problematic for accessibility

Rem + clamp()

  • Clean, predictable behavior
  • Smooth scaling between defined breakpoints
  • Easy to apply based on a few fixed designs
  • Supports user font preferences out of the box
Code Examples
  • 14px: minimum font size on small screens
  • 1vw + 0.5rem: fluid growth depending on screen width
  • 18px: maximum font size on large screens
html {
  font-size: clamp(14px, 1vw + 0.5rem, 18px);
}

The rem + clamp() Formula

To create fluid sizing between two resolutions, use:

clamp(MIN, CALC, MAX)

Where:
  • MIN = value at smallest breakpoint (e.g., 375px)
  • MAX = value at largest breakpoint (e.g., 1440px or 1920px)
  • CALC = fluid value using this formula:
Real Example: Font scaling from 14px (375px) to 18px (1440px):
font-size: 
clamp(14px, calc((18 - 14) / (1440 - 375) * 100vw + 14px), 18px);

How to Use It in a Real Project

1. Set your root font-size using clamp:
html {
  font-size: clamp(14px, 1vw + 0.5rem, 18px);
}

2. Set your root font-size using clamp:
h1 {
  font-size: 2rem;
}

.container {
  padding: 2rem;
}

3. Avoid px unless necessary, e.g., for borders or icons.

Bonus: Accessibility Gains

Using rem instead of px ensures your layout respects user-defined font sizes — a key feature for users with visual impairments or custom browser settings. The entire UI will scale consistently and accessibly, without breaking your design.

This is especially helpful for users who rely on browser zooming, OS-level scaling, or high-contrast modes.


Final Thoughts

Building responsive layouts using rem and clamp() is fast, flexible, and reliable. With just three design breakpoints, you can cover virtually the entire screen spectrum, reduce the need for media queries, and make your site more accessible and user-friendly.
Written by
Alex Gudaev —
May 2025

Other notes