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 ]
Initialinline
Applies toAll elements
InheritedNo
AnimationDiscrete; can transition to and from none with transition-behavior: allow-discrete
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-display-4
ValueEffect
blockA block-level box on its own line, laid out with normal flow inside.
inlineFlows inline with text; width, height and vertical margins do not apply.
inline-blockFlows inline but is sized like a block, accepting width, height and all margins.
flex / inline-flexA flex container. See Flexbox.
grid / inline-gridA grid container. See Grid.
flow-rootA block that starts a new block formatting context, so it contains its floats and its children's margins do not collapse through it.
list-itemA block box that also generates a ::marker.
table, table-row, table-cell …The table layout model and its internal parts.
ruby, ruby-textRuby annotation layout.
contentsThe element generates no box of its own; its children take its place.
noneThe 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.

Syntaxvisible | hidden | collapse
Initialvisible
Applies toAll elements
InheritedYes
AnimationDiscrete
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-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.

Syntaxcontent-box | border-box
Initialcontent-box
Applies toAll elements that accept width or height
InheritedNo
AnimationDiscrete
SupportChrome 10 · Firefox 29 · Safari 5.1
Speccss-sizing-3
*, *::before, *::after { box-sizing: border-box; }
The common reset: every declared size includes its padding and border.

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.

Syntaxauto | <box-size> | <anchor-size()>
Initialauto
Applies toAll elements except non-replaced inline boxes
InheritedNo
PercentagesRelative to the width (for width) or height (for height) of the containing block
AnimationBy computed value type
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-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-*.

Syntaxauto | <box-size> | <anchor-size()>
Initialauto
Applies toAll elements that accept width or height
InheritedNo
PercentagesRelative to the width or height of the containing block
AnimationBy computed value type
Supportmin-width: Chrome 1 · Firefox 1 · Safari 1
min-height: Chrome 1 · Firefox 3 · Safari 1.3
Speccss-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-*.

Syntaxnone | <box-size> | <anchor-size()>
Initialnone
Applies toAll elements that accept width or height
InheritedNo
PercentagesRelative to the width or height of the containing block
AnimationBy computed value type
Supportmax-width: Chrome 1 · Firefox 1 · Safari 1
max-height: Chrome 1 · Firefox 1 · Safari 1.3
Speccss-sizing-3
max-width: min(100%, 60rem);
Fill the container, but never grow past a readable measure.

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.

Syntaxauto || <ratio>
Initialauto
Applies toAll elements except inline boxes and internal ruby or table boxes
InheritedNo
AnimationBy computed value
SupportChrome 88 · Firefox 89 · Safari 15
Speccss-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.

Syntaxauto | content-width | content-height | content-block-size | content-inline-size
Initialauto
Applies toReplaced elements
InheritedNo
AnimationDiscrete
SupportChrome 154 · Firefox no · Safari no
Speccss-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,∞]>
Initial1
Applies toAll elements
InheritedNo (but the effect multiplies down the tree)
AnimationBy computed value type
SupportChrome 1 · Firefox 126 · Safari 3.1
Speccss-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}
Initial0
Applies toAll elements except internal table elements, ruby base containers and ruby annotation containers
InheritedNo
PercentagesRefer to the logical width of the containing block, on every side
AnimationBy computed value type
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-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()>
Initial0
Applies toAll elements except internal table elements, ruby base containers and ruby annotation containers
InheritedNo
PercentagesRefer to the logical width of the containing block
AnimationBy computed value type
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-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.

Syntaxnone | block | [ block-start || block-end ]
Initialnone
Applies toBlock containers and multi-column containers
InheritedNo
AnimationDiscrete
SupportChrome 155 · Firefox no · Safari 16.4
Speccss-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}
Initial0
Applies toAll elements except internal table elements other than table cells, ruby base containers and ruby annotation containers
InheritedNo
PercentagesRefer to the logical width of the containing block, on every side
AnimationBy computed value type
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-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,∞]>
Initial0
Applies toAll elements except internal table elements other than table cells, ruby base containers and ruby annotation containers
InheritedNo
PercentagesRefer to the logical width of the containing block
AnimationBy computed value type
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-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}
Initialvisible
Applies toBlock containers, flex containers and grid containers
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-overflow-3
ValueEffect
visibleContent spills outside the box.
hiddenContent is clipped with no scrollbars, but the box is still a scroll container and can be scrolled by script.
clipContent is clipped and the box cannot be scrolled at all, not even by script.
scrollContent is clipped and scrollbars are always shown.
autoContent 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.

Syntaxvisible | hidden | clip | scroll | auto
Initialvisible
Applies toBlock containers, flex containers, grid containers and table grid boxes
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 3.5 · Safari 3
Speccss-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,∞]>
Initial0px
Applies toBoxes to which overflow applies
InheritedNo
AnimationPer computed value if the <visual-box> values match, otherwise discrete
SupportChrome 90 · Firefox 102 · Safari no
Speccss-overflow-4