Sub topic 10
Constraining size with min and max dimensions
This chapter assumes you know how width, height, padding, and border combine into a box, and what box-sizing changes. It builds on both.
The working model
Setting width or height asks for a size. The min- and max- properties set limits on that size. The browser computes the size you asked for and then clamps it to the allowed range.
| Property | Default | Effect |
|---|---|---|
| `min-width` | `auto` (acts as 0 for most block boxes) | The box never gets narrower than this |
| `max-width` | `none` | The box never gets wider than this |
| `min-height` | `auto` (acts as 0 for most block boxes) | The box never gets shorter than this |
| `max-height` | `none` | The box never gets taller than this |
The rule is: used size = the requested size, raised to the minimum if it is smaller, then lowered to the maximum if it is larger. When the minimum is larger than the maximum, the minimum wins.
The diagram applies the maximum first and the minimum last. This is why the minimum wins in a conflict.
A fluid but bounded container
A percentage width makes a box fluid. The limits keep it readable on very wide and very narrow screens.
.card {
width: 90%;
max-width: 40rem;
min-width: 16rem;
margin: 0 auto;
}On a 1200px viewport, 90% would be 1080px. The box stops at 40rem (640px at the default font size). On a 200px viewport, 90% would be 180px, but the box stays at 16rem and may overflow its parent.
Limits follow box-sizing
Minimum and maximum values apply to the same box that width applies to. With box-sizing: content-box, max-width: 40rem limits the content area, so padding and border add to the final size. With border-box, the limit includes padding and border.
*,
*::before,
*::after {
box-sizing: border-box;
}
.panel {
max-width: 30rem;
padding: 1.5rem;
border: 2px solid #333;
}Here the border box can never exceed 30rem. Under content-box it could reach 30rem plus 3rem of padding plus 4px of border.
Images and media
Replaced elements such as images have an intrinsic size. A common pattern stops them overflowing their container without enlarging them:
img {
max-width: 100%;
height: auto;
}max-width: 100% caps the image at the container's width. height: auto keeps the aspect ratio as the width shrinks. A small image stays at its natural size, which width: 100% would not do.
Common errors
- Using
heightwheremin-heightis needed. A fixedheightdoes not grow with its content, so text can spill out of the box.min-heightgives a floor and lets the box grow. - Percentage heights that do nothing.
min-height: 100%andheight: 50%only work when the parent has a definite height. Usemin-height: 100vhto fill the viewport. - Expecting
max-widthto beatmin-width. Ifmin-width: 500pxandmax-width: 300pxare both set, the box is 500px wide. - Forgetting that
max-heightcan hide content. A box withmax-height: 10remand content taller than that overflows. What you see then depends on theoverflowproperty, which a later chapter covers. - Putting
max-widthon inline elements. Plain inline elements ignore width and height, so their limits do nothing. Changedisplaytoblockorinline-blockfirst.
Exercise
A .box has box-sizing: border-box; width: 600px; max-width: 400px; min-width: 450px; padding: 20px; border: 5px solid;. What is its border-box width, and what is its content width?
Show answer
The border-box width is 450px. The requested 600px is first capped to 400px by max-width, then raised to 450px by min-width, because the minimum wins. Under border-box the limits include padding and border, so the content width is 450 - 40 - 10 = 400px.