CSS Clamp

Examples

Fluid values in real layouts

Each example defines a mobile and a desktop size and lets clamp() fill in everything between 360px and 1440px. Pick a screen width to see the result at that size.

Fluid type scale

Headings h1 to h6 and body text. Large headings change a lot between mobile and desktop, body text barely moves.

1440px
h1

Fluid type

64.0px
h2

Section title

48.0px
h3

Subsection

36.0px
h4

Card heading

28.0px
h5

Small heading

22.0px
h6

Label

18.0px
p

Body text scales a little, so it stays comfortable to read on every screen.

18.0px
ElementMobileDesktopclamp()
h136px64px
clamp(2.25rem, 1.667rem + 2.593vw, 4rem)
h230px48px
clamp(1.875rem, 1.5rem + 1.667vw, 3rem)
h324px36px
clamp(1.5rem, 1.25rem + 1.111vw, 2.25rem)
h420px28px
clamp(1.25rem, 1.083rem + 0.741vw, 1.75rem)
h518px22px
clamp(1.125rem, 1.042rem + 0.37vw, 1.375rem)
h616px18px
clamp(1rem, 0.958rem + 0.185vw, 1.125rem)
p16px18px
clamp(1rem, 0.958rem + 0.185vw, 1.125rem)

Copy the whole scale

Custom properties that scale from 360px to 1440px. Using Tailwind CSS v4? Put them in @theme instead of :root and use text-h1, text-p and so on.

:root {
    --text-h1: clamp(2.25rem, 1.667rem + 2.593vw, 4rem);
    --text-h2: clamp(1.875rem, 1.5rem + 1.667vw, 3rem);
    --text-h3: clamp(1.5rem, 1.25rem + 1.111vw, 2.25rem);
    --text-h4: clamp(1.25rem, 1.083rem + 0.741vw, 1.75rem);
    --text-h5: clamp(1.125rem, 1.042rem + 0.37vw, 1.375rem);
    --text-h6: clamp(1rem, 0.958rem + 0.185vw, 1.125rem);
    --text-p: clamp(1rem, 0.958rem + 0.185vw, 1.125rem);
}

Dashboard layout

The striped areas are the page gutters on both sides of the content. They grow with the screen together with the gaps and the card padding, so the dashboard feels roomy on a desktop and never wastes space on a phone.

1440px
Page gutter16px → 64px
64.0px
Gap12px → 32px
32.0px
Card padding16px → 28px
28.0px

Copy the layout tokens

Three values do all the work: the gutter on both sides of the page, the gap between blocks and the padding inside cards.

:root {
    --page-gutter: clamp(1rem, 0rem + 4.444vw, 4rem);
    --layout-gap: clamp(0.75rem, 0.333rem + 1.852vw, 2rem);
    --card-padding: clamp(1rem, 0.75rem + 1.111vw, 1.75rem);
}

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

.grid {
    display: grid;
    gap: var(--layout-gap);
}

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