Sub topic 1
Intrinsic CSS Layouts with `min()`, `max()`, and `clamp()`
Prerequisites
Assume you know basic CSS syntax, the cascade, the box model, common length units, and basic CSS Grid.
Why intrinsic layouts: fewer breakpoints, more flexibility
An intrinsic layout responds to the space available instead of switching only at fixed viewport widths. A media query says, “At this width, change the layout.” An intrinsic rule says, “Choose a suitable size whenever the available space changes.”
This reduces breakpoint-specific rules. It also handles intermediate widths, zoom, split-screen windows, and different device sizes more naturally.
A quick refresher on length units
pxis an absolute CSS pixel.remis relative to the root element's font size.%is relative to a containing value.vwis one percent of the viewport width.emis relative to the current element's font size.
Relative units are useful for fluid layouts because they respond to context. Combining units often gives a value that is both flexible and bounded.
Math in CSS: `calc()` and the rules for operators
CSS math functions calculate values during layout. calc() can combine compatible units:
.panel {
width: calc(100% - 2rem);
padding: calc(1rem + 1vw);
}Put spaces around + and -. Multiplication and division use * and /, and one side must be a unitless number. Modern min(), max(), and clamp() expressions can often contain calculations directly.
> callout note > CSS resolves these expressions after it knows the relevant container or viewport size. The result is one computed value for the property.
`min()`: capping a value
min() returns the smallest argument. It creates an upper bound:
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}The container uses the available width minus gutters until it reaches 70rem. It then stops growing.
`max()`: setting a floor
max() returns the largest argument. It creates a lower bound:
.card {
width: max(50%, 18rem);
}The card is at least 18rem wide, but can grow to half its container. Check narrow containers carefully: a floor can cause overflow if the surrounding layout cannot provide that space.
Exercise
Which function should you use when a value must not exceed a maximum?
Show answer
Use min(), because it returns the smaller value.