clamp in CSS

clamp in CSS is a function that lets a value grow and shrink between a minimum and maximum limit. It is commonly used for fluid typography, responsive spacing, flexible widths, and layouts that should adapt smoothly without writing many media queries.

The main idea is simple: give the browser a smallest allowed value, a preferred flexible value, and a largest allowed value. The browser picks the preferred value when it fits, but never lets the final value go below the minimum or above the maximum.

Basic clamp Syntax

The syntax is clamp(minimum, preferred, maximum). The middle value is usually flexible, often using viewport units or a calculation.

h1 {
  font-size: clamp(2rem, 5vw, 4rem);
}

The heading will never be smaller than 2rem and never larger than 4rem. Between those limits, it can scale with 5vw. This creates fluid typography without separate font-size rules at every breakpoint.

How clamp Chooses the Value

The browser compares the three values. If the preferred value is smaller than the minimum, the minimum wins. If the preferred value is larger than the maximum, the maximum wins. Otherwise, the preferred value is used.

ConditionFinal Value
preferred is below minimumminimum
preferred is between limitspreferred
preferred is above maximummaximum

This makes clamp useful when you want flexibility but still need safe boundaries. The value can respond to the screen while staying readable and controlled.

Fluid Typography with clamp

Typography is the most common use for clamp. Large headings should not be tiny on desktop or enormous on mobile. clamp solves that with one rule.

.hero-title {
  font-size: clamp(2.25rem, 7vw, 5.5rem);
  line-height: 1.05;
}

The title starts at a readable mobile size, grows with viewport width, and stops at a maximum size. The line height stays tight enough for a display heading.

Responsive Spacing

clamp is also useful for section padding and layout gaps. It can make spacing compact on mobile and more open on desktop.

.section {
  padding-block: clamp(2rem, 6vw, 6rem);
}

.grid {
  gap: clamp(1rem, 3vw, 2.5rem);
}

This avoids a pile of breakpoint-specific spacing values. The design breathes more on wide screens but does not waste space on small screens.

Using rem and vw Together

Good clamp values often combine relative typography units and viewport units. A minimum and maximum in rem keeps the value connected to user font settings, while the preferred vw value creates fluid scaling.

p {
  font-size: clamp(1rem, 1.4vw, 1.25rem);
}

This paragraph text can scale slightly, but it remains within a safe readable range. Body text should not grow as aggressively as hero headings.

clamp with calc

The preferred value can use calc(). This is useful when a simple viewport unit scales too quickly or too slowly.

h2 {
  font-size: clamp(1.75rem, calc(1.2rem + 2vw), 3rem);
}

The fixed rem part gives a stable base, and the viewport part adds fluid growth. This often feels smoother than using only vw.

Fluid Widths

clamp can control widths too. A component can have a minimum useful width, a preferred fluid width, and a maximum width.

.panel {
  width: clamp(280px, 80vw, 720px);
}

The panel is never narrower than 280px and never wider than 720px. Between those limits, it uses 80 percent of the viewport width.

clamp vs Media Queries

clamp does not replace all media queries. It is best for values that can scale smoothly, such as font size, spacing, width, and gaps. Media queries are still better when the layout structure must change, such as switching from one column to three columns.

NeedBetter Tool
Fluid heading sizeclamp
Fluid section paddingclamp
Switch layout columnsmedia query
Hide or show navigation patternmedia query
Limit flexible widthclamp

Accessibility and Readability

clamp should not be used to create text that becomes too small. A minimum value exists for a reason. Keep body text readable and respect user font-size preferences by using rem-based limits.

Avoid using pure viewport units for important text without limits. A value like 5vw can become too small on narrow screens or too large on wide screens. clamp gives that flexible value guardrails.

Building a Fluid Type Scale

A type scale is the relationship between body text, headings, labels, and display text. clamp can make the scale fluid while keeping each level controlled. Body text can change slightly, while display headings can scale more dramatically.

:root {
  --step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --step-1: clamp(1.25rem, 1rem + 1vw, 1.75rem);
  --step-2: clamp(1.75rem, 1.2rem + 2.5vw, 3rem);
  --step-3: clamp(2.25rem, 1.4rem + 4vw, 5rem);
}

body { font-size: var(--step-0); }
h2 { font-size: var(--step-2); }

This keeps typography consistent across the site. Instead of choosing random font sizes for every component, the design uses named steps that scale within safe limits.

clamp with CSS Variables

clamp becomes easier to maintain when repeated values are stored in custom properties. This is useful for design systems where spacing and typography need a shared rhythm.

:root {
  --space-fluid: clamp(1rem, 3vw, 2.5rem);
}

.card {
  padding: var(--space-fluid);
}

If the spacing needs to change later, the variable can be updated once. Every component using it receives the new scale.

clamp for Line Length

Readable text should not stretch endlessly on wide screens. clamp can help control content width while still allowing smaller screens to use available space.

.article {
  width: clamp(18rem, 70vw, 46rem);
  margin-inline: auto;
}

The article stays usable on narrow screens, grows on medium screens, and stops before line length becomes uncomfortable. This is useful for blog posts, documentation, and tutorials.

min, max and clamp

CSS also has min() and max(). Use min() when you only need an upper limit. Use max() when you only need a lower limit. Use clamp() when you need both limits and a flexible preferred value.

FunctionUse Case
min()choose the smaller of values
max()choose the larger of values
clamp()keep a preferred value between two boundaries

For example, a container can use min(), while a heading size often uses clamp(). Choosing the simplest function keeps the CSS easier to read.

Practical clamp Checklist

  • Use clamp when a value should scale smoothly.
  • Set readable minimums for text.
  • Set sensible maximums for large screens.
  • Use calc in the preferred value when scaling needs tuning.
  • Store repeated clamp values in custom properties.
  • Use media queries when the layout structure changes.

Real Example: Responsive Card

A card component can use clamp for padding, gap, and title size. This keeps the card compact in narrow columns and more spacious when it appears in a wider layout.

.feature-card {
  padding: clamp(1rem, 3vw, 2rem);
  gap: clamp(0.75rem, 2vw, 1.5rem);
}

.feature-card h3 {
  font-size: clamp(1.25rem, 1rem + 1vw, 2rem);
}

The card does not need a breakpoint just to adjust spacing. If the card layout changes from one column to two columns, that structural change can still happen with a media query.

clamp for Layout Safety

clamp can prevent flexible elements from becoming unusable. A search box, modal, or sidebar can be fluid while still keeping practical limits.

.search-panel {
  width: clamp(18rem, 60vw, 42rem);
}

The panel is not too narrow for form controls and not too wide for comfortable scanning. This is especially useful for overlays, drawers, and centered UI components.

When clamp Becomes Hard to Read

clamp values can become difficult when the preferred value contains a long formula. If a teammate cannot quickly understand why the numbers exist, store the value in a custom property or add a short comment in the design system documentation.

The goal is not to replace every breakpoint with a math expression. The goal is to reduce unnecessary breakpoint rules while keeping the CSS understandable.

clamp for Design Tokens

Design tokens can use clamp for values that should adapt across the whole site. This works well for section spacing, page gutters, card padding, and heading sizes.

:root {
  --page-gutter: clamp(1rem, 4vw, 3rem);
  --section-gap: clamp(2rem, 7vw, 7rem);
}

.page {
  padding-inline: var(--page-gutter);
}

The page gutter now grows with the viewport but remains within usable limits. This keeps narrow screens compact and wide screens balanced.

Testing clamp Values

Test clamp values at narrow, medium, and wide widths. A value can look good at mobile and desktop sizes but feel awkward in the middle. The preferred value controls that middle range, so tune it carefully.

Debugging clamp

When a clamp value seems wrong, inspect the computed value in browser developer tools. The browser will show the final calculated size. Then resize the viewport and watch where the value reaches its minimum and maximum.

  • Check that the minimum is not larger than the maximum.
  • Check whether the preferred value can actually change.
  • Use rem limits for text readability.
  • Use calc in the preferred value for smoother scaling.
  • Do not use clamp when a structural media query is needed.

Common clamp Mistakes

  • Using a minimum that is still too small for readable text.
  • Expecting clamp to change layout structure.
  • Using only viewport units for body text without safe limits.
  • Making the preferred value too aggressive.
  • Forgetting that clamp chooses one final computed value.
  • Using clamp everywhere instead of where fluid scaling helps.

clamp in CSS FAQ

What does clamp do in CSS?

clamp chooses a value between a minimum and maximum, using a preferred value when it is inside that range.

Is clamp only for font size?

No. clamp can be used for font sizes, spacing, widths, gaps, and other numeric CSS values.

Does clamp replace media queries?

No. clamp is good for fluid values, while media queries are still useful for structural layout changes.

Can clamp use calc?

Yes. The preferred value inside clamp often uses calc for smoother responsive scaling.


Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.