1. Introduction
This section is not normative.
CSS 2.1 defined four layout modes — algorithms which determine the size and position of boxes based on their relationships with their sibling and ancestor boxes:
-
block layout, designed for laying out documents
-
inline layout, designed for laying out text
-
table layout, designed for laying out 2D data in a tabular format
-
positioned layout, designed for very explicit positioning without much regard for other elements in the document
This module introduces a new layout mode, flex layout, which is designed for laying out more complex applications and webpages.
1.1. Overview
This section is not normative.
Flex layout is superficially similar to block layout. It lacks many of the more complex text- or document-centric properties that can be used in block layout, such as floats and columns. In return it gains simple and powerful tools for distributing space and aligning content in ways that web apps and complex web pages often need. The contents of a flex container:
-
can be laid out in any flow direction (leftwards, rightwards, downwards, or even upwards!)
-
can have their display order reversed or 'order|rearranged' at the style layer (i.e., visual order can be independent of source and speech order)
-
can be laid out linearly along a single (main) axis or wrapped into multiple lines along a secondary (cross) axis
-
can “flex” their sizes to respond to the available space
-
can be aligned with respect to their container or each other on the secondary (cross)
-
can be dynamically collapsed or uncollapsed along the main axis while preserving the container’s cross size
-
The catalog uses flex layout to lay out rows of items horizontally, and to ensure that items within a row are all equal-height. Each entry is then itself a column flex container, laying out its contents vertically.
-
Within each entry, the source document content is ordered logically with the title first, followed by the description and the photo. This provides a sensible ordering for speech rendering and in non-CSS browsers. For a more compelling visual presentation, however, order is used to pull the image up from later in the content to the top, and align-self is used to center it horizontally.
-
An auto margin above the purchase button forces it to the bottom within each entry box, regardless of the height of that item’s description.
#deals{ display : flex; /* Flex layout so items have equal height */ flex-flow: row wrap; /* Allow items to wrap into multiple lines */ } .sale-item{ display : flex; /* Lay out each item using flex layout */ flex-flow: column; /* Lay out item's contents vertically */ } .sale-item > img{ order : -1 ; /* Shift image before other content (in visual order) */ align-self: center; /* Center the image cross-wise (horizontally) */ } .sale-item > button{ margin-top : auto; /* Auto top margin pushes button to bottom */ }
< section id = "deals" > < section class = "sale-item" > < h1 > Computer Starter Kit</ h1 > < p > This is the best computer money can buy, if you don’t have much money.< ul > < li > Computer< li > Monitor< li > Keyboard< li > Mouse</ ul > < img src = "images/computer.jpg" alt = "You get: a white computer with matching peripherals." > < button > BUY NOW</ button > </ section > < section class = "sale-item" > …</ section > …</ section >
Computer Starter Kit
This is the best computer money can buy, if you don’t have much money.
- Computer
- Monitor
- Keyboard
- Mouse
Printer
Only capable of printing ASCII art.
- Paper and ink not included.
1.2. Module interactions
This module extends the definition of the display property [CSS2], adding a new block-level and new inline-level display type, and defining a new type of formatting context along with properties to control its layout. None of the properties defined in this module apply to the ::first-line or ::first-letter pseudo-elements.
The CSS Box Alignment Module extends and supersedes the definitions of the alignment properties (justify-content, align-items, align-self, align-content) introduced here.
Tests
1.3. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with 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 CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
2. Flex Layout Box Model and Terminology
A flex container is the box generated by an element with a computed display of flex or inline-flex. In-flow children of a flex container are called flex items and are laid out using the flex layout model.
Unlike block and inline layout, whose layout calculations are biased to the block and inline flow directions, flex layout is biased to the flex directions. To make it easier to talk about flex layout, this section defines a set of flex flow–relative terms. The flex-flow value and the writing mode determine how these terms map to physical directions (top/right/bottom/left), axes (vertical/horizontal), and sizes (width/height).
- main axis
- main dimension
- The main axis of a flex container is the primary axis along which flex items are laid out. It extends in the main dimension.
- main-start
- main-end
- The flex items are placed within the container starting on the main-start side and going toward the main-end side.
- main size
- main size property
-
The main size of a flex container or flex item
refers to its width or height,
whichever is in the main dimension.
Its main size property
is either its width or height property,
whichever is in the main dimension.
Likewise, its min and
max main size properties
are its min-width/max-width or min-height/max-height properties,
whichever are in the main dimension,
and determine its min/max main size.
In flex layout, the main size is controlled by the flex property rather than directly by the main size property.
Note: This means any references to a flex item’s used size in the main dimension (width, height, inline size, block size) refers to its post-flexing main size.
- cross axis
- cross dimension
- The axis perpendicular to the main axis is called the cross axis. It extends in the cross dimension.
- cross-start
- cross-end
- Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side.
- cross size
- cross size property
- The cross size of a flex container or flex item refers to its width or height, whichever is in the cross dimension. Its cross size property is either its width or height property, whichever is in the cross dimension. Likewise, its min and max cross size properties are its min-width/max-width or min-height/max-height properties, whichever are in the cross dimension, and determine its min/max cross size.
Additional sizing terminology used in this specification is defined in CSS Intrinsic and Extrinsic Sizing. [CSS-SIZING-3]
3. Flex Containers: the flex and inline-flex display values
| Name: | display |
|---|---|
| New values: | flex | inline-flex |
- flex
-
This value causes an element to generate a flex container box
that is block-level when placed in flow layout.
Tests
- baseline-synthesis-001.html (live test) (source)
- baseline-synthesis-002.html (live test) (source)
- baseline-synthesis-003.html (live test) (source)
- baseline-synthesis-004.html (live test) (source)
- baseline-synthesis-vert-lr-line-under.html (live test) (source)
- display-flex-001.htm (live test) (source)
- dynamic-change-simplified-layout-002.html (live test) (source)
- dynamic-change-simplified-layout.html (live test) (source)
- fixedpos-video-in-abspos-quirk-crash.html (live test) (source)
- flexbox_flex-0-0-0.html (live test) (source)
- flexbox_flex-0-0-0-unitless.html (live test) (source)
- flexbox_flex-0-0-1-unitless-basis.html (live test) (source)
- flexbox_flex-0-0-auto.html (live test) (source)
- flexbox_flex-0-0-auto-shrink.html (live test) (source)
- flexbox_flex-0-0.html (live test) (source)
- flexbox_flex-0-0-N.html (live test) (source)
- flexbox_flex-0-0-Npercent.html (live test) (source)
- flexbox_flex-0-0-Npercent-shrink.html (live test) (source)
- flexbox_flex-0-0-N-shrink.html (live test) (source)
- flexbox_flex-0-0-N-unitless-basis.html (live test) (source)
- flexbox_flex-0-1-0.html (live test) (source)
- flexbox_flex-0-1-0-unitless.html (live test) (source)
- flexbox_flex-0-1-1-unitless-basis.html (live test) (source)
- flexbox_flex-0-1-auto.html (live test) (source)
- flexbox_flex-0-1-auto-shrink.html (live test) (source)
- flexbox_flex-0-1.html (live test) (source)
- flexbox_flex-0-1-N.html (live test) (source)
- flexbox_flex-0-1-Npercent.html (live test) (source)
- flexbox_flex-0-1-Npercent-shrink.html (live test) (source)
- flexbox_flex-0-1-N-shrink.html (live test) (source)
- flexbox_flex-0-1-N-unitless-basis.html (live test) (source)
- flexbox_flex-0-auto.html (live test) (source)
- flexbox_flex-0-N-0.html (live test) (source)
- flexbox_flex-0-N-0-unitless.html (live test) (source)
- flexbox_flex-0-N-auto.html (live test) (source)
- flexbox_flex-0-N-auto-shrink.html (live test) (source)
- flexbox_flex-0-N.html (live test) (source)
- flexbox_flex-0-N-N.html (live test) (source)
- flexbox_flex-0-N-Npercent.html (live test) (source)
- flexbox_flex-0-N-Npercent-shrink.html (live test) (source)
- flexbox_flex-0-N-N-shrink.html (live test) (source)
- flexbox_flex-1-0-0.html (live test) (source)
- flexbox_flex-1-0-0-unitless.html (live test) (source)
- flexbox_flex-1-0-auto.html (live test) (source)
- flexbox_flex-1-0-auto-shrink.html (live test) (source)
- flexbox_flex-1-0.html (live test) (source)
- flexbox_flex-1-0-N.html (live test) (source)
- flexbox_flex-1-0-Npercent.html (live test) (source)
- flexbox_flex-1-0-Npercent-shrink.html (live test) (source)
- flexbox_flex-1-0-N-shrink.html (live test) (source)
- flexbox_flex-1-1-0.html (live test) (source)
- flexbox_flex-1-1-0-unitless.html (live test) (source)
- flexbox_flex-1-1-auto.html (live test) (source)
- flexbox_flex-1-1-auto-shrink.html (live test) (source)
- flexbox_flex-1-1.html (live test) (source)
- flexbox_flex-1-1-N.html (live test) (source)
- flexbox_flex-1-1-Npercent.html (live test) (source)
- flexbox_flex-1-1-Npercent-shrink.html (live test) (source)
- flexbox_flex-1-1-N-shrink.html (live test) (source)
- flexbox_flex-1-N-0.html (live test) (source)
- flexbox_flex-1-N-0-unitless.html (live test) (source)
- flexbox_flex-1-N-auto.html (live test) (source)
- flexbox_flex-1-N-auto-shrink.html (live test) (source)
- flexbox_flex-1-N.html (live test) (source)
- flexbox_flex-1-N-N.html (live test) (source)
- flexbox_flex-1-N-Npercent.html (live test) (source)
- flexbox_flex-1-N-Npercent-shrink.html (live test) (source)
- flexbox_flex-1-N-N-shrink.html (live test) (source)
- flexbox_flex-N-0-0.html (live test) (source)
- flexbox_flex-N-0-0-unitless.html (live test) (source)
- flexbox_flex-N-0-auto.html (live test) (source)
- flexbox_flex-N-0-auto-shrink.html (live test) (source)
- flexbox_flex-N-0.html (live test) (source)
- flexbox_flex-N-0-N.html (live test) (source)
- flexbox_flex-N-0-Npercent.html (live test) (source)
- flexbox_flex-N-0-Npercent-shrink.html (live test) (source)
- flexbox_flex-N-0-N-shrink.html (live test) (source)
- flexbox_flex-N-1-0.html (live test) (source)
- flexbox_flex-N-1-0-unitless.html (live test) (source)
- flexbox_flex-N-1-auto.html (live test) (source)
- flexbox_flex-N-1-auto-shrink.html (live test) (source)
- flexbox_flex-N-1.html (live test) (source)
- flexbox_flex-N-1-N.html (live test) (source)
- flexbox_flex-N-1-Npercent.html (live test) (source)
- flexbox_flex-N-1-Npercent-shrink.html (live test) (source)
- flexbox_flex-N-1-N-shrink.html (live test) (source)
- flexbox_flex-N-N-0.html (live test) (source)
- flexbox_flex-N-N-0-unitless.html (live test) (source)
- flexbox_flex-N-N-auto.html (live test) (source)
- flexbox_flex-N-N-auto-shrink.html (live test) (source)
- flexbox_flex-N-N.html (live test) (source)
- flexbox_flex-N-N-N.html (live test) (source)
- flexbox_flex-N-N-Npercent.html (live test) (source)
- flexbox_flex-N-N-Npercent-shrink.html (live test) (source)
- flexbox_flex-N-N-N-shrink.html (live test) (source)
- flexbox_flex-formatting-interop.html (live test) (source)
- flexbox_generated-flex.html (live test) (source)
- flexbox_generated.html (live test) (source)
- flexbox_generated-nested-flex.html (live test) (source)
- flexbox-iframe-intrinsic-size-001.html (live test) (source)
- flexbox_item-bottom-float.html (live test) (source)
- flexbox_item-clear.html (live test) (source)
- flexbox_item-float.html (live test) (source)
- flexbox_item-top-float.html (live test) (source)
- flexbox_item-vertical-align.html (live test) (source)
- flexbox_block.html (live test) (source)
- flexbox_box-clear.html (live test) (source)
- flexbox_display.html (live test) (source)
- flexbox_fbfc2.html (live test) (source)
- flexbox_fbfc.html (live test) (source)
- flexbox_nested-flex.html (live test) (source)
- flexbox-root-node-001a.html (live test) (source)
- flexbox-root-node-001b.html (live test) (source)
- flexbox_stf-fixpos.html (live test) (source)
- flexbox_stf-float.html (live test) (source)
- flexbox_stf-inline-block.html (live test) (source)
- flexbox_stf-table-caption.html (live test) (source)
- flexbox_stf-table-cell.html (live test) (source)
- flexbox_stf-table.html (live test) (source)
- flexbox_stf-table-row-group.html (live test) (source)
- flexbox_stf-table-row.html (live test) (source)
- flexbox_stf-table-singleline-2.html (live test) (source)
- flexbox_stf-table-singleline.html (live test) (source)
- flexbox_table-fixed-layout.html (live test) (source)
- flexbox-with-multi-column-property.html (live test) (source)
- flexbox_computedstyle_display.html (live test) (source)
- grid-flex-item-001.html (live test) (source)
- grid-flex-item-002.html (live test) (source)
- grid-flex-item-003.html (live test) (source)
- grid-flex-item-004.html (live test) (source)
- grid-flex-item-005.html (live test) (source)
- grid-flex-item-006.html (live test) (source)
- grid-flex-item-007.html (live test) (source)
- flexbox_interactive_flex-transitions.html (manual test) (source)
- flexbox_interactive_order-transitions-2.html (manual test) (source)
- flexbox_interactive_order-transitions.html (manual test) (source)
- nested-flex-image-loading-invalidates-intrinsic-sizes.html (live test) (source)
- percentage-margins-001.html (live test) (source)
- stretch-after-sibling-size-change.html (live test) (source)
- stretched-child-in-nested-flexbox-001.html (live test) (source)
- stretched-child-in-nested-flexbox-002.html (live test) (source)
- stretched-child-in-nested-flexbox-003.html (live test) (source)
- stretched-child-shrink-on-relayout.html (live test) (source)
- stretch-flex-item-checkbox-input.html (live test) (source)
- stretch-flex-item-radio-input.html (live test) (source)
- stretching-orthogonal-flows.html (live test) (source)
- table-with-percent-intrinsic-width.html (live test) (source)
- inline-flex
-
This value causes an element to generate a flex container box
that is inline-level when placed in flow layout.
Tests
- flexbox_inline.html (live test) (source)
- flex-inline.html (live test) (source)
- flexbox_inline-float.html (live test) (source)
- inline-flexbox-absurd-block-size-crash.html (live test) (source)
- inline-flexbox-wrap-vertically-width-calculation.html (live test) (source)
- inline-flex-editing-crash.html (live test) (source)
- inline-flex-editing-with-updating-text-crash.html (live test) (source)
- inline-flex-frameset-main-axis-crash.html (live test) (source)
- inline-flex.html (live test) (source)
- inline-flex-min-content-height.html (live test) (source)
- flexbox_computedstyle_display-inline.html (live test) (source)
- intrinsic-width-orthogonal-writing-mode.html (live test) (source)
A flex container establishes a new flex formatting context for its contents. This is the same as establishing a block formatting context, except that flex layout is used instead of block layout. For example, floats do not intrude into the flex container, and the flex container’s margins do not collapse with the margins of its contents. Flex containers form a containing block for their contents exactly like block containers do. [CSS2] The overflow property applies to flex containers.
Tests
- flexbox-overflow-horiz-001.html (live test) (source)
- flexbox-overflow-horiz-002.html (live test) (source)
- flexbox-overflow-horiz-003.html (live test) (source)
- flexbox-overflow-horiz-004.html (live test) (source)
- flexbox-overflow-horiz-005.html (live test) (source)
- flexbox-overflow-padding-001.html (live test) (source)
- flexbox-overflow-padding-002.html (live test) (source)
- flexbox-overflow-vert-001.html (live test) (source)
- flexbox-overflow-vert-002.html (live test) (source)
- flexbox-overflow-vert-003.html (live test) (source)
- flexbox-overflow-vert-004.html (live test) (source)
- flexbox-overflow-vert-005.html (live test) (source)
- flexbox_rowspan-overflow-automatic.html (live test) (source)
- flexbox_rowspan-overflow.html (live test) (source)
- flexbox-safe-overflow-position-001.html (live test) (source)
- flexbox-safe-overflow-position-002.html (live test) (source)
- flexbox-safe-overflow-position-003.html (live test) (source)
- flexbox-safe-overflow-position-004.html (live test) (source)
- flexbox-safe-overflow-position-005.html (live test) (source)
- flexbox-safe-overflow-position-006.html (live test) (source)
- flexbox_width-overflow.html (live test) (source)
- min-size-auto-overflow-clip.html (live test) (source)
- negative-overflow-002.html (live test) (source)
- negative-overflow-003.html (live test) (source)
- negative-overflow-004-no-padding.html (live test) (source)
- negative-overflow.html (live test) (source)
- overflow-area-001.html (live test) (source)
- overflow-area-002.html (live test) (source)
- overflow-area-003.html (live test) (source)
- overflow-auto-001.html (live test) (source)
- overflow-auto-002.html (live test) (source)
- overflow-auto-003.html (live test) (source)
- overflow-auto-004.html (live test) (source)
- overflow-auto-005.html (live test) (source)
- overflow-auto-006.html (live test) (source)
- overflow-auto-007.html (live test) (source)
- overflow-auto-008.html (live test) (source)
- overflow-top-left.html (live test) (source)
- padding-overflow.html (live test) (source)
- select-as-flex-item-child-with-overflow.html (live test) (source)
- select-as-flex-item-with-overflow.html (live test) (source)
- text-overflow-on-flexbox-001.html (live test) (source)
- synthesize-vrl-baseline.html (live test) (source)
Flex containers are not block containers, and so some properties that were designed with the assumption of block layout don’t apply in the context of flex layout. In particular:
-
float and clear do not create floating or clearance of flex item, and do not take it out-of-flow.
-
vertical-align has no effect on a flex item.
-
the ::first-line and ::first-letter pseudo-elements do not apply to flex containers, and flex containers do not contribute a first formatted line or first letter to their ancestors.
Tests
- align-content-wrap-004.html (live test) (source)
- align-items-baseline-column-vert-lr-table-item.html (live test) (source)
- align-items-baseline-vert-lr-column-horz-table-item.html (live test) (source)
- align-items-baseline-vert-rl-column-horz-table-item.html (live test) (source)
- flexbox_box-clear.html (live test) (source)
- flexbox_first-letter.html (live test) (source)
- flexbox_first-line.html (live test) (source)
- flexbox-ignores-first-letter.html (live test) (source)
- flexbox_item-vertical-align.html (live test) (source)
- flexbox-with-pseudo-elements-001.html (live test) (source)
- flexbox-with-pseudo-elements-002.html (live test) (source)
- flexbox-with-pseudo-elements-003.html (live test) (source)
- flexible-box-float.html (live test) (source)
- flex-item-vertical-align.html (live test) (source)
- flex-vertical-align-effect.html (live test) (source)
- hittest-before-pseudo.html (live test) (source)
If an element’s specified display is inline-flex, then its display property computes to flex in certain circumstances: the table in CSS 2.1 Section 9.7 is amended to contain an additional row, with inline-flex in the "Specified Value" column and flex in the "Computed Value" column.
4. Flex Items
Loosely speaking, the flex items of a flex container are boxes representing its in-flow contents.
Each in-flow child of a flex container becomes a flex item, and each child text sequence is wrapped in an anonymous block container flex item. However, if the entire text sequences contains only document white space characters (i.e. characters that can be affected by the white-space property) it is instead not rendered (just as if its text nodes were display:none).
Tests
- anonymous-block.html (live test) (source)
- anonymous-flex-item-001.html (live test) (source)
- anonymous-flex-item-002.html (live test) (source)
- anonymous-flex-item-003.html (live test) (source)
- anonymous-flex-item-004.html (live test) (source)
- anonymous-flex-item-005.html (live test) (source)
- anonymous-flex-item-006.html (live test) (source)
- anonymous-flex-item-document-white-space-crash.html (live test) (source)
- anonymous-flex-item-restyle.html (live test) (source)
- canvas-dynamic-change-001.html (live test) (source)
- column-flex-child-with-max-width.html (live test) (source)
- flexbox-whitespace-handling-001a.xhtml (live test) (source)
- flexbox-whitespace-handling-001b.xhtml (live test) (source)
- flexbox-whitespace-handling-002.xhtml (live test) (source)
- hittest-anonymous-box.html (live test) (source)
- percentage-descendant-of-anonymous-flex-item.html (live test) (source)
- percentage-size-subitems-001.html (live test) (source)
- whitespace-in-flexitem-001.html (live test) (source)
Examples of flex items:
< div style = "display:flex" > <!-- flex item: block child --> < div id = "item1" > block</ div > <!-- flex item: floated element; floating is ignored --> < div id = "item2" style = "float: left;" > float</ div > <!-- flex item: anonymous block box around inline content --> anonymous item 3<!-- flex item: inline child --> < span > item 4<!-- flex items do not split around blocks --> < q style = "display: block" id = not-an-item > item 4</ q > item 4</ span > </ div >
Note that the inter-element white space disappears: it does not become its own flex item, even though the inter-element text does get wrapped in an anonymous flex item.
Note also that the anonymous item’s box is unstyleable, since there is no element to assign style rules to. Its contents will however inherit styles (such as font settings) from the flex container.
A flex item establishes an independent formatting context for its contents. However, flex items themselves are flex-level boxes, not block-level boxes: they participate in their container’s flex formatting context, not in a block formatting context.
Note: Authors reading this spec may want to skip past the following box-generation and static position details.
If the computed display value of an element’s nearest ancestor element (skipping display:contents ancestors) is flex or inline-flex, the element’s own display value is blockified. (See CSS2.1§9.7 [CSS2] and CSS Display 3 § 2.7 Automatic Box Type Transformations for details on this type of display value conversion.)
Note: Blockification still occurs even when the flex or inline-flex element does not end up generating a flex container box, e.g. when it is replaced or in a display: none subtree.
Note: Some values of display normally trigger the creation of anonymous boxes around the original box. If such a box is a flex item, it is blockified first, and so anonymous box creation will not happen. For example, two contiguous flex items with display: table-cell will become two separate display: block flex items, instead of being wrapped into a single anonymous table.
In the case of flex items with display: table, the table wrapper box becomes the flex item, so the align-self property applies to it. The contents of any caption boxes contribute to the calculation of the table wrapper box’s min-content and max-content sizes. However, like width and height, the flex longhands apply to the table box as follows: the flex item’s final size is calculated by performing layout as if the distance between the table wrapper box’s edges and the table box’s content edges were all part of the table box’s border+padding area, and the table box were the flex item.
4.1. Absolutely-Positioned Flex Children
As it is out-of-flow, an absolutely-positioned child of a flex container does not participate in flex layout.
The cross-axis edges of the static-position rectangle of an absolutely-positioned child of a flex container are the content edges of the flex container. The main-axis edges of the static-position rectangle are where the margin edges of the child would be positioned if it were the sole flex item in the flex container, assuming both the child and the flex container were fixed-size boxes of their used size. (For this purpose, the child’s auto margins are treated as zero.)
Tests
- abspos-autopos-htb-ltr.html (live test) (source)
- abspos-autopos-htb-rtl.html (live test) (source)
- abspos-autopos-vlr-ltr.html (live test) (source)
- abspos-autopos-vlr-rtl.html (live test) (source)
- abspos-autopos-vrl-ltr.html (live test) (source)
- abspos-autopos-vrl-rtl.html (live test) (source)
- dynamic-align-self-001.html (live test) (source)
- flex-abspos-staticpos-align-self-safe-002.html (live test) (source)
- flex-abspos-staticpos-align-self-safe-003.html (live test) (source)
- abspos-descendent-001.html (live test) (source)
- flex-abspos-staticpos-align-content-001.html (live test) (source)
- flex-abspos-staticpos-align-self-001.html (live test) (source)
- flex-abspos-staticpos-align-self-002.html (live test) (source)
- flex-abspos-staticpos-align-self-003.html (live test) (source)
- flex-abspos-staticpos-align-self-004.html (live test) (source)
- flex-abspos-staticpos-align-self-005.html (live test) (source)
- flex-abspos-staticpos-align-self-006.html (live test) (source)
- flex-abspos-staticpos-align-self-007.html (live test) (source)
- flex-abspos-staticpos-align-self-008.html (live test) (source)
- flex-abspos-staticpos-align-self-rtl-001.html (live test) (source)
- flex-abspos-staticpos-align-self-rtl-002.html (live test) (source)
- flex-abspos-staticpos-align-self-rtl-003.html (live test) (source)
- flex-abspos-staticpos-align-self-rtl-004.html (live test) (source)
- flex-abspos-staticpos-align-self-safe-001.html (live test) (source)
- flex-abspos-staticpos-align-self-vertWM-001.html (live test) (source)
- flex-abspos-staticpos-align-self-vertWM-002.html (live test) (source)
- flex-abspos-staticpos-align-self-vertWM-003.html (live test) (source)
- flex-abspos-staticpos-align-self-vertWM-004.html (live test) (source)
- flex-abspos-staticpos-fallback-justify-content-001.html (live test) (source)
- flex-abspos-staticpos-justify-content-001.html (live test) (source)
- flex-abspos-staticpos-justify-content-002.html (live test) (source)
- flex-abspos-staticpos-justify-content-003.html (live test) (source)
- flex-abspos-staticpos-justify-content-004.html (live test) (source)
- flex-abspos-staticpos-justify-content-005.html (live test) (source)
- flex-abspos-staticpos-justify-content-006.html (live test) (source)
- flex-abspos-staticpos-justify-content-007.html (live test) (source)
- flex-abspos-staticpos-justify-content-008.html (live test) (source)
- flex-abspos-staticpos-justify-content-rtl-001.html (live test) (source)
- flex-abspos-staticpos-justify-content-rtl-002.html (live test) (source)
- flex-abspos-staticpos-justify-content-vertWM-001.html (live test) (source)
- flex-abspos-staticpos-justify-content-vertWM-002.html (live test) (source)
- flex-abspos-staticpos-justify-self-001.html (live test) (source)
- flex-abspos-staticpos-margin-001.html (live test) (source)
- flex-abspos-staticpos-margin-002.html (live test) (source)
- flex-abspos-staticpos-margin-003.html (live test) (source)
- flexbox_absolute-atomic.html (live test) (source)
- flexbox-abspos-child-001a.html (live test) (source)
- flexbox-abspos-child-001b.html (live test) (source)
- flexbox-abspos-child-002.html (live test) (source)
- flexbox_inline-abspos.html (live test) (source)
- position-absolute-001.html (live test) (source)
- position-absolute-002.html (live test) (source)
- position-absolute-003.html (live test) (source)
- position-absolute-004.html (live test) (source)
- position-absolute-005.html (live test) (source)
- position-absolute-006.html (live test) (source)
- position-absolute-007.html (live test) (source)
- position-absolute-008.html (live test) (source)
- position-absolute-009.html (live test) (source)
- position-absolute-010.html (live test) (source)
- position-absolute-011.html (live test) (source)
- position-absolute-012.html (live test) (source)
- position-absolute-013.html (live test) (source)
- position-absolute-014.html (live test) (source)
- position-absolute-015.html (live test) (source)
- position-absolute-containing-block-001.html (live test) (source)
- position-absolute-containing-block-002.html (live test) (source)
- flex-content-alignment-with-abspos-001.html (live test) (source)
- dynamic-grid-flex-abspos.html (live test) (source)
- flexbox_stf-abspos.html (live test) (source)
- flex-item-position-relative-001.html (live test) (source)
4.2. Flex Item Margins and Paddings
The margins of adjacent flex items do not collapse.
Percentage margins and paddings on flex items, like those on block boxes, are resolved against the inline size of their containing block, e.g. left/right/top/bottom percentages all resolve against their containing block’s width in horizontal writing modes.
Auto margins expand to absorb extra space in the corresponding dimension. They can be used for alignment, or to push adjacent flex items apart. See Aligning with auto margins.
Tests
4.3. Flex Item Z-Ordering
Flex items paint exactly the same as inline blocks [CSS2], except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static (behaving exactly as if position were relative).
Note: Descendants that are positioned outside a flex item still participate in any stacking context established by the flex item.
Tests
- flexbox-paint-ordering-001.xhtml (live test) (source)
- flexbox-paint-ordering-002.xhtml (live test) (source)
- flexbox-paint-ordering-003.html (live test) (source)
- flex-item-z-ordering-001.html (live test) (source)
- flex-item-z-ordering-002.html (live test) (source)
- flexbox-items-as-stacking-contexts-001.xhtml (live test) (source)
- flexbox-items-as-stacking-contexts-002.html (live test) (source)
- flexbox-items-as-stacking-contexts-003.html (live test) (source)
- hittest-overlapping-margin.html (live test) (source)
- hittest-overlapping-order.html (live test) (source)
- hittest-overlapping-relative.html (live test) (source)
4.4. Collapsed Items
Specifying visibility:collapse on a flex item causes it to become a collapsed flex item, producing an effect similar to visibility:collapse on a table-row or table-column: the collapsed flex item is removed from rendering entirely, but leaves behind a "strut" that keeps the flex line’s cross-size stable. Thus, if a flex container has only one flex line, dynamically collapsing or uncollapsing items may change the flex container’s main size, but is guaranteed to have no effect on its cross size and won’t cause the rest of the page’s layout to "wobble". Flex line wrapping is re-done after collapsing, however, so the cross size of a flex container with multiple lines might or might not change.
Though collapsed flex items aren’t rendered, they do appear in the formatting structure. Therefore, unlike on display:none items [CSS2], effects that depend on a box appearing in the formatting structure (like incrementing counters or running animations and transitions) still operate on collapsed items.
Tests
@media ( min-width:60 em ) { /* two column layout only when enough room (relative to default text size) */ div{ display : flex; } #main{ flex : 1 ; /* Main takes up all remaining space */ order:1 ; /* Place it after (to the right of) the navigation */ min-width:12 em ; /* Optimize main content area sizing */ } } /* menu items use flex layout so that visibility:collapse will work */ nav > ul > li{ display : flex; flex-flow : column; } /* dynamically collapse submenus when not targeted */ nav > ul > li:not( :target) :not( :hover) > ul{ visibility : collapse; }
< div > < article id = "main" > Interesting Stuff to Read</ article > < nav > < ul > < li id = "nav-about" >< a href = "#nav-about" > About</ a > …< li id = "nav-projects" >< a href = "#nav-projects" > Projects</ a > < ul > < li >< a href = "…" > Art</ a > < li >< a href = "…" > Architecture</ a > < li >< a href = "…" > Music</ a > </ ul > < li id = "nav-interact" >< a href = "#nav-interact" > Interact</ a > …</ ul > </ nav > </ div > < footer > …
To compute the size of the strut, flex layout is first performed with all items uncollapsed, and then re-run with each collapsed flex item replaced by a strut that maintains the original cross size of the item’s original line. See the Flex Layout Algorithm for the normative definition of how visibility:collapse interacts with flex layout.
Note: Using visibility:collapse on any flex items will cause the flex layout algorithm to repeat partway through, re-running the most expensive steps. It’s recommended that authors continue to use display:none to hide items if the items will not be dynamically collapsed and uncollapsed, as that is more efficient for the layout engine. (Since only part of the steps need to be repeated when visibility is changed, however, 'visibility: collapse' is still recommended for dynamic cases.)
4.5. Automatic Minimum Size of Flex Items
Note: The auto keyword, representing an automatic minimum size, is the new initial value of the min-width and min-height properties. The keyword was previously defined in this specification, but is now defined in the CSS Sizing module.
To provide a more reasonable default minimum size for flex items, the used value of a main axis automatic minimum size on a flex item whose computed overflow value is non-scrollable is its content-based minimum size; for main-axis scroll containers the automatic minimum size is zero, as usual.
The content-based minimum size of a flex item differs depending on whether the flex item is replaced or not:
- For replaced elements
-
Use the smaller of the content size suggestion and the transferred size suggestion (if one exists), capped by the specified size suggestion (if one exists).
- For non-replaced elements
-
Use the larger of the content size suggestion and the transferred size suggestion (if one exists), capped by the specified size suggestion (if one exists).
In either case, the size is clamped by the maximum main size if it’s definite.
The content size suggestion, specified size suggestion, and transferred size suggestion used in this calculation account for the relevant min/max/preferred size properties so that the content-based minimum size does not interfere with any author-provided constraints, and are defined below:
- specified size suggestion
- If the item’s preferred main size is definite and not automatic, then the specified size suggestion is that size. It is otherwise undefined.
- transferred size suggestion
- If the item has a preferred aspect ratio and its preferred cross size is definite, then the transferred size suggestion is that size (clamped by its minimum and maximum cross sizes if they are definite), converted through the aspect ratio. It is otherwise undefined.
- content size suggestion
- The content size suggestion is the min-content size in the main axis, clamped, if it has a preferred aspect ratio, by any definite minimum and maximum cross sizes converted through the aspect ratio.
Tests
- content-height-with-scrollbars.html (live test) (source)
- fieldset-as-item-dynamic.html (live test) (source)
- fieldset-as-item-overflow.html (live test) (source)
- flex-aspect-ratio-img-column-001.html (live test) (source)
- flex-aspect-ratio-img-column-002.html (live test) (source)
- flex-aspect-ratio-img-column-003.html (live test) (source)
- flex-aspect-ratio-img-column-004.html (live test) (source)
- flex-aspect-ratio-img-column-005.html (live test) (source)
- flex-aspect-ratio-img-column-006.html (live test) (source)
- flex-aspect-ratio-img-column-007.html (live test) (source)
- flex-aspect-ratio-img-column-008.html (live test) (source)
- flex-aspect-ratio-img-column-009.html (live test) (source)
- flex-aspect-ratio-img-column-010.html (live test) (source)
- flex-aspect-ratio-img-column-011.html (live test) (source)
- flex-aspect-ratio-img-column-012.html (live test) (source)
- flex-aspect-ratio-img-column-013.html (live test) (source)
- flex-aspect-ratio-img-column-014.html (live test) (source)
- flex-aspect-ratio-img-column-015.html (live test) (source)
- flex-aspect-ratio-img-column-016.html (live test) (source)
- flex-aspect-ratio-img-column-017.html (live test) (source)
- flex-aspect-ratio-img-column-018.html (live test) (source)
- flex-aspect-ratio-img-row-001.html (live test) (source)
- flex-aspect-ratio-img-row-002.html (live test) (source)
- flex-aspect-ratio-img-row-003.html (live test) (source)
- flex-aspect-ratio-img-row-004.html (live test) (source)
- flex-aspect-ratio-img-row-005.html (live test) (source)
- flex-aspect-ratio-img-row-006.html (live test) (source)
- flex-aspect-ratio-img-row-007.html (live test) (source)
- flex-aspect-ratio-img-row-008.html (live test) (source)
- flex-aspect-ratio-img-row-009.html (live test) (source)
- flex-aspect-ratio-img-row-010.html (live test) (source)
- flex-aspect-ratio-img-row-011.html (live test) (source)
- flex-aspect-ratio-img-row-012.html (live test) (source)
- flex-aspect-ratio-img-row-013.html (live test) (source)
- flex-aspect-ratio-img-row-014.html (live test) (source)
- flex-aspect-ratio-img-row-015.html (live test) (source)
- flex-aspect-ratio-img-row-016.html (live test) (source)
- flex-aspect-ratio-img-row-017.html (live test) (source)
- flex-aspect-ratio-img-vert-lr.html (live test) (source)
- flexbox-definite-cross-size-constrained-percentage.html (live test) (source)
- flexbox-min-height-auto-001.html (live test) (source)
- flexbox-min-height-auto-002a.html (live test) (source)
- flexbox-min-height-auto-002b.html (live test) (source)
- flexbox-min-height-auto-002c.html (live test) (source)
- flexbox-min-height-auto-003.html (live test) (source)
- flexbox-min-height-auto-004.html (live test) (source)
- flexbox-min-width-auto-001.html (live test) (source)
- flexbox-min-width-auto-002a.html (live test) (source)
- flexbox-min-width-auto-002b.html (live test) (source)
- flexbox-min-width-auto-002c.html (live test) (source)
- flexbox-min-width-auto-003.html (live test) (source)
- flexbox-min-width-auto-004.html (live test) (source)
- flexbox-min-width-auto-005.html (live test) (source)
- flexbox-min-width-auto-006.html (live test) (source)
- flex-item-compressible-001.html (live test) (source)
- flex-item-compressible-002.html (live test) (source)
- flexitem-stretch-image.html (live test) (source)
- flexitem-stretch-range.html (live test) (source)
- flex-minimum-height-flex-items-001.xht (live test) (source)
- flex-minimum-height-flex-items-002.xht (live test) (source)
- flex-minimum-height-flex-items-003.xht (live test) (source)
- flex-minimum-height-flex-items-004.xht (live test) (source)
- flex-minimum-height-flex-items-005.xht (live test) (source)
- flex-minimum-height-flex-items-006.xht (live test) (source)
- flex-minimum-height-flex-items-007.xht (live test) (source)
- flex-minimum-height-flex-items-008.xht (live test) (source)
- flex-minimum-height-flex-items-009.html (live test) (source)
- flex-minimum-height-flex-items-010.html (live test) (source)
- flex-minimum-height-flex-items-011.xht (live test) (source)
- flex-minimum-height-flex-items-012.html (live test) (source)
- flex-minimum-height-flex-items-013.html (live test) (source)
- flex-minimum-height-flex-items-014.html (live test) (source)
- flex-minimum-height-flex-items-015.html (live test) (source)
- flex-minimum-height-flex-items-016.html (live test) (source)
- flex-minimum-height-flex-items-017.html (live test) (source)
- flex-minimum-height-flex-items-018.html (live test) (source)
- flex-minimum-height-flex-items-019.html (live test) (source)
- flex-minimum-height-flex-items-020.html (live test) (source)
- flex-minimum-height-flex-items-021.html (live test) (source)
- flex-minimum-height-flex-items-022.html (live test) (source)
- flex-minimum-height-flex-items-023.html (live test) (source)
- flex-minimum-height-flex-items-024.html (live test) (source)
- flex-minimum-height-flex-items-025.html (live test) (source)
- flex-minimum-height-flex-items-026.html (live test) (source)
- flex-minimum-height-flex-items-027.html (live test) (source)
- flex-minimum-height-flex-items-028.html (live test) (source)
- flex-minimum-height-flex-items-029.html (live test) (source)
- flex-minimum-height-flex-items-030.html (live test) (source)
- flex-minimum-height-flex-items-031.html (live test) (source)
- flex-minimum-size-001.html (live test) (source)
- flex-minimum-size-002.html (live test) (source)
- flex-minimum-size-003.html (live test) (source)
- flex-minimum-width-flex-items-001.xht (live test) (source)
- flex-minimum-width-flex-items-002.xht (live test) (source)
- flex-minimum-width-flex-items-003.xht (live test) (source)
- flex-minimum-width-flex-items-004.xht (live test) (source)
- flex-minimum-width-flex-items-005.xht (live test) (source)
- flex-minimum-width-flex-items-006.xht (live test) (source)
- flex-minimum-width-flex-items-007.xht (live test) (source)
- flex-minimum-width-flex-items-008.xht (live test) (source)
- flex-minimum-width-flex-items-009.html (live test) (source)
- flex-minimum-width-flex-items-010.html (live test) (source)
- flex-minimum-width-flex-items-011.html (live test) (source)
- flex-minimum-width-flex-items-012.html (live test) (source)
- flex-minimum-width-flex-items-013.html (live test) (source)
- flex-minimum-width-flex-items-014.html (live test) (source)
- flex-minimum-width-flex-items-015.html (live test) (source)
- flex-minimum-width-flex-items-016.html (live test) (source)
- checkbox-percentage-width-in-flex.html (live test) (source)
- flexbox_computedstyle_min-auto-size.html (live test) (source)
- flexbox_computedstyle_min-height-auto.html (live test) (source)
- flexbox_computedstyle_min-width-auto.html (live test) (source)
- select-element-zero-height-001.html (live test) (source)
- select-element-zero-height-002.html (live test) (source)
Note: The content-based minimum size is a type of intrinsic size contribution, and thus the cyclic percentage provisions in CSS Sizing 3 § 5.2 Intrinsic Contributions apply.
For the purpose of calculating an intrinsic size of the box (e.g. the box’s min-content size), a content-based minimum size causes the box’s size in that axis to become indefinite (even if e.g. its width property specifies a definite size). Note this means that percentages calculated against this size will behave as auto.
For any purpose other than calculating intrinsic sizes, a content-based minimum size (unlike an explicit min-content/etc minimum size) does not force the box’s size to become indefinite. However, if a percentage resolved against the box’s size before this minimum was applied, it must be re-resolved against the new size after it is applied.
In particular, if flex sizing is being used for a major content area of a document, it is better to set an explicit font-relative minimum width such as min-width: 12em. A content-based minimum width could result in a large table or large image stretching the size of the entire content area into an overflow zone, and thereby making lines of text gratuitously long and hard to read.
Note also, when content-based sizing is used on an item with large amounts of content, the layout engine must traverse all of this content before finding its minimum size, whereas if the author sets an explicit minimum, this is not necessary. (For items with small amounts of content, however, this traversal is trivial and therefore not a performance concern.)
Tests
- aspect-ratio-intrinsic-size-001.html (live test) (source)
- aspect-ratio-intrinsic-size-002.html (live test) (source)
- aspect-ratio-intrinsic-size-003.html (live test) (source)
- aspect-ratio-intrinsic-size-004.html (live test) (source)
- aspect-ratio-intrinsic-size-005.html (live test) (source)
- aspect-ratio-intrinsic-size-006.html (live test) (source)
- aspect-ratio-intrinsic-size-007.html (live test) (source)
- aspect-ratio-intrinsic-size-008.html (live test) (source)
- aspect-ratio-intrinsic-size-009.html (live test) (source)
- aspect-ratio-intrinsic-size-010.html (live test) (source)
- aspect-ratio-transferred-max-size.html (live test) (source)
- flex-item-content-is-min-width-max-content.html (live test) (source)
5. Ordering and Orientation
The contents of a flex container can be laid out in any direction and in any order. This allows an author to trivially achieve effects that would previously have required complex or fragile methods, such as hacks using the float and clear properties. This functionality is exposed through the flex-direction, flex-wrap, and order properties.
Note: The reordering capabilities of flex layout intentionally affect only the visual rendering, leaving speech order and navigation based on the source order. This allows authors to manipulate the visual presentation while leaving the source order intact for non-CSS UAs and for linear models such as speech and sequential navigation. See CSS Display 3 § 3.1 Reordering and Accessibility and the Flex Layout Overview for examples that use this dichotomy to improve accessibility.
Authors must not use order or the *-reverse values of flex-flow/flex-direction as a substitute for correct source ordering, as that can ruin the accessibility of the document.
5.1. Flex Flow Direction: the flex-direction property
| Name: | flex-direction |
|---|---|
| Value: | row | row-reverse | column | column-reverse |
| Initial: | row |
| Applies to: | flex containers |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
The flex-direction property specifies how flex items are placed in the flex container, by setting the direction of the flex container’s main axis. This determines the direction in which flex items are laid out.
- row
-
The flex container’s main axis has the same orientation as the
inline axis
of the current writing mode.
The main-start and main-end directions are equivalent to the
inline-start and
inline-end directions, respectively,
of the current writing mode.
Tests
- flex-child-percent-basis-resize-1.html (live test) (source)
- flexbox-flex-basis-content-001a.html (live test) (source)
- flexbox-flex-basis-content-001b.html (live test) (source)
- flexbox-flex-direction-row.htm (live test) (source)
- flexbox_justifycontent-left-001.html (live test) (source)
- flexbox-writing-mode-010.html (live test) (source)
- flexbox-writing-mode-011.html (live test) (source)
- flexbox-writing-mode-012.html (live test) (source)
- flexbox-writing-mode-016.html (live test) (source)
- percentage-size.html (live test) (source)
- row-reverse
-
Same as row,
except the main-start and main-end directions are swapped.
Tests
- flexbox_direction-row-reverse.html (live test) (source)
- flexbox-mbp-horiz-001-reverse.xhtml (live test) (source)
- flexbox-mbp-horiz-001-rtl-reverse.xhtml (live test) (source)
- multi-line-wrap-with-row-reverse.html (live test) (source)
- flexbox_justifycontent-right-001.html (live test) (source)
- flexbox_justifycontent-start.html (live test) (source)
- flexbox_justifycontent-start-rtl.html (live test) (source)
- column
-
The flex container’s main axis has the same orientation as the
block axis
of the current writing mode.
The main-start and main-end directions are equivalent to the
block-start and
block-end directions, respectively,
of the current writing mode.
Tests
- columns-height-set-via-top-bottom.html (live test) (source)
- dynamic-bsize-change.html (live test) (source)
- flexbox_direction-column.html (live test) (source)
- flexbox_direction-column-reverse.html (live test) (source)
- flexbox_rtl-flow.html (live test) (source)
- flex-column-relayout-assert.html (live test) (source)
- flex-item-max-height-min-content.html (live test) (source)
- flex-item-transferred-sizes-padding-border-sizing.html (live test) (source)
- flex-item-transferred-sizes-padding-content-sizing.html (live test) (source)
- flex-outer-flexbox-column-recalculate-height-on-resize-001.html (live test) (source)
- image-nested-within-definite-column-flexbox.html (live test) (source)
- layout-with-inline-svg-001.html (live test) (source)
- nested-orthogonal-flexbox-relayout.html (live test) (source)
- percentage-max-width-cross-axis.html (live test) (source)
- percentage-size.html (live test) (source)
- column-reverse
- Same as column, except the main-start and main-end directions are swapped.
Note: The reverse values do not reverse box ordering: like writing-mode and direction [CSS3-WRITING-MODES], they only change the direction of flow. Painting order, speech order, and sequential navigation orders are not affected.
Note: Depending on the value of justify-content, the reverse values of flex-direction can alter the initial scroll position on flex containers that are also scroll containers. See CSS Box Alignment 3 § 5.3 Alignment Overflow and Scroll Containers.
Tests
- discrete-no-interpolation.html (live test) (source)
- change-column-flex-width.html (live test) (source)
- column-flex-child-with-max-width.html (live test) (source)
- column-flex-child-with-overflow-scroll.html (live test) (source)
- cross-axis-scrollbar.html (live test) (source)
- dynamic-orthogonal-flex-item.html (live test) (source)
- flexbox-writing-mode-001.html (live test) (source)
- flexbox-writing-mode-002.html (live test) (source)
- flexbox-writing-mode-003.html (live test) (source)
- flexbox-writing-mode-004.html (live test) (source)
- flexbox-writing-mode-005.html (live test) (source)
- flexbox-writing-mode-006.html (live test) (source)
- flexbox-writing-mode-007.html (live test) (source)
- flexbox-writing-mode-008.html (live test) (source)
- flexbox-writing-mode-009.html (live test) (source)
- flexbox-writing-mode-010.html (live test) (source)
- flexbox-writing-mode-011.html (live test) (source)
- flexbox-writing-mode-012.html (live test) (source)
- flexbox-writing-mode-013.html (live test) (source)
- flexbox-writing-mode-014.html (live test) (source)
- flexbox-writing-mode-015.html (live test) (source)
- flexbox-writing-mode-016.html (live test) (source)
- flexbox-writing-mode-slr.html (live test) (source)
- flexbox-writing-mode-slr-row-mix.html (live test) (source)
- flexbox-writing-mode-slr-rtl.html (live test) (source)
- flexbox-writing-mode-srl.html (live test) (source)
- flexbox-writing-mode-srl-row-mix.html (live test) (source)
- flexbox-writing-mode-srl-rtl.html (live test) (source)
- flexbox_object.html (live test) (source)
- flexbox_writing_mode_vertical_lays_out_contents_from_top_to_bottom.html (live test) (source)
- flexbox_computedstyle_flex-direction-column.html (live test) (source)
- flexbox_computedstyle_flex-direction-column-reverse.html (live test) (source)
- flexbox_computedstyle_flex-direction-invalid.html (live test) (source)
- flexbox_computedstyle_flex-direction-row.html (live test) (source)
- flexbox_computedstyle_flex-direction-row-reverse.html (live test) (source)
5.2. Flex Line Wrapping: the flex-wrap property
| Name: | flex-wrap |
|---|---|
| Value: | nowrap | wrap | wrap-reverse |
| Initial: | nowrap |
| Applies to: | flex containers |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
The flex-wrap property controls whether the flex container is single-line or multi-line, and the direction of the cross-axis, which determines the direction new lines are stacked in.
- nowrap
- The flex container is single-line.
- wrap
- The flex container is multi-line.
- wrap-reverse
- Same as wrap.
For the values that are not wrap-reverse, the cross-start direction is equivalent to either the inline-start or block-start direction of the current writing mode (whichever is in the cross axis) and the cross-end direction is the opposite direction of cross-start. When flex-wrap is wrap-reverse, the cross-start and cross-end directions are swapped.
Note: Depending on the value of align-content, the wrap-reverse value of flex-wrap can alter the initial scroll position on flex containers that are also scroll containers. See CSS Box Alignment 3 § 5.3 Alignment Overflow and Scroll Containers.
Tests
- discrete-no-interpolation.html (live test) (source)
- flexbox-flex-wrap-default.htm (live test) (source)
- flexbox-flex-wrap-flexing-002.html (live test) (source)
- flexbox-flex-wrap-flexing-003.html (live test) (source)
- flexbox-flex-wrap-flexing.html (live test) (source)
- flexbox-flex-wrap-horiz-001.html (live test) (source)
- flexbox-flex-wrap-horiz-002.html (live test) (source)
- flexbox-flex-wrap-nowrap.htm (live test) (source)
- flexbox-flex-wrap-vert-001.html (live test) (source)
- flexbox-flex-wrap-vert-002.html (live test) (source)
- flexbox-flex-wrap-wrap.htm (live test) (source)
- flexbox-flex-wrap-wrap-reverse.htm (live test) (source)
- flexbox_rowspan.html (live test) (source)
- flexbox_rtl-flow.html (live test) (source)
- flexbox_rtl-flow-reverse.html (live test) (source)
- flexbox_rtl-order.html (live test) (source)
- flex-box-wrap.html (live test) (source)
- flexbox_wrap.html (live test) (source)
- flexbox_wrap-long.html (live test) (source)
- flexbox_wrap-reverse.html (live test) (source)
- multi-line-wrap-reverse-column-reverse.html (live test) (source)
- multi-line-wrap-reverse-row-reverse.html (live test) (source)
- flexbox_computedstyle_flex-wrap-invalid.html (live test) (source)
- flexbox_computedstyle_flex-wrap-nowrap.html (live test) (source)
- flexbox_computedstyle_flex-wrap-wrap.html (live test) (source)
- flexbox_computedstyle_flex-wrap-wrap-reverse.html (live test) (source)
- multiline-min-preferred-width.html (live test) (source)
- multiline-reverse-wrap-baseline.html (live test) (source)
5.3. Flex Direction and Wrap: the flex-flow shorthand
| Name: | flex-flow |
|---|---|
| Value: | <'flex-direction'> || <'flex-wrap'> |
| Initial: | see individual properties |
| Applies to: | see individual properties |
| Inherited: | see individual properties |
| Percentages: | see individual properties |
| Computed value: | see individual properties |
| Animation type: | see individual properties |
| Canonical order: | per grammar |
Tests
- box-sizing-001.html (live test) (source)
- button-column-wrap-crash.html (live test) (source)
- column-intrinsic-size-aspect-ratio-crash.html (live test) (source)
- css-flexbox-row.html (live test) (source)
- css-flexbox-row-reverse.html (live test) (source)
- css-flexbox-row-reverse-wrap.html (live test) (source)
- css-flexbox-row-reverse-wrap-reverse.html (live test) (source)
- css-flexbox-row-wrap.html (live test) (source)
- css-flexbox-row-wrap-reverse.html (live test) (source)
- css-flexbox-test1.html (live test) (source)
- flexbox-flex-direction-column.htm (live test) (source)
- flexbox-flex-direction-column-percentage-ignored.html (live test) (source)
- flexbox-flex-direction-column-reverse.htm (live test) (source)
- flexbox-flex-direction-default.htm (live test) (source)
- flexbox-flex-direction-row.htm (live test) (source)
- flexbox-flex-direction-row-reverse.htm (live test) (source)
- flex-direction-column-001-visual.html (visual test) (source)
- flex-direction-column.html (live test) (source)
- flex-direction-column-overlap-001.html (live test) (source)
- flex-direction-column-reverse-001-visual.html (visual test) (source)
- flex-direction-column-reverse-002-visual.html (visual test) (source)
- flex-direction-column-reverse.html (live test) (source)
- flex-direction.html (live test) (source)
- flex-direction-modify.html (live test) (source)
- flex-direction-row-001-visual.html (visual test) (source)
- flex-direction-row-002-visual.html (visual test) (source)
- flex-direction-row-reverse-001-visual.html (visual test) (source)
- flex-direction-row-reverse-002-visual.html (visual test) (source)
- flex-direction-row-reverse.html (live test) (source)
- flex-direction-row-vertical.html (live test) (source)
- flex-direction-with-element-insert.html (live test) (source)
- flexbox-flex-flow-001.html (live test) (source)
- flexbox-flex-flow-002.html (live test) (source)
- flexbox_flow-column-reverse-wrap.html (live test) (source)
- flexbox_flow-column-reverse-wrap-reverse.html (live test) (source)
- flexbox_flow-column-wrap.html (live test) (source)
- flexbox_flow-column-wrap-reverse.html (live test) (source)
- flexbox_flow-row-wrap.html (live test) (source)
- flexbox_flow-row-wrap-reverse.html (live test) (source)
- flex-flow-001.html (live test) (source)
- flex-flow-002.html (live test) (source)
- flex-flow-003.html (live test) (source)
- flex-flow-004.html (live test) (source)
- flex-flow-005.html (live test) (source)
- flex-flow-006.html (live test) (source)
- flex-flow-007.html (live test) (source)
- flex-flow-008.html (live test) (source)
- flex-flow-009.html (live test) (source)
- flex-flow-010.html (live test) (source)
- flex-flow-011.html (live test) (source)
- flex-flow-012.html (live test) (source)
- flex-flow-013.html (live test) (source)
- flex-wrap-002.html (live test) (source)
- flex-wrap-003.html (live test) (source)
- flex-wrap-004.html (live test) (source)
- flex-wrap-005.html (live test) (source)
- flex-wrap-006.html (live test) (source)
- flexbox_computedstyle_flex-flow-column.html (live test) (source)
- flexbox_computedstyle_flex-flow-column-nowrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-column-reverse.html (live test) (source)
- flexbox_computedstyle_flex-flow-column-reverse-nowrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-column-reverse-wrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-column-wrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-column-wrap-reverse.html (live test) (source)
- flexbox_computedstyle_flex-flow-nowrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-row.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-nowrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-reverse.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-reverse-nowrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-reverse-wrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-reverse-wrap-reverse.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-wrap.html (live test) (source)
- flexbox_computedstyle_flex-flow-row-wrap-reverse.html (live test) (source)
- flexbox_computedstyle_flex-flow-wrap.html (live test) (source)
- multiline-column-max-height.html (live test) (source)
- flex-direction-computed.html (live test) (source)
- flex-direction-invalid.html (live test) (source)
- flex-direction-valid.html (live test) (source)
- flex-flow-computed.html (live test) (source)
- flex-flow-invalid.html (live test) (source)
- flex-flow-shorthand.html (live test) (source)
- flex-flow-valid.html (live test) (source)
- flex-wrap-computed.html (live test) (source)
- flex-wrap-invalid.html (live test) (source)
- flex-wrap-valid.html (live test) (source)
The flex-flow property is a shorthand for setting the flex-direction and flex-wrap properties, which together define the flex container’s main and cross axes.
div | |
div | |
div |
| English | Japanese |
|---|---|
|
|
5.4. Reordering and Accessibility: the order property
Flex items are, by default, displayed and laid out
in the same order as they appear in the source document,
which represents their logical ordering.
This same order is used in rendering to non-visual media
(such as speech),
in the default traversal order of sequential navigation modes
(such as cycling through links,
see e.g. tabindex [HTML]),
and when content is represented in non-CSS UAs.
The order property can be used to change flex items’ ordering, laying them out in order-modified document order instead, in order to make their spatial arrangement on the 2D visual canvas differ from their logical order in linear presentations such as speech and sequential navigation. See CSS Display 3 § 3 Display Order: the order property. [CSS-DISPLAY-3]
Note: Since visual perception is two-dimensional and non-linear, the desired box order is not always the same logical order used by non-visual media and non-CSS UAs.
Authors must use order only for visual, not logical, reordering of content. Style sheets that use order to perform logical reordering are non-conforming.
<!DOCTYPE html> < header > ...</ header > < main > < article > ...</ article > < nav > ...</ nav > < aside > ...</ aside > </ main > < footer > ...</ footer >
This layout can be easily achieved with flex layout:
main{ display : flex; } main > article{ order : 2 ; min-width : 12 em ; flex : 1 ; } main > nav{ order : 1 ; width : 200 px ; } main > aside{ order : 3 ; width : 200 px ; }
As an added bonus, the columns will all be equal-height by default, and the main content will be as wide as necessary to fill the screen. Additionally, this can then be combined with media queries to switch to an all-vertical layout on narrow screens:
@media all and( max-width:600 px ) { /* Too narrow to support three columns */ main{ flex-flow : column; } main > article, main > nav, main > aside{ /* Return them to document order */ order:0 ; width : auto; } }
(Further use of multi-line flex containers to achieve even more intelligent wrapping left as an exercise for the reader.)
6. Flex Lines
Flex items in a flex container are laid out and aligned within flex lines, hypothetical containers used for grouping and alignment by the layout algorithm. A flex container can be either single-line or multi-line, depending on the flex-wrap property:
-
A single-line flex container (i.e. one with flex-wrap: nowrap) lays out all of its children in a single line, even if that would cause its contents to overflow.
-
A multi-line flex container (i.e. one with flex-wrap: wrap or flex-wrap: wrap-reverse) breaks its flex items across multiple lines, similar to how text is broken onto a new line when it gets too wide to fit on the existing line. When additional lines are created, they are stacked in the flex container along the cross axis according to the flex-wrap property. Every line contains at least one flex item, unless the flex container itself is completely empty.
#flex{ display : flex; flex-flow : row wrap; width : 300 px ; } .item{ width : 80 px ; }
< div id = "flex" > < div class = "item" > 1</ div > < div class = "item" > 2</ div > < div class = "item" > 3</ div > < div class = "item" > 4</ div > </ div >
Since the container is 300px wide, only three of the items fit onto a single line. They take up 240px, with 60px left over of remaining space. Because the flex-flow property specifies a multi-line flex container (due to the wrap keyword appearing in its value), the flex container will create an additional line to contain the last item.
Once content is broken into lines, each line is laid out independently; flexible lengths and the justify-content and align-self properties only consider the items on a single line at a time.
In a multi-line flex container (even one with only a single line), the cross size of each line is the minimum size necessary to contain the flex items on the line (after alignment due to align-self), and the lines are aligned within the flex container with the align-content property. In a single-line flex container, the cross size of the line is the cross size of the flex container, and align-content has no effect. The main size of a line is always the same as the main size of the flex container’s content box.
Tests
- align-content-001.htm (live test) (source)
- align-content-002.htm (live test) (source)
- align-content-003.htm (live test) (source)
- align-content-004.htm (live test) (source)
- align-content-005.htm (live test) (source)
- align-content-006.htm (live test) (source)
- align-content-007.htm (live test) (source)
- flex-wrap-column-grow.html (live test) (source)
- flexbox-lines-must-be-stretched-by-default.html (live test) (source)
7. Flexibility
The defining aspect of flex layout is the ability to make the flex items “flex”, altering their width/height to fill the available space in the main dimension. This is done with the flex property. A flex container distributes free space to its items (proportional to their flex grow factor) to fill the container, or shrinks them (proportional to their flex shrink factor) to prevent overflow.
A flex item is fully inflexible if both its flex-grow and flex-shrink values are zero, and flexible otherwise.
7.1. The flex Shorthand
| Name: | flex |
|---|---|
| Value: | none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] |
| Initial: | 0 1 auto |
| Applies to: | flex items |
| Inherited: | no |
| Percentages: | see individual properties |
| Computed value: | see individual properties |
| Animation type: | by computed value type |
| Canonical order: | per grammar |
Tests
- flexbox-dyn-resize-001.html (live test) (source)
- flex-item-max-width-min-content.html (live test) (source)
- flex-item-min-height-min-content.html (live test) (source)
- flex-item-min-width-min-content.html (live test) (source)
- flex-shorthand-calc.html (live test) (source)
- flex-computed.html (live test) (source)
- flex-invalid.html (live test) (source)
- flex-shorthand.html (live test) (source)
- flex-valid.html (live test) (source)
The flex property specifies the components of a flexible size: the flex factors (grow and shrink) and the flex basis. When a box is a flex item, flex is consulted instead of the main size property to determine the main size of the box. If a box is not a flex item, flex has no effect.
Note: The initial values of the flex longhands are equivalent to flex: 0 1 auto. This differs from their defaults when omitted in the flex shorthand (effectively 1 1 0px) so that the flex shorthand can better accommodate the most common cases.
- <'flex-grow'>
-
This <number [0,∞]> component sets flex-grow longhand
and specifies the flex grow factor,
which determines how much the flex item will grow
relative to the rest of the flex items in the flex container
when positive free space is distributed.
When omitted, it is set to 1.
Tests
- flex-grow-interpolation.html (live test) (source)
- flex-001.htm (live test) (source)
- flex-003.htm (live test) (source)
- flex-grow-001.xht (live test) (source)
- flex-grow-002.html (live test) (source)
- flex-grow-003.html (live test) (source)
- flex-grow-004.html (live test) (source)
- flex-grow-005.html (live test) (source)
- flex-grow-006.html (live test) (source)
- flex-grow-007.html (live test) (source)
- flex-grow-008.html (live test) (source)
- flexbox_flex-natural.html (live test) (source)
- flexbox_flex-natural-mixed-basis-auto.html (live test) (source)
- flexbox_flex-natural-mixed-basis.html (live test) (source)
- flexbox_flex-natural-variable-auto-basis.html (live test) (source)
- flexbox_flex-natural-variable-zero-basis.html (live test) (source)
- flexbox_flex-none.html (live test) (source)
- flexbox_flex-none-wrappable-content.html (live test) (source)
- flex-factor-less-than-one.html (live test) (source)
- flexbox_computedstyle_flex-shorthand-0-auto.html (live test) (source)
- flexbox_computedstyle_flex-shorthand-auto.html (live test) (source)
- flexbox_computedstyle_flex-shorthand.html (live test) (source)
- flexbox_computedstyle_flex-shorthand-initial.html (live test) (source)
- flexbox_computedstyle_flex-shorthand-invalid.html (live test) (source)
- flexbox_computedstyle_flex-shorthand-none.html (live test) (source)
- flexbox_computedstyle_flex-shorthand-number.html (live test) (source)
- flexbox_computedstyle_flex-grow-0.html (live test) (source)
- flexbox_computedstyle_flex-grow-invalid.html (live test) (source)
- flexbox_computedstyle_flex-grow-number.html (live test) (source)
- flexbox_interactive_flex-grow-transitions.html (manual test) (source)
- flex-grow-computed.html (live test) (source)
- flex-grow-invalid.html (live test) (source)
- flex-grow-valid.html (live test) (source)
- table-item-flex-percentage-min-width.html (live test) (source)
- table-item-flex-percentage-width.html (live test) (source)
- flex-rounding.html (live test) (source)
Flex values between 0 and 1 have a somewhat special behavior: when the sum of the flex values on the line is less than 1, they will take up less than 100% of the free space.
An item’s flex-grow value is effectively a request for some proportion of the free space, with 1 meaning “100% of the free space”; then if the items on the line are requesting more than 100% in total, the requests are rebalanced to keep the same ratio but use up exactly 100% of it. However, if the items request less than the full amount (such as three items that are each flex-grow: .25) then they’ll each get exactly what they request (25% of the free space to each, with the final 25% left unfilled). See § 9.7 Resolving Flexible Lengths for the exact details of how free space is distributed.
This pattern is required for continuous behavior as flex-grow approaches zero (which means the item wants none of the free space). Without this, a flex-grow: 1 item would take all of the free space; but so would a flex-grow: 0.1 item, and a flex-grow: 0.01 item, etc., until finally the value is small enough to underflow to zero and the item suddenly takes up none of the free space. With this behavior, the item instead gradually takes less of the free space as flex-grow shrinks below 1, smoothly transitioning to taking none of the free space at zero.
Unless this “partial fill” behavior is specifically what’s desired, authors should stick to values ≥ 1; for example, using 1 and 2 is usually better than using .33 and .67, as they’re more likely to behave as intended if items are added, removed, or line-wrapped.
- <'flex-shrink'>
-
This <number [0,∞]> component sets flex-shrink longhand
and specifies the flex shrink factor,
which determines how much the flex item will shrink
relative to the rest of the flex items in the flex container
when negative free space is distributed.
When omitted, it is set to 1.
Tests
- flex-shrink-interpolation.html (live test) (source)
- flex-002.htm (live test) (source)
- flex-004.htm (live test) (source)
- flex-factor-less-than-one.html (live test) (source)
- flex-shrink-001.html (live test) (source)
- flex-shrink-002.html (live test) (source)
- flex-shrink-003.html (live test) (source)
- flex-shrink-004.html (live test) (source)
- flex-shrink-005.html (live test) (source)
- flex-shrink-006.html (live test) (source)
- flex-shrink-007.html (live test) (source)
- flex-shrink-008.html (live test) (source)
- flex-shrink-large-value-crash.html (live test) (source)
- flexbox_computedstyle_flex-shrink-0.html (live test) (source)
- flexbox_computedstyle_flex-shrink-invalid.html (live test) (source)
- flexbox_computedstyle_flex-shrink-number.html (live test) (source)
- flexbox_interactive_flex-shrink-transitions.html (manual test) (source)
- flexbox_interactive_flex-shrink-transitions-invalid.html (manual test) (source)
- flex-shrink-computed.html (live test) (source)
- flex-shrink-invalid.html (live test) (source)
- flex-shrink-valid.html (live test) (source)
- table-item-flex-percentage-min-width.html (live test) (source)
- table-item-flex-percentage-width.html (live test) (source)
Note: The flex shrink factor is multiplied by the flex base size when distributing negative space. This distributes negative space in proportion to how much the item is able to shrink, so that e.g. a small item won’t shrink to zero before a larger item has been noticeably reduced.
- <'flex-basis'>
-
This component sets the flex-basis longhand,
which specifies the flex basis:
the initial main size of the flex item,
before free space is distributed according to the flex factors.
Tests
- flex-basis-composition.html (live test) (source)
- flex-basis-content-crash.html (live test) (source)
- flex-basis-interpolation.html (live test) (source)
- dynamic-isize-change-001.html (live test) (source)
- dynamic-isize-change-002.html (live test) (source)
- dynamic-isize-change-003.html (live test) (source)
- dynamic-isize-change-004.html (live test) (source)
- flex-basis-001.html (live test) (source)
- flex-basis-002.html (live test) (source)
- flex-basis-003.html (live test) (source)
- flex-basis-004.html (live test) (source)
- flex-basis-005.html (live test) (source)
- flex-basis-006.html (live test) (source)
- flex-basis-007.html (live test) (source)
- flex-basis-008.html (live test) (source)
- flex-basis-009.html (live test) (source)
- flex-basis-010.html (live test) (source)
- flex-basis-011.html (live test) (source)
- flex-basis-012.html (live test) (source)
- flex-basis-013.html (live test) (source)
- flex-basis-intrinsics-001.html (live test) (source)
- flex-basis-item-margins-001.html (live test) (source)
- flexbox-flex-basis-content-001a.html (live test) (source)
- flexbox-flex-basis-content-001b.html (live test) (source)
- flexbox-flex-basis-content-002a.html (live test) (source)
- flexbox-flex-basis-content-002b.html (live test) (source)
- flexbox-flex-basis-content-003a.html (live test) (source)
- flexbox-flex-basis-content-003b.html (live test) (source)
- flexbox-flex-basis-content-004a.html (live test) (source)
- flexbox-flex-basis-content-004b.html (live test) (source)
- flexbox_flex-basis.html (live test) (source)
- flexbox_flex-basis-shrink.html (live test) (source)
- flex-one-sets-flex-basis-to-zero-px.html (live test) (source)
- flex-shorthand-flex-basis-middle.html (live test) (source)
- flexbox_computedstyle_flex-basis-0.html (live test) (source)
- flexbox_computedstyle_flex-basis-0percent.html (live test) (source)
- flexbox_computedstyle_flex-basis-auto.html (live test) (source)
- flexbox_computedstyle_flex-basis-percent.html (live test) (source)
- flexbox_interactive_flex-basis-transitions.html (manual test) (source)
- flex-basis-computed.html (live test) (source)
- flex-basis-invalid.html (live test) (source)
- flex-basis-valid.html (live test) (source)
- table-as-item-percent-width-cell-001.html (live test) (source)
<'flex-basis'> accepts the same values as the width and height properties (except that auto is treated differently) plus the content keyword:
- auto
- When specified on a flex item, the auto keyword retrieves the value of the main size property as the used flex-basis. If that value is itself auto, then the used value is content.
- content
-
Indicates an automatic size
based on the flex item’s content.
(This is typically equivalent to the max-content size,
but with adjustments to handle preferred aspect ratios,
intrinsic sizing constraints,
and orthogonal flows;
see details in § 9 Flex Layout Algorithm.)
Note: This value was not present in the initial release of Flexible Box Layout, and thus some older implementations will not support it. The equivalent effect can be achieved by using auto together with a main size (width or height) of auto.
- <'width'>
- For all other values, flex-basis is resolved the same way as for width and height.
When omitted from the flex shorthand, its specified value is 0.
- none
- The keyword none expands to 0 0 auto.
A unitless zero that is not already preceded by two flex factors must be interpreted as a flex factor. To avoid misinterpretation or invalid declarations, authors must specify a zero <'flex-basis'> component with a unit or precede it by two flex factors.
7.1.1. Basic Values of flex
This section is informative.
The list below summarizes the effects of the four flex values that represent most commonly-desired effects:
- flex: initial
- Equivalent to flex: 0 1 auto. (This is the initial value.) Sizes the item based on the width/height properties. (If the item’s main size property computes to auto, this will size the flex item based on its contents.) Makes the flex item inflexible when there is positive free space, but allows it to shrink to its minimum size when there is insufficient space. The alignment abilities or auto margins can be used to align flex items along the main axis.
- flex: auto
- Equivalent to flex: 1 1 auto. Sizes the item based on the width/height properties, but makes them fully flexible, so that they absorb any free space along the main axis. If all items are either flex: auto, flex: initial, or flex: none, any positive free space after the items have been sized will be distributed evenly to the items with flex: auto.
- flex: none
- Equivalent to flex: 0 0 auto. This value sizes the item according to the width/height properties, but makes the flex item fully inflexible. This is similar to initial, except that flex items are not allowed to shrink, even in overflow situations.
- flex: <number [1,∞]>
- Equivalent to flex: <number [1,∞]> 1 0. Makes the flex item flexible and sets the flex basis to zero, resulting in an item that receives the specified proportion of the free space in the flex container. If all items in the flex container use this pattern, their sizes will be proportional to the specified flex factor.
Tests
By default, flex items won’t shrink below their minimum content size (the length of the longest word or fixed-size element). To change this, set the min-width or min-height property. (See § 4.5 Automatic Minimum Size of Flex Items.)
7.2. Components of Flexibility
Individual components of flexibility can be controlled by independent longhand properties.
Authors are encouraged to control flexibility using the flex shorthand rather than with its longhand properties directly, as the shorthand correctly resets any unspecified components to accommodate common uses.
7.2.1. The flex-grow property
| Name: | flex-grow |
|---|---|
| Value: | <number [0,∞]> |
| Initial: | 0 |
| Applies to: | flex items |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified number |
| Canonical order: | per grammar |
| Animation type: | by computed value type |
Tests
- flex-grow-interpolation.html (live test) (source)
- flex-001.htm (live test) (source)
- flex-003.htm (live test) (source)
- flex-grow-001.xht (live test) (source)
- flex-grow-002.html (live test) (source)
- flex-grow-003.html (live test) (source)
- flex-grow-004.html (live test) (source)
- flex-grow-005.html (live test) (source)
- flex-grow-006.html (live test) (source)
- flex-grow-007.html (live test) (source)
- flex-grow-008.html (live test) (source)
- flexbox_computedstyle_flex-grow-0.html (live test) (source)
- flexbox_computedstyle_flex-grow-invalid.html (live test) (source)
- flexbox_computedstyle_flex-grow-number.html (live test) (source)
- flexbox_interactive_flex-grow-transitions.html (manual test) (source)
- flex-grow-computed.html (live test) (source)
- flex-grow-invalid.html (live test) (source)
- flex-grow-valid.html (live test) (source)
Authors are encouraged to control flexibility using the flex shorthand rather than with flex-grow directly, as the shorthand correctly resets any unspecified components to accommodate common uses.
The flex-grow property sets the flex grow factor to the provided <number>. Negative values are not allowed.
7.2.2. The flex-shrink property
| Name: | flex-shrink |
|---|---|
| Value: | <number [0,∞]> |
| Initial: | 1 |
| Applies to: | flex items |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified value |
| Canonical order: | per grammar |
| Animation type: | number |
Tests
- flex-shrink-interpolation.html (live test) (source)
- flex-002.htm (live test) (source)
- flex-004.htm (live test) (source)
- flex-shrink-001.html (live test) (source)
- flex-shrink-002.html (live test) (source)
- flex-shrink-003.html (live test) (source)
- flex-shrink-004.html (live test) (source)
- flex-shrink-005.html (live test) (source)
- flex-shrink-006.html (live test) (source)
- flex-shrink-007.html (live test) (source)
- flex-shrink-008.html (live test) (source)
- flex-shrink-large-value-crash.html (live test) (source)
- flexbox_computedstyle_flex-shrink-0.html (live test) (source)
- flexbox_computedstyle_flex-shrink-invalid.html (live test) (source)
- flexbox_computedstyle_flex-shrink-number.html (live test) (source)
- flexbox_interactive_flex-shrink-transitions.html (manual test) (source)
- flexbox_interactive_flex-shrink-transitions-invalid.html (manual test) (source)
- flex-shrink-computed.html (live test) (source)
- flex-shrink-invalid.html (live test) (source)
- flex-shrink-valid.html (live test) (source)
Authors are encouraged to control flexibility using the flex shorthand rather than with flex-shrink directly, as the shorthand correctly resets any unspecified components to accommodate common uses.
The flex-shrink property sets the flex shrink factor to the provided <number>. Negative values are not allowed.
7.2.3. The flex-basis property
| Name: | flex-basis |
|---|---|
| Value: | content | <'width'> |
| Initial: | auto |
| Applies to: | flex items |
| Inherited: | no |
| Percentages: | relative to the flex container’s inner main size |
| Computed value: | specified keyword or a computed <length-percentage> value |
| Canonical order: | per grammar |
| Animation type: | by computed value type |
Tests
- flex-basis-composition.html (live test) (source)
- flex-basis-content-crash.html (live test) (source)
- flex-basis-interpolation.html (live test) (source)
- dynamic-isize-change-001.html (live test) (source)
- dynamic-isize-change-002.html (live test) (source)
- dynamic-isize-change-003.html (live test) (source)
- dynamic-isize-change-004.html (live test) (source)
- flex-basis-001.html (live test) (source)
- flex-basis-002.html (live test) (source)
- flex-basis-003.html (live test) (source)
- flex-basis-004.html (live test) (source)
- flex-basis-005.html (live test) (source)
- flex-basis-006.html (live test) (source)
- flex-basis-007.html (live test) (source)
- flex-basis-008.html (live test) (source)
- flex-basis-009.html (live test) (source)
- flex-basis-010.html (live test) (source)
- flex-basis-011.html (live test) (source)
- flex-basis-012.html (live test) (source)
- flex-basis-013.html (live test) (source)
- flex-basis-content-percentage-height.html (live test) (source)
- flex-basis-intrinsics-001.html (live test) (source)
- flex-basis-item-margins-001.html (live test) (source)
- flexbox-flex-basis-content-001a.html (live test) (source)
- flexbox-flex-basis-content-001b.html (live test) (source)
- flexbox-flex-basis-content-002a.html (live test) (source)
- flexbox-flex-basis-content-002b.html (live test) (source)
- flexbox-flex-basis-content-003a.html (live test) (source)
- flexbox-flex-basis-content-003b.html (live test) (source)
- flexbox-flex-basis-content-004a.html (live test) (source)
- flexbox-flex-basis-content-004b.html (live test) (source)
- flexbox_flex-basis.html (live test) (source)
- flexbox_flex-basis-shrink.html (live test) (source)
- flex-one-sets-flex-basis-to-zero-px.html (live test) (source)
- flex-shorthand-flex-basis-middle.html (live test) (source)
- flexbox_computedstyle_flex-basis-0.html (live test) (source)
- flexbox_computedstyle_flex-basis-0percent.html (live test) (source)
- flexbox_computedstyle_flex-basis-auto.html (live test) (source)
- flexbox_computedstyle_flex-basis-percent.html (live test) (source)
- flexbox_interactive_flex-basis-transitions.html (manual test) (source)
- flex-basis-computed.html (live test) (source)
- flex-basis-invalid.html (live test) (source)
- flex-basis-valid.html (live test) (source)
Authors are encouraged to control flexibility using the flex shorthand rather than with flex-basis directly, as the shorthand correctly resets any unspecified components to accommodate common uses.
The flex-basis property sets the flex basis. It accepts the same values as the width and height property, plus content.
For all values other than auto and content (defined above), flex-basis is resolved the same way as width in horizontal writing modes [CSS2], except that if a value would resolve to auto for width, it instead resolves to content for flex-basis. For example, percentage values of flex-basis are resolved against the flex item’s containing block (i.e. its flex container); and if that containing block’s size is indefinite, the used value for flex-basis is content. As another corollary, flex-basis determines the size of the content box, unless otherwise specified such as by box-sizing [CSS3UI].
8. Alignment
After a flex container’s contents have finished their flexing and the dimensions of all flex items are finalized, they can then be aligned within the flex container.
The margin properties can be used to align items in a manner similar to, but more powerful than, what margins can do in block layout. Flex items also respect the alignment properties from CSS Box Alignment, which allow easy keyword-based alignment of items in both the main axis and cross axis. These properties make many common types of alignment trivial, including some things that were very difficult in CSS 2.1, like horizontal and vertical centering.
Note: While the alignment properties are defined in CSS Box Alignment [CSS-ALIGN-3], Flexible Box Layout reproduces the definitions of the relevant ones here so as to not create a normative dependency that may slow down advancement of the spec. These properties apply only to flex layout until CSS Box Alignment Level 3 is finished and defines their effect for other layout modes. Additionally, any new values defined in the Box Alignment module will apply to Flexible Box Layout; in other words, the Box Alignment module, once completed, will supersede the definitions here.
Tests
8.1. Aligning with auto margins
This section is non-normative. The normative definition of how margins affect flex items is in the Flex Layout Algorithm section.
Auto margins on flex items have an effect very similar to auto margins in block flow:
-
During calculations of flex bases and flexible lengths, auto margins are treated as 0.
-
Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.
-
Overflowing boxes ignore their auto margins and overflow in the end direction.
Tests
- auto-height-column-with-border-and-padding.html (live test) (source)
- auto-height-with-flex.html (live test) (source)
- auto-margins-001.html (live test) (source)
- auto-margins-002.html (live test) (source)
- auto-margins-003.html (live test) (source)
- flexbox-margin-auto-horiz-001.xhtml (live test) (source)
- flexbox-margin-auto-horiz-002.xhtml (live test) (source)
- flexbox_margin-auto.html (live test) (source)
- flexbox_margin-auto-overflow.html (live test) (source)
- main-axis-margin-rounding.html (live test) (source)
Note: If free space is distributed to auto margins, the alignment properties will have no effect in that dimension because the margins will have stolen all the free space left over after flexing.
nav > ul{ display : flex; } nav > ul > #login{ margin-left : auto; }
< nav > < ul > < li >< a href = /about > About</ a > < li >< a href = /projects > Projects</ a > < li >< a href = /interact > Interact</ a > < li id = "login" >< a href = /login > Login</ a > </ ul > </ nav >
8.2. Axis Alignment: the justify-content property
| Name: | justify-content |
|---|---|
| Value: | flex-start | flex-end | center | space-between | space-around |
| Initial: | flex-start |
| Applies to: | flex containers |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
Tests
- dynamic-isize-change-001.html (live test) (source)
- dynamic-isize-change-002.html (live test) (source)
- dynamic-isize-change-003.html (live test) (source)
- dynamic-isize-change-004.html (live test) (source)
- flexbox_justifycontent-center.html (live test) (source)
- flexbox_justifycontent-center-overflow.html (live test) (source)
- flexbox_justifycontent-end.html (live test) (source)
- flexbox_justifycontent-end-rtl.html (live test) (source)
- flexbox_justifycontent-flex-end.html (live test) (source)
- flexbox_justifycontent-flex-start.html (live test) (source)
- flexbox-justify-content-horiz-001a.xhtml (live test) (source)
- flexbox-justify-content-horiz-001b.xhtml (live test) (source)
- flexbox-justify-content-horiz-002.xhtml (live test) (source)
- flexbox-justify-content-horiz-003.xhtml (live test) (source)
- flexbox-justify-content-horiz-004.xhtml (live test) (source)
- flexbox-justify-content-horiz-005.xhtml (live test) (source)
- flexbox-justify-content-horiz-006.xhtml (live test) (source)
- flexbox_justifycontent-left-001.html (live test) (source)
- flexbox_justifycontent-left-002.html (live test) (source)
- flexbox_justifycontent-right-001.html (live test) (source)
- flexbox_justifycontent-right-002.html (live test) (source)
- flexbox_justifycontent-rtl-001.html (live test) (source)
- flexbox_justifycontent-rtl-002.html (live test) (source)
- flexbox_justifycontent-spacearound.html (live test) (source)
- flexbox_justifycontent-spacearound-negative.html (live test) (source)
- flexbox_justifycontent-spacearound-only.html (live test) (source)
- flexbox_justifycontent-spacebetween.html (live test) (source)
- flexbox_justifycontent-spacebetween-negative.html (live test) (source)
- flexbox_justifycontent-spacebetween-only.html (live test) (source)
- flexbox_justifycontent-start.html (live test) (source)
- flexbox_justifycontent-start-rtl.html (live test) (source)
- flexbox_justifycontent-stretch.html (live test) (source)
- flexbox-justify-content-vert-001a.xhtml (live test) (source)
- flexbox-justify-content-vert-001b.xhtml (live test) (source)
- flexbox-justify-content-vert-002.xhtml (live test) (source)
- flexbox-justify-content-vert-003.xhtml (live test) (source)
- flexbox-justify-content-vert-004.xhtml (live test) (source)
- flexbox-justify-content-vert-005.xhtml (live test) (source)
- flexbox-justify-content-vert-006.xhtml (live test) (source)
- flexbox-justify-content-wmvert-001.xhtml (live test) (source)
- flexbox-justify-content-wmvert-002.html (live test) (source)
- flexbox-justify-content-wmvert-003.html (live test) (source)
- flexbox_margin.html (live test) (source)
- flexbox_margin-left-ex.html (live test) (source)
- flexbox_computedstyle_justify-content-center.html (live test) (source)
- flexbox_computedstyle_justify-content-flex-end.html (live test) (source)
- flexbox_computedstyle_justify-content-flex-start.html (live test) (source)
- flexbox_computedstyle_justify-content-space-around.html (live test) (source)
- flexbox_computedstyle_justify-content-space-between.html (live test) (source)
- justify-content-001.htm (live test) (source)
- justify-content-002.htm (live test) (source)
- justify-content-003.htm (live test) (source)
- justify-content-004.htm (live test) (source)
- justify-content-005.htm (live test) (source)
- justify-content-006.html (live test) (source)
- justify-content-007.html (live test) (source)
- justify-content_center.html (visual test) (source)
- justify-content_flex-end.html (visual test) (source)
- justify-content_flex-start.html (visual test) (source)
- justify-content-sideways-001.html (live test) (source)
- justify-content_space-around.html (visual test) (source)
- justify-content_space-between-001.html (visual test) (source)
- justify-content_space-between-002.html (live test) (source)
- scrollbars-auto.html (live test) (source)
- scrollbars.html (live test) (source)
- scrollbars-no-margin.html (live test) (source)
- justify-content-rounding.html (live test) (source)
The justify-content property aligns flex items along the main axis of the current line of the flex container. This is done after any flexible lengths and any auto margins have been resolved. Typically it helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.
- flex-start
- Flex items are packed toward the start of the line. The main-start margin edge of the first flex item on the line is placed flush with the main-start edge of the line, and each subsequent flex item is placed flush with the preceding item.
- flex-end
- Flex items are packed toward the end of the line. The main-end margin edge of the last flex item is placed flush with the main-end edge of the line, and each preceding flex item is placed flush with the subsequent item.
- center
- Flex items are packed toward the center of the line. The flex items on the line are placed flush with each other and aligned in the center of the line, with equal amounts of space between the main-start edge of the line and the first item on the line and between the main-end edge of the line and the last item on the line. (If the leftover free-space is negative, the flex items will overflow equally in both directions.)
- space-between
- Flex items are evenly distributed in the line. If the leftover free-space is negative or there is only a single flex item on the line, this value falls back to safe flex-start. Otherwise, the main-start margin edge of the first flex item on the line is placed flush with the main-start edge of the line, the main-end margin edge of the last flex item on the line is placed flush with the main-end edge of the line, and the remaining flex items on the line are distributed so that the spacing between any two adjacent items is the same.
- space-around
-
Flex items are evenly distributed in the line,
with half-size spaces on either end.
If the leftover free-space is negative or
there is only a single flex item on the line,
this value falls back to safe center.
Otherwise, the flex items on the line are distributed
such that the spacing between any two adjacent flex items on the line is the same,
and the spacing between the first/last flex items and the flex container edges
is half the size of the spacing between flex items.
Tests
- flexbox-column-row-gap-001.html (live test) (source)
- flexbox-column-row-gap-002.html (live test) (source)
- flexbox-column-row-gap-003.html (live test) (source)
- flexbox-column-row-gap-004.html (live test) (source)
- flexbox_columns-flexitems-2.html (live test) (source)
- flexbox_columns-flexitems.html (live test) (source)
- flexbox_justifycontent-spacearound.html (live test) (source)
- flexbox_justifycontent-spacearound-negative.html (live test) (source)
- flexbox_justifycontent-spacearound-only.html (live test) (source)
An illustration of the five justify-content keywords and their effects on a flex container with three colored items.
8.3. Cross-axis Alignment: the align-items and align-self properties
| Name: | align-items |
|---|---|
| Value: | flex-start | flex-end | center | baseline | stretch |
| Initial: | stretch |
| Applies to: | flex containers |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
Tests
- align-items-007.html (live test) (source)
- align-items-008.html (live test) (source)
- align-items-009.html (live test) (source)
- align-items-baseline-column-horz.html (live test) (source)
- align-items-baseline-column-vert.html (live test) (source)
- align-items-baseline-column-vert-lr-flexbox-item.html (live test) (source)
- align-items-baseline-column-vert-lr-grid-item.html (live test) (source)
- align-items-baseline-column-vert-lr-items.html (live test) (source)
- align-items-baseline-column-vert-lr-table-item.html (live test) (source)
- align-items-baseline-column-vert-rl-flexbox-item.html (live test) (source)
- align-items-baseline-column-vert-rl-grid-item.html (live test) (source)
- align-items-baseline-column-vert-rl-items.html (live test) (source)
- align-items-baseline-column-vert-rl-table-item.html (live test) (source)
- align-items-baseline-overflow-non-visible.html (live test) (source)
- align-items-baseline-row-horz.html (live test) (source)
- align-items-baseline-row-vert.html (live test) (source)
- align-items-baseline-vert-lr-column-horz-flexbox-item.html (live test) (source)
- align-items-baseline-vert-lr-column-horz-grid-item.html (live test) (source)
- align-items-baseline-vert-lr-column-horz-items.html (live test) (source)
- align-items-baseline-vert-lr-column-horz-table-item.html (live test) (source)
- align-items-baseline-vert-rl-column-horz-flexbox-item.html (live test) (source)
- align-items-baseline-vert-rl-column-horz-grid-item.html (live test) (source)
- align-items-baseline-vert-rl-column-horz-items.html (live test) (source)
- align-items-baseline-vert-rl-column-horz-table-item.html (live test) (source)
- multiline-align-self.html (live test) (source)
- column-flex-child-with-max-width.html (live test) (source)
- dynamic-stretch-change.html (live test) (source)
- flexbox_align-items-baseline.html (live test) (source)
- flexbox_align-items-center-2.html (live test) (source)
- flexbox_align-items-center-3.html (live test) (source)
- flexbox_align-items-center.html (live test) (source)
- flexbox-align-items-center-nested-001.html (live test) (source)
- flexbox_align-items-flexend-2.html (live test) (source)
- flexbox_align-items-flexend.html (live test) (source)
- flexbox_align-items-flexstart-2.html (live test) (source)
- flexbox_align-items-flexstart.html (live test) (source)
- flexbox_align-items-stretch-2.html (live test) (source)
- flexbox_align-items-stretch-3.html (live test) (source)
- flexbox_align-items-stretch-4.html (live test) (source)
- flexbox_align-items-stretch.html (live test) (source)
- flexbox_computedstyle_align-items-baseline.html (live test) (source)
- flexbox_computedstyle_align-items-center.html (live test) (source)
- flexbox_computedstyle_align-items-flex-end.html (live test) (source)
- flexbox_computedstyle_align-items-flex-start.html (live test) (source)
- flexbox_computedstyle_align-items-invalid.html (live test) (source)
- flexbox_computedstyle_align-items-stretch.html (live test) (source)
- position-relative-percentage-top-001.html (live test) (source)
- position-relative-percentage-top-002.html (live test) (source)
- position-relative-percentage-top-003.html (live test) (source)
- relayout-align-items.html (live test) (source)
- stretch-input-in-column.html (live test) (source)
- stretch-requires-computed-auto-size.html (live test) (source)
- table-as-item-stretch-cross-size-2.html (live test) (source)
- table-as-item-stretch-cross-size-3.html (live test) (source)
- table-as-item-stretch-cross-size-4.html (live test) (source)
- table-as-item-stretch-cross-size-5.html (live test) (source)
- table-as-item-stretch-cross-size.html (live test) (source)
| Name: | align-self |
|---|---|
| Value: | auto | flex-start | flex-end | center | baseline | stretch |
| Initial: | auto |
| Applies to: | flex items |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
Tests
- align-self-006.html (visual test) (source)
- align-self-010.html (visual test) (source)
- align-self-013.html (live test) (source)
- align-self-014.html (live test) (source)
- align-self-015.html (live test) (source)
- align-self-016.html (live test) (source)
- flexbox_align-self-auto.html (live test) (source)
- flexbox-align-self-baseline-compatability.html (live test) (source)
- flexbox-align-self-baseline-horiz-001a.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-001b.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-002.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-003.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-004.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-005.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-006.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-007.xhtml (live test) (source)
- flexbox-align-self-baseline-horiz-008.xhtml (live test) (source)
- flexbox_align-self-baseline.html (live test) (source)
- flexbox_align-self-center.html (live test) (source)
- flexbox_align-self-flexend.html (live test) (source)
- flexbox_align-self-flexstart.html (live test) (source)
- flexbox-align-self-horiz-001-block.xhtml (live test) (source)
- flexbox-align-self-horiz-001-table.xhtml (live test) (source)
- flexbox-align-self-horiz-002.xhtml (live test) (source)
- flexbox-align-self-horiz-003.xhtml (live test) (source)
- flexbox-align-self-horiz-004.xhtml (live test) (source)
- flexbox-align-self-horiz-005.xhtml (live test) (source)
- flexbox_align-self-stretch.html (live test) (source)
- flexbox-align-self-stretch-vert-001.html (live test) (source)
- flexbox-align-self-stretch-vert-002.html (live test) (source)
- flexbox-align-self-vert-001.xhtml (live test) (source)
- flexbox-align-self-vert-002.xhtml (live test) (source)
- flexbox-align-self-vert-003.xhtml (live test) (source)
- flexbox-align-self-vert-004.xhtml (live test) (source)
- flexbox-align-self-vert-rtl-001.xhtml (live test) (source)
- flexbox-align-self-vert-rtl-002.xhtml (live test) (source)
- flexbox-align-self-vert-rtl-003.xhtml (live test) (source)
- flexbox-align-self-vert-rtl-004.xhtml (live test) (source)
- flexbox-align-self-vert-rtl-005.xhtml (live test) (source)
- flexbox_computedstyle_align-self-baseline.html (live test) (source)
- flexbox_computedstyle_align-self-center.html (live test) (source)
- flexbox_computedstyle_align-self-flex-end.html (live test) (source)
- flexbox_computedstyle_align-self-flex-start.html (live test) (source)
- flexbox_computedstyle_align-self-invalid.html (live test) (source)
- flexbox_computedstyle_align-self-stretch.html (live test) (source)
Flex items can be aligned in the cross axis of the current line of the flex container, similar to justify-content but in the perpendicular direction. align-items sets the default alignment for all of the flex container’s items, including anonymous flex items. align-self allows this default alignment to be overridden for individual flex items. (For anonymous flex items, align-self always matches the value of align-items on their associated flex container.)
If either of the flex item’s cross-axis margins are auto, align-self has no effect.
Values have the following meanings:
- auto
- Defers cross-axis alignment control to the value of align-items on the parent box. (This is the initial value of align-self.)
- flex-start
- The cross-start margin edge of the flex item is placed flush with the cross-start edge of the line.
- flex-end
- The cross-end margin edge of the flex item is placed flush with the cross-end edge of the line.
- center
- The flex item’s margin box is centered in the cross axis within the line. (If the cross size of the flex line is less than that of the flex item, it will overflow equally in both directions.)
- baseline
- The flex item participates in baseline alignment: all participating flex items on the line are aligned such that their baselines align, and the item with the largest distance between its baseline and its cross-start margin edge is placed flush against the cross-start edge of the line. If the item does not have a baseline in the necessary axis, then one is synthesized from the flex item’s border box.
- stretch
-
If the cross size property of the flex item computes to auto,
and neither of the cross-axis margins are auto,
the flex item is stretched.
Its used value is the length necessary to make the cross size of the item’s margin box as close to the same size as the line as possible,
while still respecting the constraints imposed by min-height/min-width/max-height/max-width.
Note: If the flex container’s height is constrained this value may cause the contents of the flex item to overflow the item.
The cross-start margin edge of the flex item is placed flush with the cross-start edge of the line.
Tests
- align-items-005.htm (live test) (source)
- align-self-004.html (live test) (source)
- align-self-005.html (live test) (source)
- css-flexbox-height-animation-stretch.html (manual test) (source)
An illustration of the five align-items keywords and their effects on a flex container with four colored items.
8.4. Packing Flex Lines: the align-content property
| Name: | align-content |
|---|---|
| Value: | flex-start | flex-end | center | space-between | space-around | stretch |
| Initial: | stretch |
| Applies to: | multi-line flex containers |
| Inherited: | no |
| Percentages: | n/a |
| Computed value: | specified keyword |
| Canonical order: | per grammar |
| Animation type: | discrete |
The align-content property aligns a flex container’s lines within the flex container when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis. Note, this property has no effect on a single-line flex container. Values have the following meanings:
- flex-start
- Lines are packed toward the start of the flex container. The cross-start edge of the first line in the flex container is placed flush with the cross-start edge of the flex container, and each subsequent line is placed flush with the preceding line.
- flex-end
- Lines are packed toward the end of the flex container. The cross-end edge of the last line is placed flush with the cross-end edge of the flex container, and each preceding line is placed flush with the subsequent line.
- center
- Lines are packed toward the center of the flex container. The lines in the flex container are placed flush with each other and aligned in the center of the flex container, with equal amounts of space between the cross-start content edge of the flex container and the first line in the flex container, and between the cross-end content edge of the flex container and the last line in the flex container. (If the leftover free-space is negative, the lines will overflow equally in both directions.)
- space-between
- Lines are evenly distributed in the flex container. If the leftover free-space is negative or there is only a single flex line in the flex container, this value falls back to safe flex-start. Otherwise, the cross-start edge of the first line in the flex container is placed flush with the cross-start content edge of the flex container, the cross-end edge of the last line in the flex container is placed flush with the cross-end content edge of the flex container, and the remaining lines in the flex container are distributed so that the spacing between any two adjacent lines is the same.
- space-around
- Lines are evenly distributed in the flex container, with half-size spaces on either end. If the leftover free-space is negative this value falls back to safe center. Otherwise, the lines in the flex container are distributed such that the spacing between any two adjacent lines is the same, and the spacing between the first/last lines and the flex container edges is half the size of the spacing between flex lines.
- stretch
- Lines stretch to take up the remaining space. If the leftover free-space is negative, this value falls back to flex-start. Otherwise, the free-space is split equally between all of the lines, increasing their cross size.
Note: Only multi-line flex containers ever have free space in the cross-axis for lines to be aligned in, because in a single-line flex container the sole line automatically stretches to fill the space.
An illustration of the align-content keywords and their effects on a multi-line flex container.
Tests
- align-content_center.html (live test) (source)
- align-content_flex-end.html (live test) (source)
- align-content_flex-start.html (live test) (source)
- align-content-horiz-001a.html (live test) (source)
- align-content-horiz-001b.html (live test) (source)
- align-content-horiz-002.html (live test) (source)
- align-content-stretch-rounding.html (live test) (source)
- align-content_space-around.html (live test) (source)
- align-content_space-between.html (live test) (source)
- align-content_stretch.html (live test) (source)
- align-content-vert-001a.html (live test) (source)
- align-content-vert-001b.html (live test) (source)
- align-content-vert-002.html (live test) (source)
- align-content-wmvert-001.html (live test) (source)
- align-content-wrap-001.html (live test) (source)
- align-content-wrap-002.html (live test) (source)
- align-content-wrap-003.html (live test) (source)
- align-content-wrap-004.html (live test) (source)
- align-content-wrap-005.html (live test) (source)
- flex-align-content-center.html (live test) (source)
- flex-align-content-end.html (live test) (source)
- flex-align-content-space-around.html (live test) (source)
- flex-align-content-space-between.html (live test) (source)
- flex-align-content-start.html (live test) (source)
- flexbox_align-content-center.html (live test) (source)
- flexbox_align-content-flexend.html (live test) (source)
- flexbox_align-content-flexstart.html (live test) (source)
- flexbox_align-content-spacearound.html (live test) (source)
- flexbox_align-content-spacebetween.html (live test) (source)
- flexbox_align-content-stretch-2.html (live test) (source)
- flexbox_align-content-stretch.html (live test) (source)
- flexbox_computedstyle_align-content-center.html (live test) (source)
- flexbox_computedstyle_align-content-flex-end.html (live test) (source)
- flexbox_computedstyle_align-content-flex-start.html (live test) (source)
- flexbox_computedstyle_align-content-space-around.html (live test) (source)
- flexbox_computedstyle_align-content-space-between.html (live test) (source)
- align-content-rounding.html (live test) (source)
8.5. Flex Container Baselines
In order for a flex container to itself participate in baseline alignment (e.g. when the flex container is itself a flex item in an outer flex container), it needs to submit the position of the baselines that will best represent its contents. To this end, the baselines of a flex container are determined as follows (after reordering with order, and taking flex-direction into account):
- first/last main-axis baseline set
-
When the inline axis of the flex container
matches its main axis,
its baselines are determined as follows:
-
If any of the flex items on the flex container’s startmost flex line participate in first baseline alignment along the main axis, generate a baseline set from their shared alignment baseline and the flex container’s first available font.
-
Otherwise, if any of the flex items on the flex container’s startmost flex line participate in last
-