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 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.
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, 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).
Percentages continue to resolve as normal,
unaffected by this available space adjustment.
(To have a flex item fill the cross-axisavailable space,
use height: stretch.)
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:
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
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.
Firefox28+Safari9+Chrome29+Opera17+Edge79+Edge (Legacy)12+IENoneFirefox for Android52+iOS Safari?Chrome for Android?Android WebView4.4+Samsung Internet?Opera Mobile18+