Set dimensions, constraints, and aspect ratios.
A card should fit its container and leave room for its content. Use widths, heights, and size limits to control both.
For an ordinary block in normal flow, width: auto uses the available horizontal space after accounting for margins, padding, and borders. height: auto usually grows with the content.
.card {
box-sizing: border-box;
width: auto;
height: auto;
padding: 1rem;
border: 2px solid green;
}The card can become taller when its text wraps onto more lines. An explicit height would limit that space and could leave content overflowing.
Keep the card’s padding, border, and box sizing. Change its width and add the two constraints below.
.card {
width: 90%;
max-width: 40rem;
margin: 0 auto;
}Here the card’s containing block is the space inside its parent’s padding. The parent’s content width supplies the reference for the percentage. The card takes 90% of that width, capped at 40rem.
If the root font size is 16 pixels:
Because the card is narrower than its container, the left and right auto margins share the remaining horizontal space and center it. Its border-box setting keeps padding and borders inside these widths.
The sizing properties play different roles:
| Property | Role |
|---|---|
width, height | Request a size. |
min-width, min-height | Establish a lower limit. |
max-width, max-height | Establish an upper limit. |
For a welcome panel that should have some height but grow with its text:
.welcome {
min-height: 12rem;
padding: 1rem;
}With automatic height, the panel can expand beyond its minimum. By contrast, max-height alone does not create scrolling when content is taller; overflow handling is a separate decision.
Choose limits that can fit the available space. A min-width: 500px box can overflow a 320-pixel container. If a minimum and maximum conflict, the minimum wins in ordinary sizing.
In a normal block layout, a percentage height usually works when the containing block has a known height:
.preview {
height: 300px;
}
.preview-panel {
height: 50%;
}For a panel directly inside this preview, the height is 150 pixels. If the preview instead has an automatic height that depends on its content, the child's height: 50% generally behaves as auto. A percentage is useful only when its reference can be resolved.
aspect-ratioFor a separate, empty preview box:
.preview {
width: 320px;
max-width: 100%;
height: auto;
aspect-ratio: 16 / 9;
background-color: honeydew;
}At 320 pixels wide, the box aims for a height of 180 pixels. Leave at least one dimension automatic so aspect-ratio can calculate it. Content and size limits can change the final shape.
The card above requests 90% width and is capped at 40rem. If the root font size is 16 pixels and its container is 600 pixels wide, first find 90%: 600 × 0.9 = 540px. Does the 640-pixel cap change that result?
No. 540 pixels is below the 640-pixel maximum, so the card is 540 pixels wide.
Now calculate its width in an 800-pixel container. Explain which constraint wins.
90% is 720 pixels, so max-width caps the card at 640 pixels. The remaining space is shared by the automatic horizontal margins.
aspect-ratio supplies a preferred relationship when a dimension is automatic.Try it yourself
HTML + CSS