
The CSS Box Model: Content, Padding, Borders, and Margin
11/10/2019
CSS Table Properties: Layout, Borders, Captions, and Spacing
12/10/2019
CSS gives you three ways to size a box along each axis: request a size, set a lower limit, or cap the result. Combine them when a layout must adapt to both narrow and wide containers.
10. Width, height, and size limits
| Property | Description | Values |
|---|---|---|
| width | Sets the element’s width | Length or percentage, auto, initial, inherit |
| height | Sets the element’s height | Length or percentage, auto, initial, inherit |
| max-width | Caps the element’s width | Length or percentage, none, initial, inherit |
| min-width | Sets the element’s minimum width | Length or percentage, 0, initial, inherit |
| max-height | Caps the element’s height | Length or percentage, none, initial, inherit |
| min-height | Sets the element’s minimum height | Length or percentage, 0, initial, inherit |
10.1. What do width and height control?
width and height set dimensions. Under box-sizing: content-box they apply to the content; with border-box they include padding and border. The default auto allows the layout or the element’s intrinsic size to determine the result.
10.2. Using width and height
Use a fixed length for a deliberate size, and a percentage when the size should follow its containing block. Percentage heights generally need a definite containing-block height. For an image with one dimension set, leave the other at auto to preserve its aspect ratio.
Set a fixed width of 200px and a fixed height of 100px:
div {
width: 200px;
height: 100px;
}For a box that follows its containing block, use 50% of its width and 25% of its height:
div {
width: 50%;
height: 25%;
}10.3. Setting a maximum
max-width and max-height cap the used dimensions. For a responsive image, max-width: 100% prevents the image from exceeding the available width without forcing it to fill that width. A definite percentage max-height also depends on the containing block’s height.
A maximum height can also limit a drop-down menu or a panel whose content length varies. If the contents can exceed the limit, decide how they should scroll or wrap.
10.4. Setting a minimum
min-width and min-height let a box grow while ensuring it never falls below a chosen size. A minimum wins when it exceeds the maximum. Use a minimum height instead of a fixed height when longer content should expand the box.
10.5. Combining the properties
Limit an image to 300px wide and 200px tall:
img {
max-width: 300px;
max-height: 200px;
}Limit an image to 50% of the containing block’s width and 25% of its height:
img {
max-width: 50%;
max-height: 25%;
}Set a maximum width on a div:
div {
max-width: 300px;
}Set a minimum height on a div:
div {
min-height: 100px;
}Practical example: a responsive wrapper
This centered wrapper takes up 95% of its containing block until it reaches 1,200px. On a narrow screen, it shrinks without needing a separate breakpoint.
.content {
width: 95%;
max-width: 1200px;
margin: 0 auto;
}Try the responsive wrapper in CodePen
Try it yourself
How would you keep a card at least 10rem tall and still let it grow with its text?
Show one solution
.card { min-height: 10rem; }