W3C

Flexible Box Layout Module

W3C Working Draft, 23 July 2009

This version:
http://www.w3.org/TR/2009/WD-css3-flexbox-20090723/
Latest version:
http://www.w3.org/TR/css3-flexbox/
Editors:
L. David Baron, Mozilla Corporation, dbaron@dbaron.org
Authors and former editors:
Neil Deakin, Mozilla Corporation, enndeakin@gmail.com
Ian Hickson, formerly of Opera Software, ian@hixie.ch
David Hyatt, Apple, hyatt@apple.com

Abstract

The draft describes a CSS box model optimized for interface design. It provides an additional layout system alongside the ones already in CSS. [CSS21] In this new box model, the children of a box are laid out either horizontally or vertically, and unused space can be assigned to a particular child or distributed among the children by assignment of “flex” to the children that should expand. Nesting of these boxes (horizontal inside vertical, or vertical inside horizontal) can be used to build layouts in two dimensions. This model is based on the box model in the XUL user-interface language used for the user interface of many Mozilla-based applications (such as Firefox).

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-flexbox” in the subject, preferably like this: “[css3-flexbox] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This is the first public working draft in the “css3-flexbox” series.

Table of contents


1 Overview

Flexible boxes lay out their children using a constraint-based system that supports both relative flexible sizing and intrinsic sizing. Elements within a box may be intrinsically sized yet have their size increased if additional space is available in the container or have their size reduced if less space is available. In addition, the position and order of elements within a box may be modified.

In CSS, flexible boxes (often referred to only as boxes in this specification) may be created by setting the display property. A block-level box can be specified with a value of box and an inline box can be specified using a value of inline-box.

A block-level box placed inside a block level element is positioned and sized as if it were any other block. An inline-level box placed inside another element is positioned and sized like an inline-block.

Inline children of a flexible box are wrapped in anonymous blocks. All other children are block-level. Elements within boxes are sized intrinsically. This means that the size is determined by the size needed for its content, or, in the case of replaced elements, the size of the replaced element. The intrinsic size of non-replaced elements may be affected by widths and heights specified on the descendants. When determining the intrinsic size of an element, the calculation is done without adding any line breaks, except those necessary (e.g. for content with a white-space of ‘pre’).

Children of boxes may specify their width and height using the width and height properties. The width and height of replaced elements is computed as with replaced elements in inline contexts. Otherwise, if the width is set to ‘auto’, then the used value is the intrinsic size, which may be calculated using a shrink-to-fit algorithm, as with inline-block elements, i.e., it will be given the minimum amount of space required to lay out the element without unnecessary line breaks. Similarly, if the height is set to ‘auto’, then the used value in a similar manner.

An unnested box placed inside an enclosing block will obey all the usual sizing rules of the block. For example, setting the width of an unnested box to 100% ensures that the box is the maximum width permitted by the enclosing block (ignoring any margins, border and padding on that box).

The difference between boxes and blocks is in how the children of the element are laid out. Children of boxes are all block-level (inline-level contents get wrapped in anonymous blocks, as described in the section on inlines and blocks). These blocks then get stacked in the direction specified by the box-orient and box-direction properties, and order specified by the box-ordinal-group property. They are aligned within the box according to the box-align property. The boxes may then flex or be distributed as specified by the box-flex, box-flex-group, box-pack and box-lines properties.

The float and clear properties do not apply to children of box elements, so the used value for these properties is ‘none’. These properties do apply to the box itself if it is not a child of another box.

For example:

   <p style="width: 400px;">
     <button style="width: 200px;">Child 1</button>
     <button style="width: 100px;">Child 2</button>
   </p>

In this example, the first button is 200 pixels wide and the second button is 100 pixels wide. As the box is 400 pixels wide, there is extra space left over which is placed after the two buttons but inside the box. The placement of this extra space may be modified using the various box properties described in the next sections. Neither the height of the buttons nor the height of the box are specified, so the height is computed intrinsically from the content. In this case, the buttons will be the height needed to display the label and button border, and the box will also be this height plus any margins around the buttons.

Elements within a box may use the overflow property to control whether a scrolling mechanism appears when the children of a box overflow. A scrolling mechanism may be displayed when flexible elements are reduced below their minimum intrinsic size when the overflow property is set to auto or scroll. If overflow is hidden, the element will be clipped instead. Note that the initial value is visible, which is typically not the preferred effect in user interfaces.

2 Orientation

Name:   box-orient
Value:   horizontal | vertical | inline-axis | block-axis | inherit
Initial:   inline-axis
Applies to:   box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

A box may lay out its children either horizontally or vertically.

horizontal
The box displays its children from left to right in a horizontal line.
vertical
The box displays its children from stacked from top to bottom vertically.
inline-axis
The box displays its children along the inline axis.
block-axis
The box displays its children along the block axis.

The inline and block axes are the writing-mode dependent keywords which, in English, map to horizontal and vertical respectively.

3 Display order

Children within a horizontally oriented box are, by default, displayed from left to right in the same order as they appear in the source document. Children within a vertically oriented box are displayed top to bottom in the same order. The box-direction and box-ordinal-group properties may be used to change this ordering.

Name:   box-direction
Value:   normal | reverse | inherit
Initial:   normal
Applies to:   box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

The box-direction property specifies the direction in which children of a box are displayed.

normal
A box with a computed value of horizontal for box-orient displays its children from left to right. A box with a computed value of vertical displays its children from top to bottom.
reverse
A box with a computed value of horizontal for box-orient displays its children from right to left. A box with a computed value of vertical displays its children from bottom to top.

If the computed value for the box's ‘direction’ property is ‘normal’, a child's left edge is placed next to the right edge of the previous child, or the left edge of the box for the first child. If the computed value for the box's ‘direction’ property is ‘reverse’, a child's right edge is placed next to the left edge of the previous child, or the right edge of the box for the first child.

When the computed value for the overflow property is ‘visible’, ‘scroll’ or ‘auto’, the content may overflow the container. If the computed value for direction is normal, the content will overflow over the right or bottom side. If the computed value for direction is reverse, the content will overflow over the left or top side.

Name:   box-ordinal-group
Value:   <integer>
Initial:   1
Applies to:   children of box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

The children of a box element may be assigned to ordinal groups using the box-ordinal-group property. This property is a natural number value with an initial value is 1.

Ordinal groups can be used in conjunction with the ‘box-direction’ property to control the order in which the direct children of a box appear. When the computed box-direction is normal, a box will display its elements starting from the lowest numbered ordinal group and ensure that those elements appear to the left (for horizontal boxes) or at the top (for vertical boxes) of the container. Elements with the same ordinal group are flowed in the order they appear in the source document tree. In the reverse direction, the ordinal groups are examined in the same order, except the elements appear reversed.

This example shows how ordinal groups might be used.

    #div1 { display: box; }
    #span1 { box-ordinal-group: 2; }
    #span3 { box-ordinal-group: 2; }
    #span4 { box-ordinal-group: 1; }

    <div id="div1">
      <span id="span1" >Sentence One</span>
      <span id="span2" >Sentence Two</span>
      <span id="span3" >Sentence Three</span>
      <span id="span4" >Sentence Four</span>
    </div>

The first ordinal group, 1, contains span2 and span4. As span2 does not specify an ordinal group, it will default to 1. The elements will be displayed in document order, so span2 will be displayed before span4. The second ordinal group, 2, contains the remaining two spans. The resulting display order will be:

    span2 span4 span1 span3

Elements within a box can use the visibility property to render themselves invisible. Boxes behave like tables in that the value collapsed can be used to specify that an element within a box should not take up any space at all. The computed width and height of a collapsed element are both 0, and the element is not considered when calculating flexibility. Other non-collapsed flexible elements may expand as needed to fill in any space left open by a collapsed element.

4 Alignment

Name:   box-align
Value:   start | end | center | baseline | stretch
Initial:   stretch
Applies to:   box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

When the size of the containing box is larger than the size of a child, extra space will be available. The box-align property specifies how a box's children are placed and aligned along the direction perpendicular to the box orientation, and where the extra space, if any, is positioned. For horizontal orientation, it specifies how the children are positioned vertically. For vertical orientation, it specifies how the children are positioned horizontally.

The amount of extra space may be different for each child. For example, if the containing box has a height of 200 pixels, and it contains two children at 100 and 150 pixels respectively, there will be 100 pixels of extra space for the first child and 50 pixels of space for the second child.

The following values are valid for the box-align property, but see the text afterward for more specifics as to how children are positioned.

start
For normal direction boxes, the top edge of each child is placed along the top of the box. Extra space is placed below the element. For reverse direction boxes, the bottom edge of each child is placed along the bottom of the box. Extra space is placed above the element.
end
For normal direction boxes, the bottom edge of each child is placed along the bottom of the box. Extra space is placed above the element. For reverse direction boxes, the top edge of each child is placed along the top of the box. Extra space is placed below the element.
center
Any extra space is divided evenly, with half placed above the child and the other half placed after the child.
baseline
If this box orientation is inline-axis or horizontal, all children are placed with their baselines aligned, and extra space placed before or after as necessary. For block flows, the baseline of the first non-empty line box located within the element is used. For tables, the baseline of the first cell is used. The children, once aligned on their baselines, should then be placed into the box so that the child with the earliest extent margin has its top margin edge (or bottom margin edge for reverse direction boxes) flush with the top (or bottom) edge of the box's content area. If the box does not have an ‘auto’ height, overflow will always be on the bottom (or top for reverse direction boxes) edge. If the box orientation is block-axis or vertical, then baseline is interpreted as center.
stretch
The height of each child is adjusted to that of the containing block. However, note the text below.

The following rules should be applied when determining the size of children inside boxes in the dimension opposite to the orientation of the box. Exceptions to these rules are noted under each of the possible values of ‘box-align’ below.

All normal flow children of boxes will attempt to satisfy the following sizing equation when placed inside vertical boxes if and only if at least one of the specified values is auto:

margin-left’ + ‘border-left-width’ + ‘padding-left’ + ‘width’ + ‘padding-right’ + ‘border-right-width’ + ‘margin-right’ = width of containing box

and a similar equation for height when placed inside horizontal boxes:

margin-top’ + ‘border-top-width’ + ‘padding-top’ + ‘height’ + ‘padding-bottom’ + ‘border-bottom-width’ + ‘margin-bottom’ = height of containing box

If all the values in the equation are specified (i.e., if there are no auto values), then the equation above does not apply, and the specified values should be used. If applying the equation results in a negative value for width/height, then that value is set to 0, and those values should be used.

If the value of box-align is stretch, then elements obey the sizing rules specified above. This is the default value. If both width/height and one or both margin values are ‘auto’, the ‘auto’ margins are set to 0 and the equation is used to find a value for width/height. Otherwise, if both margins are ‘auto’, then the equation is solved under the additional constraint that the two margins must get the same value. If neither the width/height or either margin is auto, then the extra space is positioned as if box-align was start.

If the value of box-align is start, center, baseline, or end, and a value of ‘auto’ is specified for width/height, then the intrinsic size (or shrink to fit size) of the element should be used. If one margin has the value of auto, then the equations above should be used. If both margins have a value of auto then the above equations should be used, with the space being divided evenly between the two margins. These rules are similar to the rules for replaced elements inside blocks.

If the above would cause the width or height to greater than the computed maximum width or height, then the width or height is set to the maximum width or height, and the auto margins increased by the extra amount left over. If both margins are auto, they should both be increased by the same amount. If neither margin is auto, the extra space is placed as necessary depending on the box-align value.

Once the sizes are computed, the margin boxes of the elements should be aligned at the start, center, baseline, or end of the box. For horizontal boxes, this typically means the top, center, baseline or bottom of the box. For vertical boxes, the start and end alignments depend upon the direction. For LTR, the elements would be aligned at the left side of the box for start and the right side for end. For RTL, the elements would be aligned at the right of the box for start and the left side for end.

5 Flexibility

Each element directly within a box may be made either flexible and inflexible. Flexible elements may grow when the containing box has extra space available after the size of all of its children have been computed, and shrink if the size would cause the containing box to overflow, yet the preferred width of the flexible element is larger than its minimum width. Inflexible elements do not change in size, even when there is extra space left over in the box.

The flexibility only affects the size in the axis along the box's orientation. (e.g. the flexibility only affects the width in a horizontal box, and only the height in a vertical box.)

Name:   box-flex
Value:   <number>
Initial:   0.0
Applies to:   in-flow children of box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value
Name:   box-flex-group
Value:   <integer>
Initial:   1
Applies to:   in-flow children of box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

An element is flexible when the box-flex property is specified. The box-flex property is a floating point value representing the flexibility of the element. Its initial value is 0, which indicates that the element is inflexible. Elements that are flexible can shrink or grow as the box shrinks and grows. Whenever there is extra space left over in a box, the flexible elements are expanded to fill that space. All flex is relative. For example, a child with a box-flex of 2 is twice as flexible as a child with a box-flex of 1. A negative value for box-flex is not allowed.

Flexible elements can be assigned to flex groups using the box-flex-group property. This property is a natural number value (the first flex group is 1 and higher values are later flex groups). The initial value is 1.

In a horizontally oriented box, the preferred width of each child is computed. If the width of the margin box of each child is equal to the width of the containing block, then there is no extra space available, so the preferred widths are used for each child. If the width of the margin box adds up to a value smaller than the width of the containing block, then extra space is available. This extra space is divided up among the flexible children, as described below. If the width of the margin box adds up to a value larger than the width of the containing block, then the flexible children shrink as much as necessary to prevent overflow.

Flexibility only applies to elements in normal flow. As absolute and fixed positioned elements are not in flow, any flexibility or flexgroup specified on them is ignored. In addition, as the float property does not apply to children of flexible boxes, they are considered part of normal flow and flexibility does apply.

When dividing up extra space, first take all elements within the first flex group. Each element within that group should be given extra width based on the ratio of that element's flexibility compared to the flexibility of other elements within the same flex group.

However, if the preferred width of the element plus the extra width allotted to it is larger than the maximum width of the element, then the width is set to that maximum width, and any remaining extra width beyond that is divided up among the other children.

In this example there is 60 pixels of extra space available in the containing box.

    #div1 { display: box; width: 300px; }
    #button1 { box-flex: 1.0; width: 100px; }
    #button2 { box-flex: 2.0; width: 140px; }

    <div id="div1">
      <button id="button1">Hello</button>
      <button id="button2">Goodbye</button>
    </div>

As both child buttons are flexible, the extra space will be divided up between them. The first child button has a flexibility of 1.0 and the second child button has a flexibility of 2.0. The first button will receive 20 pixels of extra width and the second button will receive 40 pixels of extra width, maintaining the same ratio of extra width to flexibility values. This extra width is added to the preferred size of the element.

However, if the second button had a maximum width of 150 pixels, it could only grow by 10 pixels before hitting this maximum size, so the remaining 30 pixels would instead be given to the first element, breaking the flexibility ratio.

More specifically, the percentage of extra space that an element may receive is calculated as follows:

box-flex of child’ / ‘total of box-flex values of child and all siblings

If the width of all flexible children within the group has been increased to their maximum widths, the process repeats for the children within the next flex group, using any space left over from the previous flex group. Once there are no more flex groups, and there is still space remaining, the extra space is divided within the containing box according to the box-pack property.

If the box would overflow after the preferred width of the children have been computed, then width is removed from flexible elements in a manner similar to that used when adding extra space. Each flex group is examined in turn and space is removed according to the ratio of the flexibility of each element. Elements do not shrink below their minimum widths.

If all children have been shrunk to their minimum sizes, then the box overflows, although if the box-lines property is set to multiple, the box may be able to move elements to additional lines to prevent this.

For vertically oriented boxes, the algorithm as described above is similar except using the height instead of the width.

When a child box of a horizontally oriented box contains an inline element, it is likely that shrinking the width of the element due to flexibility may cause the inline element to grow in height, as the text within it may need to wrap to additional lines.

Examples:

    #div1 { display: box; box-orient: vertical; height: 200px }

    <div id="div1">
      <button>Cat</button>
      <button style="box-flex: 1">Piranha</button>
      <button>Antidisestablishmentarianism</button>
    </div>

In the example above, the box is 200 pixels tall and is more than enough room for the three buttons. Because the first and third buttons are inflexible, they remain the same size, which is their intrinsic size. The second button is specified as being flexible, and because it is the only flexible element in the box, it receives all of the extra space.

    <div style="display: box; box-orient: vertical;">
      <button style="box-flex: 1; height: 1000px;">
        Cat
      </button>
    </div>

In this example, if the height of the box is reduced, for instance, because the user resized the containing viewport, the height of the flexible button also shrinks with the box, despite the specification of 1000 pixels as the preferred height. It continues to shrink until the minimum required height for the button is reached, which here will likely be the height needed to display the button's label and border. After that, the button can shrink no further. Elements within a box can therefore have their own notions of minimum and maximum intrinsic sizes. In the above example, the button could not get any smaller than the minimum height required to draw its borders and its text.

    #div1 { display: box; }
    #iframe1 { box-flex: 1; min-width: 100px; max-width: 300px; height: 300px; }

    <div id="div1">
      <iframe id="iframe1" src="http://www.mozilla.org/"/>
    </div>

In this example, the iframe has a minimum width of 100 pixels and a maximum width of 300 pixels. If the containing box is less than 100 pixels wide, the iframe will overflow its containing div. If the containing box is between 100 pixels and 300 pixels inclusive, the width of the iframe would be set to that size, minus any necessary padding, borders and margins. If the width of the containing box is larger than 300 pixels, the extra space will be added inside the div. The extra space is added after the iframe inside the box.

    <p style="display: box;">
      <button style="box-flex: 1; max-width: 50px;">Child 1</button>
      <button style="box-flex: 1; min-width: 50px;">Child 2</button>
    </p>

In this example, the box has been stretched so that it is very wide. The first child has a maximum width of 50 pixels, and it divides the excess space equally with the second child until its maximum width has been reached. After that, since it is not allowed to grow any further, the remaining space all goes to the second child.

6 Packing along the box axis

Name:   box-pack
Value:   start | end | center | justify
Initial:   start
Applies to:   box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

When all of the elements within a box are inflexible or when all elements have grown to their maximum sizes and can stretch no further, extra space may be left over in the box. The box-pack property may be used to dictate how any additional space along the box-axis should be distributed between elements. The box-pack property does not affect the position of elements in the opposite direction. That is, box-pack affects only the horizontal position in horizontally oriented boxes and only the vertical position in vertically oriented boxes.

start
For normal direction boxes, the left edge of the first child is placed at the left side, with all extra space placed after the last child. For reverse direction boxes, the right edge of the last child is placed at the right side, with all extra space placed before the first child.
end
For normal direction boxes, the right edge of the last child is placed at the right side, with all extra space placed before the first child. For reverse direction boxes, the left edge of the first child is placed at the left side, with all extra space placed after the last child.
center
The extra space is divided evenly, with half placed before the first child and the other half placed after the last child.
justify
The space is divided evenly in-between each child, with none of the extra space placed before the first child or after the last child. If there is only one child, treat the pack value as if it were start.
    <p style="box-align: center; box-pack: center; width: 300px; height: 300px;">
      <button>centered</button>
    </p>

In the example above, the button is centered within the box using the box-align and box-pack properties together. The former centers the button vertically, and the latter centers the button horizontally.

7 Multiple Lines

Name:   box-lines
Value:   single | multiple
Initial:   single
Applies to:   box elements
Inherited:   no
Percentages:   no
Media:   visual
Computed value:   specified value

By default a horizontal box will lay out its children in a single row, and a vertical box will lay out its children in a single column. This behavior can be changed using the box-lines property. The default value is single, which means that all elements will be placed in a single row or column, and any elements that don't fit will simply be considered overflow.

If a value of multiple is specified, however, then the box is allowed to expand to multiple lines (that is, multiple rows or columns) in order to accommodate all of its children. The box must attempt to fit its children on as few lines as possible by shrinking all elements down to their minimum widths or heights if necessary.

If the children in a horizontal box still do not fit on a line after being reduced to their minimum widths, then children are moved one by one onto a new line, until the elements remaining on the previous line fit. This process can repeat to an arbitrary number of lines. If a line contains only a single element that doesn't fit, then the element should stay on that line and overflow out of the box. The later lines are placed below the earlier lines in normal direction boxes and above in reverse direction boxes. The height of a line is the height of the largest child in that line. No additional space appears between the lines apart from the margins on the largest elements in each line. For calculating the height of a line, margins with a computed value of auto should be treated as having a value of 0.

A similar process occurs for children in a vertical box. Later lines in normal direction boxes are placed to the right of earlier lines and to the left in reverse direction boxes.

Once the number of lines has been determined, elements with a computed value for box-flex other than 0 stretch as necessary in an attempt to fill the remaining space on the lines. Each line computes flexes independently, so only elements on that line are considered when evaluating flex and flex-groups. The packing of elements in a line, as specified by the box-pack property, is also computed independently for each line.

This example shows four buttons that do not fit horizontally.

    #div1 {
      display: box;
      box-lines: multiple;
      width: 300px;
    }
    button {
      box-flex: 1.0;
      width: 100px;
    }

    <div id="div1">
      <button id="button1">Elephant</button>
      <button id="button2">Tiger</button>
      <button id="button1">Antelope</button>
      <button id="button2">Wildebeest</button>
    </div>

The buttons are shrunk to their minimum widths, in this case calculated intrinsically. Assume that the four buttons have a minimum intrinsic width of 80 pixels. This will allow the first three buttons to fit in 240 pixels with 60 pixels left over of remaining space. Because the box-lines property has a specified value of multiple, the fourth button may be moved onto a second line.

Flexibility is applied to each element, separately for each line. The first line has 60 pixels of remaining space, so each of the three buttons on that line will receive 20 pixels of extra width. The remaining button on a line of its own will stretch to the entire width of the containing box, or 300 pixels.

If the box was resized, the buttons may rearrange onto different lines as necessary. If the style rules in the example above were changed to the following:

    #div1 {
      display: box;
      box-lines: multiple;
      box-pack: center;
      width: 300px;
    }
    button {
      box-flex: 1.0;
      width: 90px;
      max-width: 90px;
    }

Now, each of the buttons will only stretch to include an additional 10 pixels of width, as the maximum width of 90 pixels is only 10 pixels larger than the minimum intrinsic width of the buttons. The remaining 30 pixels of space left over is divided up and placed inside the box outside of the buttons, as the value of box-pack is center. The fourth button will also appear at 90 pixels wide, centered within the box.

7.1 Multiple Lines and alignment

Note: not sure what this should do yet.

The box-align property may be used to control the alignment of children in multiple line boxes, as with single line boxes. If box-align is start, the first line is placed along the top or left edge of the box (or the bottom or right edge for reverse direction boxes), and additional lines are placed afterward with no extra spacing between them. Each element within a line is placed with that same edge aligned. For instance, in a normal direction, vertical box, the top edges of each element are aligned with the top edge of the line it is within. If box-align is end, the first line is placed along the bottom or right edge of the box (or the top or left edge for reverse direction boxes), and additional lines are placed before. Each element within a line is placed with that same edge aligned.

If box-align is center, the extra spacing is divided evenly on both sides of the box, and the lines placed between this space. Elements are centered within their lines. If box-align is baseline, the elements in a line are aligned by their baselines as with single line boxes, with space placed before or after each element in the line as necessary. The first line is placed as with the value start.

If box-align is stretch, then each line is stretched equally, and the elements within each line are stretched to the size of the largest item in that line.

8 Relationship to Inlines and blocks

A box placed in a block is considered a block level element and will be positioned and sized according to the rules for CSS 2.1 (section 10.3 for widths). Specifically, the following equation applies for calculating the width:

margin-left’ + ‘border-left-width’ + ‘padding-left’ + ‘width’ + ‘padding-right’ + ‘border-right-width’ + ‘margin-right’ + scrollbar width (if any) = width of containing block

All children of ‘box’ elements are block-level. If inline-level elements are nested inside boxes, they get wrapped in an anonymous block which then takes part in the box layout.

If the width of a child of a flexible box is computed as ‘auto’, then the used value is the shrink-to-fit width. This calculation is done similarly to how the width of a floating element is determined. Otherwise if the width has a computed value, then that value is used as the width.

In a horizontally oriented box, the flexibility is then applied which may increase or decrease the used width. In a vertically oriented box, the position and size of the child may be adjusted by the value of the container's box-align property.

The min-width, min-height, max-width, and max-height CSS properties act the same on boxes as on blocks.

The height of a child of a flexible box is determined in the same manner as specified in the CSS specification. The used height may further be adjusted by the container's box-align property in a horizontally oriented box, or the flexibility in a vertically oriented box.

Note that it is possible for a horizontally oriented box that the height of a flexible child containing an inline descendant will change when the flexibility is applied. Similarly, in a vertically oriented box, the height of a child may change based on the box-align property of the box.

Note: more details are needed here, and the relationship with other CSS constructs is yet to be defined.


Acknowledgments

[This section will contain further acknowledgments.]

Thanks for feedback from Shinichiro Hamaji.

References

Normative references

[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 23 April 2009. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2009/CR-CSS2-20090423/

Index

Property index

Property Values Initial Applies to Inh. Percentages Media
box-align start | end | center | baseline | stretch stretch box elements no no visual
box-direction normal | reverse | inherit normal box elements no no visual
box-flex <number> 0.0 in-flow children of box elements no no visual
box-flex-group <integer> 1 in-flow children of box elements no no visual
box-lines single | multiple single box elements no no visual
box-ordinal-group <integer> 1 children of box elements no no visual
box-orient horizontal | vertical | inline-axis | block-axis | inherit inline-axis box elements no no visual
box-pack start | end | center | justify start box elements no no visual