Reference: CSS 2.2 Visual formatting model
In CSS 2.2 elements generate boxes, these are laid out in certain formatting contexts and positioning schemes.
display
property.position
and
float
properties, and this can also change the type of box that is generated.
Formatting context |
A context where a set of related boxes are laid out according to rules for the particular type
of formatting context. A box can:
|
---|---|
Containing block | Many things in CSS are calculated according to an element's containing block. It is always some ancestor of the element. See here for more details. |
Block formatting context | Lays elements out vertically, see here for more details. |
Block-level box | Participates in a block formatting context. |
Block container box |
Contains all block-level boxes which participate in a block formatting context or
all inline-level boxes which participate in an inline formatting context. All block-level boxes except tables or the principal box of a replaced element are block containers. |
Block box | Box that is block-level AND a block container. |
Anonymous block box | Box that wraps any inline-level boxes in a block container with mixed children. If an inline-level element has block-level children this can cause the children to become direct children of the block container, see examples here. |
Inline formatting context | Lays elements out horizontally in lines, see here for more details. |
Inline-level box | Participates in an inline formatting context. |
Inline box | Box that is inline-level and whose contents participates the same inline formatting context as the box iteself. |
Line box | Contains inline-level boxes in a single line. More are created when the line is too wide. |
Atomic inline-level box | Inline-level boxes that aren't inline boxes, i.e. whose contents doesn't participate in its containing inline formatting context. |
Replaced element | An element whose content is outside the scope of the CSS formatting model, e.g. images or iframes. May
have intrinsic dimensions (width, height or ratio) which are used in some layout calculations. Relevant
elements: img , input type=img , canvas , iframe ,
embed , object , audio , video . |
A block formatting context:
A block container may or may not establish a new block formatting context. What are the practical differences if it does or doesn't?
See here for some live examples.
An inline formatting context:
See here for some live examples.
Formatting context | Established by |
---|---|
Block |
|
Inline | Block containers that contain only inline-level children. |
Table |
Elements that are display: table or display: inline-table generate:
|
The display
property controls the type of box that an element generates. See the tables below for
what types of boxes display
values end up generating.
display |
Block-level box? | Block-container? | Block box? | Inline-level box? | Inline box? | Atomic inline-level box? | Other information |
---|---|---|---|---|---|---|---|
block |
Yes | Yes | Yes | No | No | No | Generates a principal block box. |
inline |
No | No | No | Yes | Yes | No | Generates one or more inline boxes. |
inline-block |
No | Yes | No | Yes | No | Yes | Generates a principal inline-level block container. |
list-item |
Yes | Yes | Yes | No | No | No | Generates a marker box. |
table |
Yes | No | No | No | No | No | Behaves according to table rules. |
inline-table |
No | No | No | Yes | No | Yes | Behaves according to table rules. |
none |
No | No | No | No | No | No | Displays nothing and does not affect page in any way. |
CSS 2.2 lays out boxes in three positioning schemes: normal flow, floats and absolute positioning. Additionally a box can be relatively positioned in some of these schemes.
The position
and float
properties determine how a box is positioned. See the table below
for details:
Positioning scheme | Supported position values |
Supported float values |
Supported display values |
In flow? | Establishes formatting context (for children)? | Notes |
---|---|---|---|---|---|---|
Normal flow |
static relative
|
none (if set to left or right becomes a float)
|
All | Yes | Possibly block, inline or table. See table below. |
Boxes in normal flow belong to (as opposed to
establish) a formatting context of block, inline or table (in CSS 2.2). First laid out in normal flow, then positioned relatively if set. |
Floats |
static relative
|
left right
|
Basically forced to be block-level (see table below). | No (or so it claims, but line boxes flow around it) | Block | First laid out in normal flow, then shifted to the left or right. Line boxes flow along side. Finally positioned relatively if set. |
Absolute positioning |
absolute fixed
|
Forced to none .
|
Basically forced to be block-level (see table below). | No | Block |
Removed from normal flow entirely and positioned relative to containing block.fixed position is the same but its containing block is the viewport.
|
Initial value | Final value |
---|---|
block table table-cell list-item none
|
As specified |
inline inline-block table descendants except table-cell |
block |
inline-table |
table |