Syntax
calc(expression)
clamp(min, preferred, max)Examples
calc() for Mixed-Unit Math
Combining different units in a single calculation.
.sidebar-offset {
width: calc(100% - 250px); /* fills remaining space next to a fixed 250px sidebar */
}
.centered-minus-gap {
margin-top: calc(2rem + 10px);
}clamp() for Fluid Typography
The most common modern pattern - text that scales with the viewport but stays within safe bounds.
h1 {
font-size: clamp(1.75rem, 4vw + 1rem, 3.5rem);
/* never smaller than 1.75rem, never larger than 3.5rem,
scales fluidly with viewport width in between */
}Parameters and values
- calc() (function): Performs math, mixing different units
- min() (function): Uses the smallest of the given values
- max() (function): Uses the largest of the given values
- clamp(min, val, max) (function): Fluidly scales val, bounded between min and max
Best practices
- Use clamp() for fluid typography instead of setting different font-size values in multiple media queries
- Use calc() whenever you need to mix units, like subtracting a fixed sidebar width from a 100% fluid container
- Use min() to cap a fluid width so it never grows unreasonably large on very wide screens, without needing a max-width media query
- Remember these functions can be nested and combined for sophisticated responsive behavior entirely within CSS, with no JavaScript needed
At a glance
- Purpose
- Presentation and layout
- File extension
- .css
- Runs in
- Web browsers
- Usually used with
- HTML and JavaScript
Specifications & further reading
Related CSS documentation
Length Units: px, %, em, rem
CSS offers several length units. px is an absolute, fixed unit. % is relative to the parent element. em is relative to the current element's font-size (compounding when nested). rem is relative to the root (html) element's font-size, avoiding the compounding problem of em, and is generally preferred for consistent, predictable sizing.Viewport Units: vh, vw, dvh
Viewport units are relative to the browser window's dimensions rather than a parent element. vh and vw represent 1% of the viewport height and width respectively. The newer dvh (dynamic viewport height) solves the classic mobile browser problem where vh included space later covered by the address bar.Sizing: width, min/max-width
Beyond a fixed width, CSS offers min-width and max-width to set flexible boundaries - an element can shrink or grow within those limits. This is fundamental to responsive design, letting content adapt fluidly while never becoming unreadably narrow or unreasonably wide.Logical Properties
Logical properties describe direction in terms of writing mode (inline/block flow) rather than fixed physical directions (left/right/top/bottom). margin-inline-start behaves like margin-left in English but automatically adapts for right-to-left languages, making them essential for properly internationalized layouts.
CSS offers several length units. px is an absolute, fixed unit. % is relative to the parent element. em is relative to the current element's font-size (compounding when nested). rem is relative to the root (html) element's font-size, avoiding the compounding problem of em, and is generally preferred for consistent, predictable sizing.Viewport Units: vh, vw, dvh
Viewport units are relative to the browser window's dimensions rather than a parent element. vh and vw represent 1% of the viewport height and width respectively. The newer dvh (dynamic viewport height) solves the classic mobile browser problem where vh included space later covered by the address bar.Sizing: width, min/max-width
Beyond a fixed width, CSS offers min-width and max-width to set flexible boundaries - an element can shrink or grow within those limits. This is fundamental to responsive design, letting content adapt fluidly while never becoming unreadably narrow or unreasonably wide.Logical Properties
Logical properties describe direction in terms of writing mode (inline/block flow) rather than fixed physical directions (left/right/top/bottom). margin-inline-start behaves like margin-left in English but automatically adapts for right-to-left languages, making them essential for properly internationalized layouts.