CSS Flexible Box Layout Module Level 2

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-flexbox-2/
Issue Tracking:
CSSWG Issues Repository
Editors:
Tab Atkins Jr. (Google)
Elika J. Etemad / fantasai (Apple)
(Microsoft)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

The specification describes a CSS box model optimized for user interface design. In the flex layout model, the children of a flex container can be laid out in any direction, and can “flex” their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent. Both horizontal and vertical alignment of the children can be easily manipulated. Nesting of these boxes (horizontal inside vertical, or vertical inside horizontal) can be used to build layouts in two dimensions.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-flexbox” in the title, like this: “[css-flexbox] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document is governed by the 18 August 2025 W3C Process Document.

1. Introduction

This section is not normative.

This module is currently a diff spec over [CSS-FLEXBOX-1]. It defines a new flex wrapping mode, flex-wrap: balance, which attempts to find linebreak locations which make the size of each flex line as similar as possible.

2. Ordering and Orientation

...

2.1. Flex Line Wrapping: the flex-wrap property

Name: flex-wrap
Value: nowrap | [ wrap | wrap-reverse ] || balance
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
The flex container is multi-line, but the lines are stacked "in reverse" (see below).
balance
The flex container is multi-line, and attempts to "balance" the lengths of the flex lines to be as similar as possible. See [[#flex-balance=]].

balance can be combined with wrap or wrap-reverse to dictate which direction the flex lines are stacked in. If neither are specified, it behaves as if wrap was specified.

By default, the cross-start direction of the flex container is either the inline-start or block-start direction of the flex container’s writing mode (whichever is in the cross axis) and the cross-end direction is the opposite direction of cross-start. If wrap-reverse is specified, 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.

3. Flex Lines

...

3.1. Minimum Flex Lines: the flex-line-count property

Name: flex-line-count
Value: <integer [1,∞]>
Initial: 1
Applies to: multi-line flex containers
Inherited: no
Percentages: n/a
Computed value: the specified integer, computed
Canonical order: per grammar
Animation type: as integer

By default, a balance flexbox will create as many lines as a normal multi-line flexbox, then rebalance how the flex items are assigned across that many lines. In some situations, an author might want to ensure there are always a certain number of lines, or might be using a flexbox in a way where there is no limit to the line length (the standard for a column flexbox) and so the "default" line number calculation is useless. The flex-line-count property specifies a minimum number of flex lines that the flex container should create to balance the items across.

If the flex container has less flex items than the flex-line-count value, it will only create as many lines as there are flex items. If the flex lines are limited in length and the flex items will fill more than the specified number of lines, the actual flex line count can be higher than what is specified.

Unused lines (ones with no flex items assigned to them) are discarded and do not affect layout. This only affects non-balance flexboxes. A balance flexbox will always assign at least one flex item to each line, and if it has less items than flex-line-count, that’s handled specially, above.

In addition to setting a minimum number of lines that are used by balance, flex-line-count changes the available space in the cross axis for flex items: subtracting the size of N-1 gaps from the normal available space, then dividing the leftover by N, where N is the flex-line-count value. This uses the actual flex-line-count value, not the number of lines produced (which can be higher or lower than what is specified).

Note: This available space adjustment is the only effect flex-line-count has on non-balance flexboxes.

Percentages continue to resolve as normal, unaffected by this available space adjustment. (To have a flex item fill the cross-axis available space, use height: stretch.)

If the flex container is single-line (aka, flex-wrap: nowrap), this property has no effect.

4. Flex Layout Algorithm

...

TODO fill in the details of how balancing works.

In short, do flex layout up to the linebreaking part. Linebreak as normal (but with the floor-at-0 thing, below) to discover how many lines are needed; if flex-line-count is higher (and there are at least that many flex items), use that line count instead.

Then do balanced line-breaking, using a well-known algorithm I forget the name of right this moment.

For the purpose of the balancing algorithm, hypothetical sizes are floored at 0, so it can be done efficiently. (Negative margins can make the hypothetical size negative, which does affect line-breaking.)

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Tests

Tests relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.


Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Partial implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-FLEXBOX-1]
Elika Etemad; Tab Atkins Jr.; Rossen Atanassov. CSS Flexible Box Layout Module Level 1. URL: https://drafts.csswg.org/css-flexbox/
[CSS-SIZING-3]
Tab Atkins Jr.; Elika Etemad. CSS Box Sizing Module Level 3. URL: https://drafts.csswg.org/css-sizing-3/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. URL: https://drafts.csswg.org/css-values-4/
[CSS-WRITING-MODES-4]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 4. URL: https://drafts.csswg.org/css-writing-modes-4/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119

Non-Normative References

[CSS-ALIGN-3]
Elika Etemad; Tab Atkins Jr.. CSS Box Alignment Module Level 3. URL: https://drafts.csswg.org/css-align/
[CSS-OVERFLOW-3]
Elika Etemad; Florian Rivoal. CSS Overflow Module Level 3. URL: https://drafts.csswg.org/css-overflow-3/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value
flex-line-count <integer [1,∞]> 1 multi-line flex containers no n/a as integer per grammar the specified integer, computed
flex-wrap nowrap | [ wrap | wrap-reverse ] || balance nowrap flex containers no n/a discrete per grammar specified keyword
MDN

flex-wrap

In all current engines.

Firefox28+Safari9+Chrome29+
Opera17+Edge79+
Edge (Legacy)12+IENone
Firefox for Android52+iOS Safari?Chrome for Android?Android WebView4.4+Samsung Internet?Opera Mobile18+