8000 csswg-drafts/css-box-3/Overview.bs at 8d24d2f5e3c6d9b7558d14e6a8fc565f0439a395 · w3c/csswg-drafts · GitHub
Skip to content

Latest commit

 

History

History
4391 lines (3569 loc) · 165 KB

File metadata and controls

4391 lines (3569 loc) · 165 KB
<!doctype html public "-//W3C//DTD Bikeshed 2016-09//EN">
<meta content="text/html; charset=utf-8" http-equiv=content-type>
<title>CSS basic box model</title>
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<link href="../default.css" rel="stylesheet" type="text/css">
<style type="text/css">
ol.stack p {margin-top: 0}
img.small {max-width: 100%}
.minidiagram {height: 6em; vertical-align: middle}
dt {display: inline}
dt + dt::before {content: ", "}
/* Undo the max-width in the 2012 style for editor's drafts: */
/* maybe replace this with using default.css's class=data? */
.equiv-table {margin: 0.5em 0; border-collapse: collapse;
border-bottom: hidden; empty-cells: show}
.equiv-table th, .equiv-table td {padding: 0.3em 1em;
border-bottom: thin solid #666}
.equiv-table th {text-align: left}
/* .equiv-table th:first-child {text-align: right} */
.equiv-table caption {margin: 0.5em 0 0 0}
.equiv-table td > p:first-child, .equiv-table th > p:first-child {
margin-top: 0}
.equiv-table tr {vertical-align: baseline}
.equiv-table img {max-width: 100%}
/* The TR postprocessor already inserted quotes: */
.css::before, .property::before, .descriptor::before {content: none}
.css::after, .property::after, .descriptor::after {content: none}
@media screen {
#annoying-warning:not(:checked) + * + * {position: fixed; z-index: 2;
bottom: 2em; left: 0; right: 0; text-align: center;
padding: 0.5em 0.5em 1em 0.5em; box-shadow: 0 2px 8 8DE3 px black;
border-top: thin solid; border-bottom: thin solid;
background: hsla(40,100%,50%,0.9); color: black}
#annoying-warning:not(:checked) + * + * p {margin: 0}
#annoying-warning, #annoying-warning + * {position: fixed; z-index: 3}
#annoying-warning {font-size: 1em; bottom: 2.3em; right: 0.5em; margin: 0}
#annoying-warning + * {font-size: 80%; bottom: 2.5em; right: 2.3em}
#annoying-warning:checked, #annoying-warning:checked + * {display: none}
html {margin-bottom: 15em}
}
</style>
<link href="https://www.w3.org/StyleSheets/TR/W3C-ED.css" rel="stylesheet"
title="Default" type="text/css">
<link rel=contents href="#contents">
<link rel=index href="#index">
<!-- TODO: cross-references aren't automatically generated by
Bikeshed. Is there a better way than to add <a> around every
<span> and <em>? -->
<!-- TODO: bibliography still doesn't show the dated specs. -->
<pre class=metadata>
Title: CSS basic box model
Shortname: css-box
Level: 3
Status: ED
Work Status: rewriting
Previous version: http://www.w3.org/TR/2007/WD-css3-box-20070809/
TR: https://www.w3.org/TR/css-box-3/
ED: http://dev.w3.org/csswg/css-box/
Group: csswg
Editor: Bert Bos, W3C, mailto:bert@w3.org, w3cid 3343
Editor: Anton Prowse, Invited Expert, mailto:prowse@moonhenge.net, w3cid 48426
Default Biblio Status: dated
Markup Shorthands: markdown no, markup yes
Abstract:
<![CDATA[<p><abbr title="Cascading Style Sheets">CSS</abbr> describes the
rendering of documents on various media. When textual documents (e.g.,
HTML) are laid out on visual media (e.g., screen or print), CSS models
their layout as a collection of rectangular boxes containing words, lines,
paragraphs, tables, etc., each with properties such as size, color and
font.
<p>This module describes the basic types of boxes: block, list item,
inline, etc.; and some of their properties, including margins, padding
and width/height. It defines a layout called a “flow,” in which a
series of boxes is laid out one after the other, and it defines
“floating” boxes. Other kinds of layout, such as tables, absolute
positioning, ruby annotations, grid layouts, columns and page boxes,
are described by other modules. Also, the layout of text inside a line
(including the handling of left-to-right and right-to-left scripts) is
defined elsewhere.
<p>In CSS level 3, boxes may contain either horizontal or vertical text.
Different orientations can be mixed.
]]>
Status Text:
<![CDATA[<p> <div> <input id=annoying-warning type=checkbox title="hide
warning"> <label for=annoying-warning>(hide)</label> <div> <p>This
draft is undergoing changes and many parts are not consistent with
other modules of CSS. <p>Please, refer to CSS level 2 [[!CSS21]]
instead for the definition of the basic box model. </div> </div>
<p>This module should eventually replace corresponding parts of the
revised CSS level 2 specification [[!CSS21]]. But this is an early
draft and any differences to level 2 are most likely unintentional,
unless they concern new features, such as vertical text or float
displacement. <em>Please help us improve the next draft by pointing
out such differences.</em>
]]>
</pre>
<pre class=link-defaults>
spec:css-display-3; type:value; for:display; text:block
spec:css-display-3; type:value; for:display; text:list-item
spec:css-display-3; type:value; for:display; text:table
</pre>
<!--=================================================================-->
<!--
<p>Each property is defined in part in the text and in part by a table
that groups together a number of facts about the property, including a
regular expression to restrict its syntax. See <span
class=issue>[where?]</span> for the meaning. The “Inherited” and
“Initial” rows in the table are used by the Cascading and Inheritance
module [[CSS3CASCADE]] and “Media” by the Media Queries specification
[[MEDIAQ]].
<p>The specification may refer to the <a>used value</a> and the
<a>computed value</a> of a property. Unless stated explicitly, the
short form “value” means the computed value.
-->
<!--=================================================================-->
<h2 id=dependencies>Dependencies on other modules</h2>
<p>This CSS module depends on the following other CSS modules:
<p class=issue>To do…
<!--=================================================================-->
<h2 id=intro>Introduction &amp; definitions</h2>
<div class=issue id=center-text>
<p>How to center the text of a block element horizontally (more
precisely: in the inline direction) even if (part of) the block is
next to a float?
</div>
<div class=issue id=center-block>
<p>How to center a block horizontally in its CB if the block is wider
than the CB? (I.e., when 'margin: auto' doesn't work and the document
structure doesn't allow transforms or flexbox.)
</div>
<div class=issue id=distribute>
<p>How to center or distribute blocks vertically when flexbox doesn't
apply (e.g., because the blocks aren't siblings)?
</div>
<div class=issue id=anonymous-box-pseudo>
<p>Investigate if it is possible to make a pseudo-element to select
anonymous boxes ('::paragraph'). See minutes of 10 Aug 2011.
</div>
<p class=note>Note: The model in this specification differs from the
model described in the CSS level 2 specification [[!CSS21]], because
it is generalized to apply also to vertical text. A document using
only features from level 2 renders exactly as described there, except
possibly for features that are undefined in that specification and
have become defined since.
<p>CSS assumes that the document to lay out is modeled as a <span
class=index title="tree!!of elements|document 7DDD tree|element
tree">tree</span> of <dfn title="element">elements.</dfn> Each element
has an ordered list of zero or more child elements, with an optional
string of text before the list, in-between the children and after the
list. Each child has one parent, except for the unique element that
has no parent, which is called the <dfn>root element.</dfn>
<p class=note>Note that such a tree can be described in XML or SGML,
but also in other notations and that it is the definition of that
notation, not CSS, that determines the equivalence between the
abstract tree and the concrete notation. E.g., SGML often allows white
space to be added between elements for readability, which does not
result in a string of spaces in the abstract tree.
<p>CSS describes how each element and each string of text is laid out
by transforming the document tree into a set of boxes, whose size,
position, and stacking level on the <a>canvas</a> depend on their
properties. We say that an element <dfn title="generated
box">generates</dfn> one or more boxes.
<div class=issue id=box-tree>
<p>Define the box tree <!-- Is it a tree (possibly with cross-links)?
A graph? Purely geometrical? Or are boxes not linked to each other,
but only to elements or viewports? When we say box A contains box B,
does that mean B is geometrically within A, or just that B's
generating element is a descendant of A's generating element? Is there
only one kind of box or are there several (line box, block box, margin
box…)? E.g., if we talk about an “inline box”, is that a kind of box,
or only a shorthand for “a box generated by an inline element”? Are
letters (glyphs) boxes? Are words? Is the space between words a box or
a margin? Or does that depend on the white-space property? Is there an
order defined for boxes? E.g., can we talk about the “first” or the
“next” box? About the “first box of a fragmentainer”? The “first box
of an element”? Does bidi-reordering, flexbox reordering, run-in
reordering or absolute positioning change that order? -->
</div>
<div class=figure>
<p><img src="box-intro.png" alt="Each generated box in the rendering
has a link to the element that generated it.">
<p class="caption">Relation between four displayed boxes in the
rendered document (on the right) and the three corresponding elements
in the source document on the (left).
</div>
<div class=example>
<p>For example, a fragment of HTML such as
<pre>&lt;ul&gt;
&lt;li&gt;The first item in the list.
&lt;li&gt;The second item.
&lt;/ul&gt;</pre>
<p>may result in <span class=issue>[rewrite the following
sentence?]</span> one block-level box for the <code>ul</code> element,
containing two block-level boxes for the two <code>li</code> elements,
each of which has one line box (i.e., one line of text). Both line
boxes contain two inline-level boxes: one that contains the list
bullet and one that contains the text.
<p>Note how the <code>li</code> is transformed into multiple boxes, including
one that contains “generated content,” viz., the list bullet,
which is not present in the source document.
<p>If the document is rendered in a narrow window, it may be that the
<code>li</code> elements get transformed into even more boxes, because the
text requires multiple lines. And if the document is rendered on paper
or in multiple columns, it
may be that a page break falls in the middle of the <code>ul</code> element,
so that it is not transformed into a single block-level box, but into two
smaller ones, each on a different page or column.
</div>
<p>Each box belongs to exactly one element. It is either <a
title="generated box">generated</a> directly by the element, or it
is an <a>anonymous box,</a> which has been inserted to ensure that
every box and its parent are of mutually compatible types as defined
by the layout rules of CSS. An anonymous box is defined to belong to
whichever element generates the box's closest element-generated
ancestor box. When we need to be precise, we say that the anonymous
box is <dfn title="induce">induced,</dfn> rather than generated, by
the element it belongs to.
<p class=note>For example, an anonymous table wrapper box that is
created around a table cell because the table cell element doesn't
have a table element as parent, belongs to the parent element of the
table cell, not to the table cell itself.
<p class=note>An anonymous box is never a <em>principal</em>
box. Except for that, an anonymous box cannot be distinguished by its
property values and characteristics from a box generated by some
hypothetical document element. <span class=issue>Remove this note? Do
we actually use the concept of “principal box” anywhere?</span>
<div class=issue>
<p>Do boxes have properties? Or do they just have margins, borders,
padding, a size and some content (which may be text in a certain font
and a certain color, but without the box itself having a font or a
color property)?
</div>
<!--
<p class=note>Boxes are frequently referred to by their property
values. For example, a “floated box” is a box whose computed value of
'float' is not ''none'', and a “positioned box” is a box whose
computed value of 'position' is not ''static''. However, terms such as
<a>block-level box,</a> <a>block container box</a> and <a>table
box</a> describe characteristics which cannot be deduced solely from
a box's property values.
-->
<div class=figure id=various>
<p><img src="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>
<p>Boxes have padding, a border and margins (see
the <a href="#various">figure</a>). Different properties determine the
thickness of each of these (which may be zero). The margins of
adjacent boxes are also subject
to <em title="collapse">collapsing</em> (i.e., the actual margin
between adjacent boxes may be less than the sum of the boxes'
individual margins).
<p>Each box has a <dfn>content area</dfn> (a.k.a. <dfn>content
box</dfn>). The rectangle that bounds this area is the <dfn>content
edge.</dfn> Around the content area is the <dfn>padding area</dfn> and
its outside bounds are called the <dfn>padding edge</dfn>. The padding
area and content area together form the <dfn>padding box</dfn>.
Outside the padding is the <dfn>border area</dfn> and the outside
boundary of that area is the <dfn>border edge</dfn>/ The border area,
padding area and content area together form the <dfn>border box</dfn>.
Finally, outside the border is the <dfn>margin area</dfn> and its
outer edge is the <dfn>margin edge</dfn>.
<p>When the specification says that the padding or border is
<dfn title="absent|padding!!absent|border!!absent" >“absent”</dfn>
on some side of the box, that means that
its thickness is zero.
<p><em title="line box">Line boxes</em> cannot have any padding,
border or margin, and therefore their margin edge, border edge,
padding edge and content edge all coincide.
<p class=note>Note that the margin, unlike the border and padding, may have
a negative thickness. That is one way to make adjacent boxes overlap each
other.
<p class=note>Note that the edges always form rectangles, even if
there is a 'border-radius' [[CSS3BG]] or a shape [[CSS3-EXCLUSIONS]].
<p>We say that a box or element is <dfn>horizontal</dfn> if its
'writing-mode' property is ''horizontal-tb'', otherwise it is
<dfn>vertical</dfn> (see [[!CSS3-WRITING-MODES]]).
<!--
<div class=note>
<p>Note that there are theoretically eight possible orientations for
text, but CSS only defines six:
<table class=equiv-table style="table-layout: fixed; width: 100%">
<thead>
<tr>
<th style="width: 8em">
<th>'writing-mode: horizontal-tb'
<th>'writing-mode: vertical-rl' or 'writing-mode: sideways-rl'
<th>'writing-mode: vertical-lr' or 'writing-mode: sideways-lr'
<tbody>
<tr>
<th>'direction: ltr'
<td>
<p>Text is written from left to right and paragraphs
grow downwards <p><img class=small
src="horizontal.png" alt="">
<td>
<p>Text is written top to bottom and paragraphs grow to
the left <p><img class=small src="vertical.png"
alt="">
<td>
<p>Text is written top to bottom and paragraphs grow to
the right <p><img class=small src="vertical-lr.png"
alt="">
<tr>
<th>'direction: rtl'
<td>
<p>Text is written from right to left and paragraphs
grow downwards <p><img class=small
src="horizontal-rtl.png" alt="">
<td>
<p>Text is written bottom to top and paragraphs grow to
the left <p><img class=small src="vertical-rtl.png"
alt="">
<td>
<p>Text is written bottom to top and paragraphs grow to
the right <p><img class=small
src="vertical-lr-rtl.png" alt="">
</table>
<p>There is no “horizontal-bt.”
</div>
-->
<p class=mtb>Calculations of the size and position of boxes are
usually very similar for horizontal and vertical boxes, except that
the horizontal and vertical directions are interchanged. To avoid many
almost identical definitions, this specification therefore often uses
abstract terms instead of top, right, bottom, left, width and height:
four terms (block-start, block-end, inline-start and inline-end) for
the four edges of a box, four terms (A edge, B edge, C edge and D
edge) for the edges of the children of a box and two terms (measure
and extent) for the width and height of a box.
<p class=note>XSL-FO [[XSL11]] uses “inline-progression dimension” for
“measure” and “block-progression dimension” for “extent.”
<div class=note>
<p>The following terms are defined in [[!CSS3-WRITING-MODES]]. For
convenience, here is a short summary.
<p>The <dfn>block-start</dfn>, <dfn>block-end</dfn>, <dfn>inline-start</dfn> and
<dfn>inline-end</dfn> edge of a box refer to the top, right, bottom
and left edges, depending on the 'writing-mode' and 'direction'
properties of that box, as follows:
<table class=equiv-table>
<thead>
<tr>
<th>'writing-mode'
<th>'direction'
<th>“block-start”
<th>“inline-end”
<th>“block-end”
<th>“inline-start”
<th>Diagram
<tbody>
<tr><th rowspan=2>''horizontal-tb'' <th>''ltr'' <td>top
<td>right <td>bottom <td>left <td><img class=minidiagram
src="box-tb-ltr.svg" alt="">
<tr><!--horizontal-tb--> <th>''rtl'' <td>top <td>left
<td>bottom <td>right <td><img class=minidiagram
src="box-tb-rtl.svg" alt="">
<tr><th rowspan=2>''vertical-rl'', ''sideways-rl'' <th>''ltr'' <td>right
<td>bottom <td>left <td>top <td><img class=minidiagram
src="box-rl-ltr.svg" alt="">
<tr><!--vertical-rl--> <th>''rtl'' <td>right <td>top
<td>left <td>bottom <td><img class=minidiagram
src="box-rl-rtl.svg" alt="">
<tr><th rowspan=2>''vertical-lr'', ''sideways-lr'' <th>''ltr'' <td>left
<td>bottom <td>right <td>top <td><img class=minidiagram
src="box-lr-ltr.svg" alt="">
<tr><!--vertical-lr--> <th>''rtl'' <td>left <td>top <td>right
<td>bottom <td><img class=minidiagram src="box-lr-rtl.svg"
alt="">
</table>
<p>For example, the “block-start padding” by default refers
to the 'padding-top' and the “inline-end border” is by default the
'border-right'.
<p>Similarly, the specification sometimes refers to the
<dfn>measure</dfn> and <dfn>extent</dfn> of a box, instead of width
and height, as follows:
<table class=equiv-table>
<tr>
<th>'Writing-mode'
<th>“measure”
<th>“extent”
<tr><td>''horizontal-tb'' <td>width <td>height
<tr><td>''vertical-rl'' <td>height <td>width
<tr><td>''sideways-rl'' <td>height <td>width
<tr><td>''vertical-lr'' <td>height <td>width
<tr><td>''sideways-lr'' <td>height <td>width
</table>
<p>These correspond to the dimensions in the <a>inline base
direction</a> and the <a>block flow direction,</a> respectively.
<p>An alternative term for the measure is
<dfn title="logical width|width!!logical">logical width</dfn>
and an alternative term for extent is
<dfn title="logical height|height!!logical">logical height</dfn>.
These terms occasionally occur in other CSS modules.
<p>The extent of a box is thus the distance between the
block-start and block-end edges, while the measure of the box is the
distance between the inline-start and inline-end edges.
</div>
<p>When boxes are laid out to form a <a class=index >flow</a> (see
below), their edges play different roles in the alignment of the boxes
within that flow. The orientation of the flow determines which edge of
a box is aligned to which edge of another box. These roles are
independent of the orientation of the box itself. We distinguish four
roles for the edges of a box, called <dfn lt="A|A edge">A
edge</dfn>, <dfn lt="B|B edge">B edge</dfn>, <dfn lt="C|C edge">C
edge</dfn> and <dfn lt="D|D edge">D edge</dfn>. They depend on the
'writing-mode' and 'direction' properties of the box's
<em class=index>containing block</em> (defined below), and map to the four sides as
follows:
<table class=equiv-table>
<thead>
<tr>
<th>'writing-mode' of<br><a>containing block</a>
<th>'direction' of<br><a>containing block</a>
<th>“A edge”
<th>“B edge”
<th>“C edge”
<th>“D edge”
<tbody>
<tr><th rowspan=2>''horizontal-tb'' <th>''ltr''
<td>top <td>right <td>bottom <td>left
<tr><!-- horizontal-tb --><th>''rtl''
<td>top <td>left <td>bottom <td>right
<tr><th rowspan=2>''vertical-rl'', ''sideways-rl'' <th>''ltr''
<td>right <td>bottom <td>left <td>top
<tr><!-- vertical-rl --><th>''rtl''
<td>right <td>top <td>left <td>bottom
<tr><th rowspan=2>''vertical-lr'', ''sideways-lr'' <th>''rtl''
<td>left <td>bottom <td>right <td>top
<tr><!-- vertical-lr --><th>''ltr''
<td>left <td>top <td>right <td>bottom
</table>
<div class=figure>
<p><img src="abcd-edges" alt="Image: A box with edges labeled
clockwise from top as A, B, C and D.">
<p class=caption>The edges of a box whose containing block is
horizontal.
</div>
<p class=note>For example, block-level boxes in a flow are laid out
such that the A edge of the first box is against the block-start edge
of the containing block and then the A edge of each subsequent box is
put against the C edge of its predecessor. For typical English text
(i.e., <a>horizontal</a> text), this means the top edge (= A edge)
of the first paragraph is at the top edge (= block-start) of the
document, the top (A edge) of the second paragraph is against the
bottom (C edge) of the first paragraph, and so on, until the bottom (C
edge) of the last paragraph becomes the bottom (block-end) of the
document.
<p>An element is called <dfn>orthogonal</dfn> if it is either a
<a>vertical</a> element with a <a>horizontal</a> <a>containing
block</a> or a <a>horizontal</a> element with a <a>vertical</a>
<a>containing block.</a> An <dfn>orthogonal flow</dfn> is a
<a>flow</a> whose <a>flow root</a> is an orthogonal element.
<!--=================================================================-->
<h2 id=viewport>The viewport and the canvas</h2>
<p>User agents for continuous media generally offer users a
<dfn>viewport</dfn> (a window or other viewing area on the screen)
through which users consult a document. User agents may change the
document's layout when the viewport is resized (see the <a>initial
containing block</a>).
<p>When the viewport is smaller than the area of the <a>canvas</a>
(see below) on which the document is rendered, the user agent usually
offers a scrolling mechanism. There is at most one viewport per canvas,
but user agents may render to more than one canvas (i.e., provide
different views of the same document).
<p>For all media, the term <dfn>canvas</dfn> describes the space where
the formatting structure is rendered. The canvas is infinite for each
dimension of the space.
<!--=================================================================-->
<h2 id=cb>Containing blocks</h2>
<p>The <dfn>containing block</dfn> of a box is a rectangle that
is associated with the box and that is used in various definitions in this
specification. Apart from a size and a position, the rectangle also has
'direction' and 'writing-mode' properties. The containing block of a box
is defined as follows:
<ul>
<li>The containing block of the <a title="root element">root
element's</a> top-level boxes is a rectangle with the dimensions of
the <a>viewport</a>, anchored at the <a>canvas's</a>
origin for continuous media and the page area for paged media. (See
[[MEDIAQ]] and [[CSS3PAGE]] for definitions of continuous and paged
media.) This containing block is called the <dfn>initial containing
block.</dfn> The 'direction' and 'writing-mode' of the initial
containing block are the same as those of the root element.
<li>For other elements, the containing block is one of the
following:
<ol>
<li>If the element is a ''run-in'' that is rendered
inline in a sibling element, then its containing block
is the <a>content edge</a> of the <a>principal
box</a> of that sibling.
<li>Otherwise, if the parent has a <a>principal
box</a> that is a <a>block container box</a>
(e.g., if the parent is a 'table-cell' or a 'block'),
then the containing block is the content edge of the
parent's <a>principal box.</a>
<li>Otherwise the containing block is the same as that
of the parent.
</ol>
</ul>
<p class=issue>Other modules may introduce other types of boxes with
other rules for how to find their containing block. E.g., the
containing block of an absolutely positioned element.
<p>In the above, a <dfn>block container box</dfn> is, informally, a
box that can contain block boxes. More precisely: any box generated by
a (pseudo-)element with a computed value for 'display' of ''block'',
''inline-block'', ''table-caption'', ''table-cell'' or ''list-item''.
<span class=note>Note that most floating and absolutely
positioned elements have a computed 'display' of ''block''. Also, a
<a>flow root</a> has a computed 'display' of ''block''.</span>
<span class=issue>Or insert the definition of block container box from
CSS 2.1 here?</span>
<p class=issue>Also define <dfn>principal box</dfn> somewhere.
<p class=note>Note that the above is modified by the Absolute Positioning
module [[CSS3POS]]: in particular, if a box's 'position' property is neither
''static'' nor ''relative'', its containing block is established
differently.
<p>If an element <span class=issue>[or a viewport?]</span> has
scrollbars (see 'overflow'), then any space taken up by the scrollbars
should be excluded from (subtracted from the dimensions of) any
containing block formed by that element.
<!--=================================================================-->
<h2 id=flows>Flows</h2>
<p>The <dfn>flow</dfn> (sometimes called <dfn>normal flow</dfn>) is
one of the two basic kinds of layout in CSS, together with line layout
[[CSS3LINE]]. It can be used, e.g., to layout a sequence of
paragraphs, headings, lists and floated elements in a single
column. Other CSS modules, such as tables [[CSS3TBL]], positioning
[[CSS3POS]], flex boxes [[CSS3-FLEXBOX]] and grid templates
[[CSS-TEMPLATE-3]], build on the flow model by defining ways to split a
document into more than one flow and positioning and aligning those
flows in various ways on the canvas.
<p>A flow is constructed starting from a <span class=index title="flow
root!!box" >box</span> called the <dfn>flow root</dfn> by the <a
href="#construct-flow">rules below.</a> We say the flow is
<dfn>channeled</dfn> by the flow root.
<p class=issue>Are there more intuitive names than flow root?
<p>The <dfn title="initial flow|flow!!initial" >initial flow</dfn> is
the flow that contains the root of the box tree.
<p>Despite not being a box, the <a>initial containing block</a> is
said to be the flow root of the initial flow.
<p class=issue id=pagination-issue>How do we model pagination? If a
flow is broken over two pages or columns, do we call it a single flow
or two flows? I.e., is the page box the flow root of the flow, or is
the page box something else, more like the viewport, which influences
the layout of a flow but isn't part of it? See [[CSS3-BREAK]].
<p>Only <a>block container boxes</a> (defined below) can be flow
roots. (Which implies that the children of a flow root are laid out in
a <a>block formatting context.</a>)
<p>Several (combinations of) properties cause an element to generate
boxes that are flow roots. This module defines a few. Other modules
may define more.
<div class=note>
<p>For example, this module defines that any of the following make a
box into a flow root:
<ul>
<li>The value of 'float' is not 'none'.
<li>The used value of 'overflow' is not 'visible'.
<!-- <li>The box is a table wrapper, a table cell, an inline block, or an
align box. -->
<!-- <li>The value of 'position' is 'absolute' or 'fixed' (see
[[CSS3POS]]). -->
<li>The box is <a>vertical</a> and its parent is
<a>horizontal.</a> <span class=issue>[Add link to the relevant
section.]</span>
<li>The box is <a>horizontal</a> and its parent is
<a>vertical.</a>
<!-- <li>The value of 'transform' is not 'none'. -->
</ul>
<p>Other examples: The table layout module [[CSS3TBL]] defines that a
table cell generates a box that is a flow root; the positioning module
[[CSS3POS]] defines that an absolutely positioned element generates a
flow root; the grid template module [[CSS-TEMPLATE-3]] defines that a
template element generate several flow roots; and the transforms
module [[CSS3-TRANSFORMS]] defines that an element with a transform
generates a flow root.
</div>
<p class=mtb id=construct-flow>The flow of a given <a>flow root</a>
is a set of boxes. A box belongs to the flow if all of the following
are true:
<ol>
<li>The used value of 'display' is ''block'', ''list-item'',
''table'' or ''flex''.
<li>The used value of 'float' is ''none''.
<li>The used value of 'position' is ''static'' or ''relative''.
<li>The used value of 'flow-into' [[CSS3-REGIONS]] <span
class=issue>a.k.a. 'flow' [[CSS-TEMPLATE-3]]</span> is ''none''.
<li>It is not a <a>flex item</a> [[!CSS3-FLEXBOX]].
<li>It is either a child of the flow root or a child of a box that
belongs to the flow.
</ol>
<p class=note>Note that the flow root is itself not part of the flow
it channels and that each flow root channels at most one flow.
<p class=note>Note that the last rule above implies that a flow with
its flow root together are always a connected subset of the box
tree. In other words; together, they form a subtree of the box tree
with possibly some branches removed.
<p class=note>Note that the element tree and the box tree are not
necessarily parallel: the element a box belongs to need not be the
child of the element that generated the box's parent. E.g., run-in
elements may generate boxes that become children of boxes from sibling
elements; and the 'flow' property [[CSS-TEMPLATE-3]] and the 'position'
property [[CSS3POS]] may cause an element to generate a box that
becomes the child of a box from a grandparent or other ancestor
element. E.g., 'DIV {flow: b}' (see [[CSS-TEMPLATE-3]]) makes the DIV
generate boxes that become children of the box generated by slot b in
some ancestor. And 'DIV {position: absolute}' causes the DIV to
generate a box that becomes a child of the box generated by the
ancestor that established the DIV's <a>containing block.</a>
<div class=example>
<p>For example, the fragment
<pre>
&lt;div class=sidebar&gt;
&lt;p&gt;Text in a sidebar.
&lt;p&gt;Here is quote:
&lt;blockquote lang=ja&gt;
&lt;p&gt;...
&lt;/blockquote&gt;
&lt;p&gt;Etc. etc.
&lt;/div&gt;
</pre>
<p>with the style
<pre>
div.sidebar { writing-mode: horizontal-tb; float: left }
blockquote[lang|=ja] { writing-mode: vertical-rl; height: 10em }
</pre>
<p>defines two flows:
<ol>
<li>The <code>div</code> is a flow root, because it floats. Its flow
consist of the 1st, 2nd and 4th <code>p</code> and the
<code>blockquote</code>.
<li>The <code>blockquote</code> is an <a>orthogonal</a> element
(in this case a <a>vertical</a> box inside a <a>horizontal</a>
parent) and it is thus a flow root. Its flow is formed by the 3rd
<code>p</code>.
</ol>
<p>(The <code>div</code> itself belongs to a third flow, but its flow
root is not shown in the fragment.)
</div>
<p class=note>Note that a flow root is not necessarily block-level, it may
be an ''inline-block'', e.g.
<p class=note>Note that a box belongs to at most one flow. (Some boxes
do not belong to any flow, because they are part of a different kind of
layout, such as table layout.)
<p>An element that generates a flow root box is itself also called a
<span class=index title="flow root!!element">flow root.</span>
<p class=note>Note: The terminology in the CSS level 2 specification
is different. A flow root is called “an element that establishes a
<span class=index title="formatting context|new formatting
context">new formatting context.</span>”
<p class=note>A consequence of this terminology is that an
<em>element,</em> unlike a box, can be the flow root of several
flows. E.g., template elements [[CSS-TEMPLATE-3]] generate several flow
root boxes. Depending on what properties are set on a list item and on
its marker pseudo-element, a list item can also generate zero, one or
two flow roots.
<!--=================================================================-->
<h2 id=boxes>Types of boxes</h2>
<p>The layout of boxes in the flow is in large part determined by the
interplay of the 'display' properties of an element and its parent,
and then fine-tuned with margins and padding.
<!-- The value ''compact'' is missing in [[!CSS-DISPLAY-3]]. -->
<h3 id=block-level-cb-flows>Block-level boxes, containing blocks
and anonymous boxes</h3>
<p>A <dfn>block-level</dfn> box is a box that has a <a>computed
value</a> for 'display' of ''block'', <!--''container'',--> ''list-item'',
''table'', ''table-*'' (i.e., all table boxes, see [[CSS3TBL]]),
''run-in'' (under
certain circumstances), or ''compact'' (under certain circumstances).
<p>An <dfn>inline-level</dfn> box is a box that has a <a>computed
value</a> for 'display' of ''inline'', ''inline-block'',
''inline-table'', ''ruby'', ''run-in'' (under certain circumstances),
or ''compact'' (under certain circumstances).
<p class=issue>[What about the other ruby values?]
<p>An <dfn>anonymous box,</dfn> informally, is a box that cannot be addressed
with CSS selectors. All its properties, except for 'display', have their
default values (either the initial value or inherited). Anonymous boxes are
created when the CSS box model requires a child box with a certain value for
'display', but the child actually has a different value. In that case an
anonymous box of the right kind is created and wraps the child (or children).
<p>This module defines one kind of anonymous box:
<p>A <a>block-level box</a> may contain either <a>line boxes</a> or
<a>block-level</a> boxes, but not both. If necessary, any line boxes
that belong to this box's element are wrapped in one or more (as few
as possible) anonymous boxes with a 'display' of ''block''.
<p>Other modules (e.g., [[CSS3TBL]], [[!CSS3TEXT]]) may also define
anonymous boxes.
<div class=example>
<p>An example of anonymous boxes is this document fragment:
<pre>&lt;p&gt;Somebody whose name I have
forgotten, said, long ago: &lt;q&gt;a box is
a box,&lt;/q&gt; and he probably meant it.&lt;/p&gt;</pre>
<p>with these style rules:
<pre>p { display: block }
q { display: block; margin: 1em }</pre>
<p>The <code>p</code> element has both line boxes and a child box for the
<code>q</code> element, which is a block-level element. The line boxes before
the <code>q</code> are wrapped in an anonymous block-level box and so are the
line boxes after the <code>q</code>. The resulting tree of boxes might be as
follows (refer to the <a href="#When">figure</a>):
<ul>
<li>block-level box [p]
<ul>
<li>block-level box [anonymous]
<ul>
<li>line box: “Somebody&hellip;”
<li>line box: “forgotten&hellip;”
</ul>
<li>block-level box [q]
<ul>
<li>line box: “a box&hellip;”
</ul>
<li>block-level box [anonymous]
<ul>
<li>line box: “and he&hellip;”
</ul>
</ul>
</ul>
<div class=figure id=When>
<p><img alt="The P element has two line boxes before the q and one
after. The first two are wrapped in an anonymous box, the last one
is wrapped in another anonymous box." src="anonymous.png">
<p class="caption">When the fragment is rendered, the text before
the q is wrapped in an anonymous block and the text after the q in
another.
</div>
</div><!--example-->
<p class=note>Note that the anonymous boxes defined in this module are
<a>block-level,</a> but anonymous boxes defined in other
modules may be different.
<h3 id=compact-boxes>Compact boxes</h3>
<p>A compact box is a box that is either put in the margin of the next
box or becomes a block of its own, depending on whether it is small
enough to fit in the margin or not. The typical use case is for lists
where most labels are small enough to fit in the margin, except for a
few.
<div class=example>
<p>An example is the DL list in HTML. Its COMPACT attribute indicates
to the formatter that the labels of the list are good candidates for
display as compact boxes:
<pre>
&lt;h3>Farm animals&lt;/h3>
&lt;dl compact>
&lt;dt>cat
&lt;dd>Lorem ipsum dolor sit amet, consectetaur adipisicing elit&hellip;
&lt;dt>dog
&lt;dd>Ut enim ad minim veniam, quis nostrud exercitation&hellip;
&lt;dt>hippopotamus
&lt;dd>Duis aute irure dolor in reprehenderit in voluptate velit&hellip;
&lt;dt>fly
&lt;dd>Excepteur sint occaecat cupidatat non proident, sunt in&hellip;
&lt;/dl>
</pre>
<p>With a style rule like
<pre>dl[compact] dt {display: compact}</pre>
<p>this might be rendered as in the figure below.
<div class=figure>
<p><img src="compact" alt="[sample rendering]" style="max-width: 24em">
<p class=caption>Three of the four labels are narrow enough to fit
in the left margin. The one that is too wide is converted into a
block.
</div>
</div>
<p class=issue>Add definition.
<!--
<p>Whether a compact box <var>X</var> is displayed as a block or in
the margin is determined as follows. Let <var>Y</var> be the next
sibling element in document order in the same flow as <var>X</var>, if
any. Let <var>W</var> be the hypothetical distance between the <a
title="D edge" >D</a> and <a title="B edge" >B</a> margin edges of
<var>X</var> if the 'display' property of <var>X</var> were set to
''block'' and any ''auto'' on its measure were replaced by
''fit-content''.
<ul>
<li>If there is no such <var>Y</var>, then <var>X</var> is displayed
as a block.
<li>If <var>Y</var> has a 'display' of ''list-item'', then
<var>X</var> is displayed as a block.
<li>If <var>Y</var> is not <em>block-level</em>, then <var>X</var> is
displayed as a block.
<li>If the <a title="D edge" >D</a> margin of Y is less than
<var>W</var>, then <var>X</var> is displayed as a block.
<li>Otherwise, <var>X</var> is displayed in that margin.
</ul>
<p class=issue>Also look at the padding of <var>Y</p>? I.e., check if
the sum of the padding, border and margin is wide enough?
<p>If <var>X</var> is displayed as a block, it is a
<a>block-level</a> element and it is sized and positioned exactly as
if its 'display' had been ''block''.
<p>Otherwise, <var>X</var> is placed such that its
<a title="D edge">D</a> margin edge aligns with the <a>inline-start</a> margin edge of
its <a>containing block</a> and its <a title="A edge" >A</a> border
edge aligns with the <a title="A edge" >A</a> border edge of
<var>Y</var>. It is in this case an <a>inline-level</a> element.
<p class=issue>Do we do anything to avoid that floats overlap with
<var>X</var>? Do we do anything if <var>X</var> is taller than
<var>Y</var>? E.g., adjust the 'min-height' of <var>Y</var>?
<div class=example>
<p>Simple example...
</div>
<div class=example>
<p>This example shows a compact box that has a different <em>writing
mode</em> than its containing block:
<pre>
dl {writing-mode: horizontal-tb; direction: ltr}
dd {margin-left: 3em}
dt {display: compact; writing-mode: vertical-rl}
...
&lt;dl>
&lt;dt>cat
&lt;dd>Lorem ipsum dolor sit amet, consectetaur adipisicing elit&hellip;
&lt;/dl>
</pre>
<p>The margin that is considered is the left margin of the DD, because
the containing block (established by the DL) is
top-to-bottom/left-to-right. That margin is thus compared to the width
of the DT and it is large enough, which leads to a rendering similar
to the figure below.
<div class=figure>
<p><img src="compact-vert" alt="[Sample rendering]" style="max-width: 24em">
<p class=caption>The word “cat” written sideways fits to
the left of the definition.
</div>
</div>
-->
<h3 id=mixing>Mixing the box model with other formatting models</h3>