<pre class='metadata'>
Title: CSS Box Model Module Level 3
Shortname: css-box
Level: 3
Status: ED
Work Status: Exploring
Group: csswg
ED: https://drafts.csswg.org/css-box-3/
TR: https://www.w3.org/TR/css-box-3/
Previous Version: https://www.w3.org/TR/2018/WD-css-box-3-20180802/
Abstract: This specification describes the margin and padding properties, which create spacing in and around a CSS box. It may later be extended to include borders (currently described in [[css-backgrounds-3]]).
Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact, w3cid 35400
Ignored Terms: internal table elements, block layout
</pre>
<pre class='link-defaults'>
spec:css-sizing-3; type:dfn; text:size
</pre>

Introduction {#intro}
============

	<p><em>This subsection is not normative.</em>

	<p>CSS describes how each element
	and each string of text in a source document
	is laid out by transforming the <a>document tree</a>
	into a set of <a>boxes</a>,
	whose size, position, and stacking level on the <a>canvas</a>
	depend on the values of their CSS properties.

	Note: <a href="https://www.w3.org/TR/css-cascade/">CSS Cascading and Inheritance</a>
	describes how properties are assigned to elements in the box tree,
	while [[css-display-3#intro]] describes how the <a>document tree</a>
	is transformed into the <a>box tree</a>.

	<p>Each CSS <a>box</a>
	has a rectangular content area,
	a band of padding around the content,
	a border around the padding,
	and a margin outside the border.
	The <a>sizing properties</a> [[css-sizing-3]],
	together with various other properties that control layout,
	define the size of the content area.
	The box styling properties--
	'padding' and its longhands,
	'border' and its longhands,
	and 'margin' and its longhands--
	define the sizes of these other areas.
	Margins and padding are defined in this module;
	borders are defined in [[css-backgrounds-3]].

	Note: This module <a href="https://www.w3.org/TR/2018/WD-css3-box-20180731/">originally contained</a>
	the CSS Level 3 specification prose relating to
	box generation (now defined in [[css-display-3]]),
	the box model (defined here),
	as well as block layout (now only defined in [[CSS2]] Chapters 9 and 10).
	Since its maintenance was put aside during the development of CSS2.1,
	its prose was severely outdated by the time CSS2 Revision 1
	was finally completed.
	Therefore, the block layout portion of the prose has been retired,
	to be re-synched to <a href="https://www.w3.org/TR/CSS2">CSS2</a> and updated
	as input to a new Block Layout module at some point in the future.
	It is being split apart from this module
	and from the <a href="https://www.w3.org/TR/css-display/">CSS Display Module</a>
	both because of the practical concern that it would be a huge amount of work
	and also in recognition that CSS now has multiple layout models
	(<a href="https://www.w3.org/TR/css-flexbox/">Flex Layout</a>,
	<a href="https://www.w3.org/TR/css-grid/">Grid Layout</a>,
	<a href="https://www.w3.org/TR/css-position/">Positioned Layout</a>,
	and <a href="https://www.w3.org/TR/css-tables/">Table Layout</a>,
	in addition to Block Layout)
	which each deserve their own parallel module.

<h3 id="values">
Values</h3>

	This specification follows the <a href="https://www.w3.org/TR/CSS2/about.html#property-defs">CSS property definition conventions</a> from [[!CSS2]]
	using the <a href="https://www.w3.org/TR/css-values-3/#value-defs">value definition syntax</a> from [[!CSS-VALUES-3]].
	Value types not defined in this specification are defined in CSS Values &amp; Units [[!CSS-VALUES-3]].
	Other CSS modules may expand the definitions of these value types.

	In addition to the property-specific values listed in their definitions,
	all properties defined in this specification
	also accept the <a>CSS-wide keywords</a> keywords as their property value.
	For readability they have not been repeated explicitly.

<h3 id="placement">
Module Interactions</h3>

	<p>This module replaces the definitions of the margin and padding properties
	defined in [[!CSS2]] sections 8.1, 8.2, 8.3 (but not 8.3.1), and 8.4.

	<p>All properties in this module apply to the
	''::first-line'' and ''::first-letter'' pseudo-elements.

The CSS Box Model {#box-model}
=================

	<p export>Each box has a <dfn>content area</dfn>
	(which contains its content--
	text, descendant boxes, an image or other <a>replaced element</a> content, etc.)
	and optional surrounding
	<dfn lt="padding area">padding</dfn>,
	<dfn lt="border area">border</dfn>,
	and <dfn lt="margin area">margin areas</dfn>;
	the size of each area is specified by corresponding properties,
	and can be zero
	(or in the case of margins, negative).
	The following diagram shows how these areas relate
	and the terminology used to refer to the various parts of the box:

	<div class="figure">
		<p><img src="images/box.png" alt="Diagram of a typical box, showing the
		content, padding, border and margin areas">

		<p class="caption">The various areas and edges of a typical box.
	</div>

	The margin, border, and padding can be broken down into
	top, right, bottom, and left segments,
	each of which can be controlled independently
	by its corresponding property.

	The perimeter of each of the four areas
	(content, padding, border, and margin)
	is called an “edge”,
	and each edge can be broken down into
	a top, right, bottom, and left side.
	Thus each <a>box</a> has four edges
	each composed of four sides:

	<dl export>
		<dt><dfn>content edge</dfn> or <dfn>inner edge</dfn>
		<dd>
			The content edge surrounds
			the rectangle given by the width and height of the box,
			which often depend on the element's content
			and/or its <a>containing block</a> size.
			The four sides of the <a>content edge</a> together
			define the box's <dfn>content box</dfn>.

		<dt><dfn>padding edge</dfn>
		<dd>
			The padding edge surrounds
			the box’s padding.
			If the padding has zero width on a given side,
			the padding edge coincides with the content edge on that side.
			The four sides of the <a>padding edge</a> together
			define the box's <dfn>padding box</dfn>,
			which contains both the
			<a lt="content area">content</a>
			and <a>padding areas</a>.

		<dt><dfn>border edge</dfn>
		<dd>
			The border edge surrounds the box’s border.
			If the border has zero width on a given side,
			the border edge coincides with the padding edge on that side.
			The four sides of the <a>border edge</a> together
			define the box's <dfn>border box</dfn>,
			which contains the box’s
			<a lt="content area">content</a>,
			<a lt="padding area">padding</a>,
			and <a>border areas</a>.

		<dt><dfn>margin edge</dfn> or <dfn>outer edge</dfn>
		<dd>
			The margin edge surrounds the box’s margin.
			If the margin has zero width on a given side,
			the margin edge coincides with the border edge on that side.
			The four sides of the <a>margin edge</a> together
			define the box's <dfn>margin box</dfn>,
			which contains the all of the box’s
			<a lt="content area">content</a>,
			<a lt="padding area">padding</a>,
			<a lt="border area">border</a>,
			and <a>margin areas</a>.
	</dl>

	The background of the content, padding, and border areas of a box
	is specified by its 'background' properties.
	The border area can additionally be painted with a border style
	using the 'border' properties.
	Margins are always transparent.
	See [[css-backgrounds-3]].

	When a box <a href="https://www.w3.org/TR/css-break-3/#fragmentation-model">fragments</a>--
	is broken, as across lines or across pages, into separate <a>box fragments</a>--
	each of its boxes
	(<a>content box</a>, <a>padding box</a>, <a>border box</a>, <a>margin box</a>)
	also fragments.
	How the content/padding/border/margin areas react to fragmentation
	is specified in [[css-break-3]]
	and controlled by the 'box-decoration-break' property.

Box-edge Keywords {#keywords}
-----------------

	The following CSS keywords are defined for use
	in properties (such as 'transform-box' and 'background-clip')
	that need to refer to various box edges:

	<dl dfn-for="<box>,<shape-box>,<geometry-box>" dfn-type=value>
		<dt><dfn>content-box</dfn>
			<dd>
				Refers to the [=content box=] or [=content edge=].
				(In an SVG context, treated as ''<box>/fill-box''.)

		<dt><dfn>padding-box</dfn>
			<dd>
				Refers to the [=padding box=] or [=padding edge=].
				(In an SVG context, treated as ''<box>/fill-box''.)

		<dt><dfn>border-box</dfn>
			<dd>
				Refers to the [=border box=] or [=border edge=].
				(In an SVG context, treated as ''<box>/stroke-box''.)

		<dt><dfn>margin-box</dfn>
			<dd>
				Refers to the [=margin box=] or [=margin edge=].
				(In an SVG context, treated as ''<box>/stroke-box''.)

		<dt><dfn>fill-box</dfn>
			<dd>
				Refers to the [=object bounding box=] or its edges.
				(In a CSS box context, treated as ''<box>/content-box''.)

		<dt><dfn>stroke-box</dfn>
			<dd>
				Refers to the [=stroke bounding box=] or its edges.
				(In a CSS box context, treated as ''<box>/border-box''.)

		<dt><dfn>view-box</dfn>
			<dd>
				Refers to the nearest [=SVG viewport=].
				(In a CSS box context, treated as ''<box>/border-box''.)
	</dl>

	For convenience, the following value types are defined:
	<pre>
		<dfn><<visual-box>></dfn> = content-box | padding-box | border-box
		<dfn><<layout-box>></dfn> = content-box | padding-box | border-box | margin-box
		<dfn><<paint-box>></dfn> = content-box | padding-box | border-box | fill-box | stroke-box
		<dfn><<coord-box>></dfn> = content-box | padding-box | border-box | fill-box | stroke-box | view-box
	</pre>

Margins {#margins}
=======

	<dfn export lt="margin">Margins</dfn> surround the border edge of a box,
	providing spacing between boxes.
	The <dfn export>margin properties</dfn> specify the thickness
	of the <a>margin area</a> of a box.
	The 'margin' <a>shorthand property</a>
	sets the margin for all four sides
	while the margin <a>longhand properties</a> only set their respective side.
	This specification defines the <a>physical</a> 'margin' <a>longhands</a>;
	[[css-logical-1#margin-properties]] additionally
	defines <a>flow-relative</a> 'margin' <a>longhands</a>.
	Both sets of properties control the same set of margins:
	they are just different ways of indexing each side.

	Note: Adjoining margins in <a>block layout</a> <em>collapse</em>.
	See <a href="https://www.w3.org/TR/CSS2/box.html#collapsing-margins">CSS2&sect;8.3.1 Collapsing Margins</a>
	for details.
	Also, margins adjoining a <a>fragmentation break</a> are sometimes truncated.
	See [[css-break-3#break-margins]] for details.

Page-relative (Physical) Margin Properties: the 'margin-top', 'margin-right', 'margin-bottom', and 'margin-left' properties {#margin-physical}
-------------------------------------------

	<pre class="propdef">
		Name: margin-top, margin-right, margin-bottom, margin-left
		Value: <<length-percentage>> | auto
		Initial: 0
		Applies to: all elements except <a>internal table elements</a>
		Inherited: no
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: the keyword ''margin/auto'' or a computed <<length-percentage>> value
		Animation type: by computed value type
	</pre>

	These properties set the top, right, bottom, and left
	<a>margin</a> of a <a>box</a>, respectively.

	Negative values for margin properties are allowed,
	but there may be implementation-specific limits.

Margin Shorthand: the 'margin' property {#margin-shorthand}
-----------------

	<pre class="propdef">
		Name: margin
		Value: <<'margin-top'>>{1,4}
		Initial: 0
		Applies to: all elements except <a>internal table elements</a>
		Inherited: no
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: see individual properties
		Animation type: by computed value type
	</pre>

	The 'margin' property is a shorthand property for setting
	'margin-top', 'margin-right', 'margin-bottom', and 'margin-left'
	in a single declaration.

	If there is only one component value,
	it applies to all sides.
	If there are two values,
	the top and bottom margins are set to the first value
	and the right and left margins are set to the second.
	If there are three values,
	the top is set to the first value,
	the left and right are set to the second,
	and the bottom is set to the third.
	If there are four values
	they apply to the top, right, bottom, and left, respectively.

	<div class="example">
		The following code demonstrates some possible 'margin' declarations.

		<pre>
			body { margin: 2em }         /* all margins set to 2em */
			body { margin: 1em 2em }     /* top & bottom = 1em, right & left = 2em */
			body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
		</pre>

		The last rule of the example above is equivalent to the example below:
		<pre>
			body {
			  margin-top: 1em;
			  margin-right: 2em;
			  margin-bottom: 3em;
			  margin-left: 2em; /* copied from opposite side (right) */
			}
		</pre>
	</div>

Margins at Container Edges: the 'margin-trim' property {#margin-trim}
--------------------------

	<pre class="propdef">
		Name: margin-trim
		Value: none | in-flow | all
		Initial: none
		Applies to: [=block containers=], [=multi-column containers=]
		Inherited: no
		Percentages: N/A
		Computed value: keyword as specified
		Animation type: discrete
	</pre>

	Oftentimes, margins are desired between siblings,
	but not at the start/end of the container
	where spacing can be controlled with padding.
	This property allows the container
	to trim the margins of its children
	where they adjoin the container’s edges.
	Values have the following meanings:

	<dl dfn-type=value dfn-for=margin-trim>
		<dt><dfn>none</dfn>
		<dd>
			Margins are not trimmed by the container.

			Note: However, in block layout,
			child margins can collapse with their parent.
			See <a href="https://www.w3.org/TR/CSS2/box.html#collapsing-margins">CSS2&sect;8.3.1: Collapsing Margins</a>.

		<dt><dfn>in-flow</dfn>
		<dd>
			For in-flow boxes contained by this box,
			block-axis margins adjacent to the box’s edges
			are truncated to zero.
			It also truncates any margins collapsed with such a margin.

		<dt><dfn>all</dfn>
		<dd>
			Trims the margins of in-flow boxes as for ''in-flow'',
			but also trims any float margin
			whose [=margin edge=] coincides
			with the container’s [=content edge=].
	</dl>

	Specifically, for [=block containers=],
	''margin-trim: in-flow'' or ''margin-trim: all'' discards:
	<ul>
		<li>The block-start margin of a block-level first child.
		<li>The block-end margin of a block-level last child.
		<li>Any margin collapsed with these margins.
	</ul>

	''margin-trim: all'' also affects floats
	for which the [=block container=] is a [=containing block=] by:
	<ul>
		<li>
			Discarding the [=block-start=] [=margin=] of any float
			whose [=block-start=] [=outer edge=] coincides
			with the [=block-start=] [=inner edge=] of the container.
		<li>
			Discarding the [=inline-start=]/[=inline-end=] [=margin=]
			of an [=inline-start=]/[=inline-end=] float (or equivalent)
			whose [=outer edge=] on that side coincides
			with the [=inner edge=] of the container.
		<li>
			Zeroing the [=inline-axis=] margins of a float
			for the purpose of calculating its [=intrinsic size contributions=]
			or its [=size=] in the container’s [=inline axis=].
		<li>
			Trimming the [=block-end=] margins of a float
			to the extent necessary to prevent such a margin
			from extending the [=block size=]
			of a [=block formatting context root=].
	</ul>

	ISSUE: Should this property apply to [=flex containers=] or [=grid containers=]?

	ISSUE: Should floats have a <css>floats</css> value that only affects floats?

	Note: See also the 'margin-break' property,
	which applies to the box’s own margins
	when they adjoin a <a>fragmentation break</a>
	(page break / column break / etc.).

	ISSUE: Define how this property affects margins at breaks
	if the box establishes a <a>fragmentation context</a>.
	See also <a href="https://github.com/w3c/csswg-drafts/issues/3314">Issue 3314</a>.

Padding {#paddings}
=======

	<dfn export>Padding</dfn> is inserted between the content edge
	and the padding edge of a box,
	providing spacing between the content and the border.
	The <dfn export>padding properties</dfn> specify the thickness
	of the <a>padding area</a> of a box.
	The 'padding' <a>shorthand property</a>
	sets the padding for all four sides
	while the padding <a>longhand properties</a> only set their respective side.
	This specification defines the <a>physical</a> 'padding' <a>longhands</a>;
	[[css-logical-1#padding-properties]] additionally
	defines <a>flow-relative</a> 'padding' <a>longhands</a>.
	Both sets of properties control the same set of padding:
	they are just different ways of indexing each side.

	Note: Backgrounds specified on the box
	are by default laid out and painted within the padding edges.
	(They are additionally painted underneath the border,
	in the <a>border area</a>.)
	This behavior can be adjusted
	using the 'background-origin' and 'background-clip' properties.

Page-relative (Physical) Padding Properties: the 'padding-top', 'padding-right', 'padding-bottom', and 'padding-left' properties {#padding-physical}
-------------------------------------------

	<pre class="propdef">
		Name: padding-top, padding-right, padding-bottom, padding-left
		Value: <<length-percentage>>
		Initial: 0
		Applies to: all elements except: <a>internal table elements</a> other than table cells
		Inherited: no
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: a computed <<length-percentage>> value
		Animation type: by computed value type
	</pre>

	These properties set the top, right, bottom, and left
	<a>padding</a> of a <a>box</a>, respectively.

	Negative values for padding properties are invalid.

Padding Shorthand: the 'padding' property {#padding-shorthand}
-----------------

	<pre class="propdef">
		Name: padding
		Value: <<'padding-top'>>{1,4}
		Initial: 0
		Applies to: all elements except: <a>internal table elements</a> other than table cells
		Inherited: no
		Percentages: refer to <a>logical width</a> of containing block
		Computed value: see individual properties
		Animation type: by computed value type
	</pre>

	The 'padding' property is a shorthand property for setting
	'padding-top', 'padding-right', 'padding-bottom', and 'padding-left'
	in a single declaration.

	If there is only one component value,
	it applies to all sides.
	If there are two values,
	the top and bottom padding are set to the first value
	and the right and left padding are set to the second.
	If there are three values,
	the top is set to the first value,
	the left and right are set to the second,
	and the bottom is set to the third.

	<div class="example">
		The following code demonstrates some possible 'padding' declarations.

		<pre>
			body { padding: 2em }         /* all padding set to 2em */
			body { padding: 1em 2em }     /* top & bottom = 1em, right & left = 2em */
			body { padding: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
		</pre>

		The last rule of the example above is equivalent to the example below:
		<pre>
			body {
			  padding-top: 1em;
			  padding-right: 2em;
			  padding-bottom: 3em;
			  padding-left: 2em; /* copied from opposite side (right) */
			}
		</pre>
	</div>

Borders {#borders}
=======

	<dfn export lt="border">Borders</dfn> fill the <a>border area</a>,
	to visually delineate the edges of the box,
	The <dfn export>border properties</dfn> specify the thickness
	of the <a>border area</a> of a box,
	as well as its drawing style and color.
	See [[css-backgrounds-3#borders]] for the definition
	of the physical variants of these properties;
	[[css-logical-1#border-properties]] additionally
	defines <a>flow-relative</a> border <a>longhands</a>.
	Both sets of properties control the same set of borders:
	they are just different ways of indexing each side.

Changes Since CSS Level 2 {#changes}
=========================

	The following changes have been made to this module
	since <a href="https://www.w3.org/TR/CSS2/box.html">CSS Level 2</a>:
	<ul>
		<li>Adding the 'margin-trim' property.
		<li>Adapting the prose slightly to account for vertical <a>writing modes</a>.
	</ul>

Privacy and Security Considerations {#priv-sec}
===============================================

	Box Model introduces no new privacy leaks,
	or security considerations beyond "implement it correctly".
