Box model
The properties that decide whether a box is generated, how big it is, the space inside and outside its border, and what happens to content that does not fit. Borders are under Borders, corners & outlines; the flow-relative sizes (inline-size, block-size, margin-inline and so on) are under Writing modes & logical.
Display & visibility
display
Sets the box's outer display type (how it takes part in its parent's layout) and its inner display type (how its own children are laid out), or removes the box altogether.
| Syntax | [ <display-outside> || <display-inside> ] | <display-listitem> | <display-internal> | <display-box> | <display-legacy> | grid-lanes | inline-grid-lanes | <display-outside> || [ <display-inside> | math ] |
|---|---|
| Initial | inline |
| Applies to | All elements |
| Inherited | No |
| Animation | Discrete; can transition to and from none with transition-behavior: allow-discrete |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-display-4 |
| Value | Effect |
|---|---|
block | A block-level box on its own line, laid out with normal flow inside. |
inline | Flows inline with text; width, height and vertical margins do not apply. |
inline-block | Flows inline but is sized like a block, accepting width, height and all margins. |
flex / inline-flex | A flex container. See Flexbox. |
grid / inline-grid | A grid container. See Grid. |
flow-root | A block that starts a new block formatting context, so it contains its floats and its children's margins do not collapse through it. |
list-item | A block box that also generates a ::marker. |
table, table-row, table-cell … | The table layout model and its internal parts. |
ruby, ruby-text | Ruby annotation layout. |
contents | The element generates no box of its own; its children take its place. |
none | The element and its descendants generate no boxes at all. |
The two-value syntax names the outer and inner types separately: display: inline flex is the same as inline-flex, and display: block flow-root the same as flow-root.
visibility
Hides a box while keeping its space in the layout. Unlike display: none, a hidden box still takes up room, and a descendant can set visibility: visible to show itself inside a hidden parent.
| Syntax | visible | hidden | collapse |
|---|---|
| Initial | visible |
| Applies to | All elements |
| Inherited | Yes |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-display-4 |
collapse removes a table row, column or group from the table as well as hiding it; on other elements it behaves like hidden.
Sizing
box-sizing
Chooses which box width and height measure: the content box alone, or the content plus its padding and border.
| Syntax | content-box | border-box |
|---|---|
| Initial | content-box |
| Applies to | All elements that accept width or height |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 10 · Firefox 29 · Safari 5.1 |
| Spec | css-sizing-3 |
*, *::before, *::after { box-sizing: border-box; }
width, height
The preferred size of the box in each physical axis. A <box-size> is a non-negative length or percentage, min-content, max-content, fit-content(<length-percentage>) or calc-size(); auto lets layout decide.
| Syntax | auto | <box-size> | <anchor-size()> |
|---|---|
| Initial | auto |
| Applies to | All elements except non-replaced inline boxes |
| Inherited | No |
| Percentages | Relative to the width (for width) or height (for height) of the containing block |
| Animation | By computed value type |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-sizing-3 |
A percentage height only resolves when the containing block has a definite height; otherwise it behaves as auto.
min-width, min-height
A lower limit on the box's size. A minimum always wins: it overrides both width/height and the matching max-*.
| Syntax | auto | <box-size> | <anchor-size()> |
|---|---|
| Initial | auto |
| Applies to | All elements that accept width or height |
| Inherited | No |
| Percentages | Relative to the width or height of the containing block |
| Animation | By computed value type |
| Support | min-width: Chrome 1 · Firefox 1 · Safari 1min-height: Chrome 1 · Firefox 3 · Safari 1.3 |
| Spec | css-sizing-3 |
auto computes to 0 for most boxes, but for flex and grid items it is the automatic minimum size, which stops an item shrinking below its content. Set min-width: 0 to let a flex item shrink past long text.
max-width, max-height
An upper limit on the box's size. It overrides width/height but loses to min-*.
| Syntax | none | <box-size> | <anchor-size()> |
|---|---|
| Initial | none |
| Applies to | All elements that accept width or height |
| Inherited | No |
| Percentages | Relative to the width or height of the containing block |
| Animation | By computed value type |
| Support | max-width: Chrome 1 · Firefox 1 · Safari 1max-height: Chrome 1 · Firefox 1 · Safari 1.3 |
| Spec | css-sizing-3 |
max-width: min(100%, 60rem);
aspect-ratio
A preferred width-to-height ratio, used to work out the box's size in one axis when only the other is known.
| Syntax | auto || <ratio> |
|---|---|
| Initial | auto |
| Applies to | All elements except inline boxes and internal ruby or table boxes |
| Inherited | No |
| Animation | By computed value |
| Support | Chrome 88 · Firefox 89 · Safari 15 |
| Spec | css-sizing-4 |
aspect-ratio: 16 / 9 gives a widescreen box; aspect-ratio: 1 a square. auto 4 / 3 uses a replaced element's natural ratio when it has one and falls back to 4:3.
frame-sizing
Lets an <iframe> size itself to the document it contains, so the embed grows to fit its content instead of scrolling.
| Syntax | auto | content-width | content-height | content-block-size | content-inline-size |
|---|---|
| Initial | auto |
| Applies to | Replaced elements |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 154 · Firefox no · Safari no |
| Spec | css-sizing-4 |
zoom
Scales an element and everything inside it, including its layout size, so surrounding content reflows around the zoomed box. Unlike transform: scale(), the box really takes up the larger or smaller space.
| Syntax | <number [0,∞]> | <percentage [0,∞]> |
|---|---|
| Initial | 1 |
| Applies to | All elements |
| Inherited | No (but the effect multiplies down the tree) |
| Animation | By computed value type |
| Support | Chrome 1 · Firefox 126 · Safari 3.1 |
| Spec | css-viewport |
Margin
margin
Shorthand for the four physical margins: the transparent space outside the border. One value sets all four sides; two set top/bottom then left/right; three set top, left/right, bottom; four go clockwise from the top.
| Syntax | <'margin-top'>{1,4} |
|---|---|
| Initial | 0 |
| Applies to | All elements except internal table elements, ruby base containers and ruby annotation containers |
| Inherited | No |
| Percentages | Refer to the logical width of the containing block, on every side |
| Animation | By computed value type |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-box-4 |
margin-inline: auto centres a sized block in its container; in a flex or grid item an auto margin absorbs the free space on that side. Adjacent vertical margins of blocks in normal flow collapse into the larger of the two.
margin-top, margin-right, margin-bottom, margin-left
The margin on one physical side. Negative values are allowed and pull neighbouring content closer.
| Syntax | <length-percentage> | auto | <anchor-size()> |
|---|---|
| Initial | 0 |
| Applies to | All elements except internal table elements, ruby base containers and ruby annotation containers |
| Inherited | No |
| Percentages | Refer to the logical width of the containing block |
| Animation | By computed value type |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-box-4 |
margin-trim
Discards the margins of a container's first and last children where they meet the container's edge, so the container's own padding sets the spacing.
| Syntax | none | block | [ block-start || block-end ] |
|---|---|
| Initial | none |
| Applies to | Block containers and multi-column containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 155 · Firefox no · Safari 16.4 |
| Spec | css-box-4 |
Padding
padding
Shorthand for the four physical paddings: the space between the border and the content. Takes one to four values in the same order as margin.
| Syntax | <'padding-top'>{1,4} |
|---|---|
| Initial | 0 |
| Applies to | All elements except internal table elements other than table cells, ruby base containers and ruby annotation containers |
| Inherited | No |
| Percentages | Refer to the logical width of the containing block, on every side |
| Animation | By computed value type |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-box-4 |
Padding can never be negative and never collapses. It takes the element's background.
padding-top, padding-right, padding-bottom, padding-left
The padding on one physical side.
| Syntax | <length-percentage [0,∞]> |
|---|---|
| Initial | 0 |
| Applies to | All elements except internal table elements other than table cells, ruby base containers and ruby annotation containers |
| Inherited | No |
| Percentages | Refer to the logical width of the containing block |
| Animation | By computed value type |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-box-4 |
Overflow
overflow
Shorthand for overflow-x and overflow-y: what happens when content is bigger than its box. One value sets both axes; two set x then y.
| Syntax | <'overflow-block'>{1,2} |
|---|---|
| Initial | visible |
| Applies to | Block containers, flex containers and grid containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 1 · Safari 1 |
| Spec | css-overflow-3 |
| Value | Effect |
|---|---|
visible | Content spills outside the box. |
hidden | Content is clipped with no scrollbars, but the box is still a scroll container and can be scrolled by script. |
clip | Content is clipped and the box cannot be scrolled at all, not even by script. |
scroll | Content is clipped and scrollbars are always shown. |
auto | Content is clipped and scrollbars appear only when needed. |
Any value other than visible or clip makes the box a scroll container, which starts a new block formatting context and becomes the reference for position: sticky.
overflow-x, overflow-y
Overflow handling in one physical axis. If one axis is visible or clip and the other is not, the visible one is treated as auto and clip as hidden.
| Syntax | visible | hidden | clip | scroll | auto |
|---|---|
| Initial | visible |
| Applies to | Block containers, flex containers, grid containers and table grid boxes |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 3.5 · Safari 3 |
| Spec | css-overflow-3 |
overflow-clip-margin
How far content may paint outside the box before it is clipped, when overflow is clip. The optional box keyword sets where the margin is measured from.
| Syntax | <visual-box> || <length [0,∞]> |
|---|---|
| Initial | 0px |
| Applies to | Boxes to which overflow applies |
| Inherited | No |
| Animation | Per computed value if the <visual-box> values match, otherwise discrete |
| Support | Chrome 90 · Firefox 102 · Safari no |
| Spec | css-overflow-4 |