Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.
This module contains the features of CSS relating to the alignment of boxes within their containers in the various CSS box layout models: block layout, table layout, flex layout, and grid layout. (The alignment of text and inline-level content is defined in [CSS3TEXT] and [CSS3LINE].) CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.
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.
The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css-align” in the subject, preferably like this: “[css-align] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This section is not normative.
CSS Levels 1 and 2 allowed for the alignment of text via text-align and the alignment of blocks by balancing auto margins. However, except in table cells, vertical alignment was not possible. As CSS3 adds further capabilities, the ability to align boxes in various dimensions becomes more critical. This module attempts to create a cohesive and common box alignment model to share among all of CSS.
Note: The alignment of text and inline-level content is defined in [CSS3TEXT] and [CSS3LINE].
Inspiration for this document:
This module adds some new alignment capabilities to the block layout model described in [CSS21] chapters 9 and 10 and defines the interaction of these properties with the alignment of table cell content using vertical-align, as defined in [CSS21] chapter 17. The interaction of these properties with Grid Layout [CSS3-GRID-LAYOUT] and Flexible Box Layout [CSS3-FLEXBOX] is defined in their respective modules.
No properties in this module apply to the ::first-line
or
::first-letter
pseudo-elements.
This specification follows the CSS property definition conventions from [CSS21]. Value types not defined in this specification are defined in CSS Level 2 Revision 1 [CSS21]. Other CSS modules may expand the definitions of these value types: for example [CSS3VAL], when combined with this module, adds the initial keyword as a possible property value.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the inherit keyword as their property value. For readability it has not been repeated explicitly.
The alignment properties in CSS can be described along two axes:
Note: This proposal uses the terms “justify” and “align” to distinguish between alignment in the inline and stacking dimensions, respectively. The choice is somewhat arbitrary, but having the two terms allows for a consistent naming scheme that works across all of CSS’s layout models.
The following table summarizes the proposed alignment properties and the display types they can apply to.
Common | Axis | Aligns | Applies to |
---|---|---|---|
justify-content | inline |
| block containers and flex containers |
align-content | stacking | block containers and flex containers | |
justify-self | inline |
| block-level elements and grid items |
align-self | stacking | flex items and grid items | |
justify-items | inline |
| grid containers |
align-items | stacking | flex containers and grid containers |
The exact definition of these properties outside what’s what’s defined in Flexbox is still being worked out. This is a second Working Draft and is NOT STABLE.
Note: The *-items values don’t affect the element itself. When set on a flex container or grid container, they specify the interpretation of any align/justify-self: auto used on the items in the container element.
All of the alignment properties use a common set of values, defined below.
The positional alignment keywords specify a position for an alignment subject with respect to its alignment container.
The <item-position> set of values is used by justify-self and align-self to align the box within its alignment container, and also by justify-items and align-items (to specify default values for justify-self and align-self). The <content-position> set of values is used by justify-content and align-content to align the box’s contents within itself.
<item-position> = center | start | end | self-start | self-end | flex-start | flex-end | left | right; <content-position> = center | start | end | flex-start | flex-end | left | right;
Values have the following meanings:
Baseline alignment is a form of positional alignment that relies on aligning the baselines of related alignment subjects to each other.
In the case of align-content/justify-content, this shifts the content of the box within it, and may also affect its sizing. In the case of align-self/justify-self this shifts the entire box within its container, and may affect the sizing of its container.
If the alignment subject’s position is not fully determined by baseline alignment, the content is start-aligned insofar as possible while preserving the baseline alignment. (Content that has no first-line baseline is thus start-aligned.)
The baseline of a box is determined differently based on the layout model, as follow:
A block has no block-axis baseline.
The block-axis baseline of a table is undefined.
Or does it have no baseline? Or is it based on its first column?
A table row has no block-axis baseline.
A table cell has no block-axis baseline.
Maybe these things are wrong? CSS 2.1 is really weird about baseline alignment. This whole section needs to be better defined and reviewed.
The distribution values are used by justify-content and align-content to distribute the items in the alignment subject evenly between the start and end edges of the alignment container. When the alignment subject cannot be distributed in this way, they behave as their fallback alignment. Each distribution value has an associated <content-position> as a fallback alignment, but one can alternatively be explicitly specified in the property.
<content-distribution> = space-between | space-around | space-evenly | stretch
When the alignment subject is larger than the alignment container, it will overflow. Some alignment modes, if honored in this situation, may cause data loss: for example, if the contents of a sidebar are centered, when they overflow they may send part of their boxes past the viewport’s start edge, which can’t be scrolled to.
To help combat this problem, an overflow alignment mode can be explicitly specified. "True" alignment honors the specified alignment mode in overflow situations, even if it causes data loss, while "safe" alignment changes the alignment mode in overflow situations in an attempt to avoid data loss.
If the overflow alignment isn’t explicitly specified, the default overflow alignment is determined by the layout mode. Document-centric layout modes, such as block layout, default to "safe" overflow alignment, while design-centric layout modes, such as flex layout, default to "true" overflow alignment.
<overflow-position> = true | safe
Transplant example 10 from flexbox.
The justify-content and align-content properties control alignment of the box’s content within the box.
Name: | justify-content, align-content |
---|---|
Value: | auto | baseline | [ <item-position> <content-position>? | <content-position> ] && <overflow-position>? |
Initial: | auto |
Applies to: | block containers, flex containers, and grid containers |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
Animatable: | no |
Aligns the contents of the box as a whole along the box’s inline/row/main axis. Values other than auto are defined above. If both a <item-position> and <content-position> are given, the second value represents an explicit fallback alignment.
The align-content property applies along the block axis, but if a <content-distribution> is specified the fallback alignment is used instead. The justify-content property does not apply to and has no effect on block containers.
All values other than auto force the block container to establish a new formatting context. For table cells, the behavior of the auto depends on the computed value of vertical-align: top makes it behave as start, middle makes it behave as center, bottom makes it behave as end, and all other values make it behave as baseline. auto otherwise behaves as start.
The align-content property applies along the block axis, but if a <content-distribution> is specified the fallback alignment is used instead. The justify-content property does not apply to and has no effect on multi-column elements.
auto behaves as start.
The alignment container is the flex container’s content box. For justify-content, the alignment subject is the flex items in each flex line; for align-content, the alignment subject is the flex lines.
The align-content property applies along the cross axis. The justify-content property applies along the main axis, but stretch behaves as start.
See [CSS3-FLEXBOX] for details.
The alignment container is the grid container’s content box. The alignment subject is the bounds of the grid. Need to dfn a better term for this in Grid.
The align-content property applies along the block (column) axis. The justify-content property applies along the inline (row) axis. In both properties, if a <content-distribution> is specified, the fallback alignment is used instead.
The content of boxes participating in row-like layout contexts can be baseline-aligned to each other. The alignment subject of a box participating in baseline content-alignment is shifted such that the baseline of the first formatted line of all participating boxes align, and the intrinsic size of each box is increased by the distance the content needed to shift in order to thus align the baselines.
The set of boxes that participate in baseline content-alignment together depends on the layout model:
If a cell/item spans multiple rows or columns, it participates in alignment within its start-most row/column only.
The justify-self and align-self properties control alignment of the box within its containing block.
Name: | justify-self |
---|---|
Value: | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] |
Initial: | auto |
Applies to: | block-level boxes, absolutely-positioned boxes, and grid items |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
Animatable: | no |
Justifies the box within its parent along the inline/row/main axis: the box’s outer edges are aligned within its alignment container as described by its alignment value.
The auto keyword computes to stretch on absolutely-positioned elements, and to the computed value of justify-items on the parent (minus any legacy keywords) on all other boxes.
The alignment container is the block’s containing block
except that for block-level elements that establish a block formatting context
and are placed next to a float,
the alignment container is reduced by the space taken up by the float.
(Note: This is the legacy behavior of HTML align
.)
The alignment subject is the block’s margin box.
The default overflow alignment is safe. In terms of CSS2.1 block-level formatting [CSS21], the rules for "over-constrained" computations in section 10.3.3 are ignored in favor of alignment as specified here and the used value of the offset properties are not adjusted to correct for the over-constraint.
The stretch value is equivalent to start on block-level boxes.
This property does not apply to floats.
When neither margin in this dimension is auto and neither offset property in this dimension is auto, values other than stretch cause non-replaced absolutely-positioned boxes to use shrink-to-fit sizing for calculating auto measures, and justify-self dictates alignment as follows:
The alignment container is the box’s containing block as modified by the offset properties (top/right/bottom/left). The alignment subject is the box’s margin box.
The default overflow alignment is safe. In terms of CSS2.1 formatting [CSS21], the rules for "over-constrained" computations in section 10.3.7 are ignored in favor of alignment as specified here and the used value of the offset properties are not adjusted to correct for the over-constraint.
The stretch keyword is equivalent to start on replaced absolutely-positioned boxes. (This is because CSS 2.1 does not stretch replaced elements to fit into fixed offsets.)
The alignment container is the grid cell. The alignment subject is the grid item’s margin box. The default overflow alignment is true.
Should we make stretch actually work on block-level/abspos replaced boxes? To maintain legacy compat, this requires adding a new value (named normal?) which has the current behavior of stretching non-replaced boxes and start-aligning replaced ones.
Name: | align-self |
---|---|
Value: | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] |
Initial: | auto |
Applies to: | block-level boxes |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
Animatable: | no |
Aligns the box within its parent along the block/column/cross axis: the box’s outer edges are aligned within its alignment container as described by its alignment value.
The auto keyword computes to stretch on absolutely-positioned elements, and to the computed value of align-items on the parent (minus any legacy keywords) on all other boxes.
When neither margin in this dimension is auto and neither offset property in this dimension is auto, values other than stretch cause non-replaced absolutely-positioned boxes to use shrink-to-fit sizing for calculating auto measures, and align-self dictates alignment as follows:
The alignment container is the box’s containing block as modified by the offset properties (top/right/bottom/left). The alignment subject is the box’s margin box.
The default overflow alignment is safe. In terms of CSS2.1 formatting [CSS21], the rules for "over-constrained" computations in section 10.6.4 are ignored in favor of alignment as specified here and the used value of the offset properties are not adjusted to correct for the over-constraint.
The stretch keyword is equivalent to start on replaced absolutely-positioned boxes. (This is because CSS 2.1 does not stretch replaced elements to fit into fixed offsets.)
The alignment container is the flex line the item is in. The alignment subject is the flex item’s margin box. The default overflow alignment is true. See [CSS3-FLEXBOX] for details.
The alignment container is the grid cell. The alignment subject is the grid item’s margin box. The default overflow alignment is true.
Boxes participating in row-like layout contexts can be baseline-aligned to each other. An alignment subject participating in baseline content-alignment is shifted such that the baseline of the first formatted line of all participating boxes align, and the intrinsic size of its alignment container is increased by the largest distance a box needed to shift in order to thus align the baselines.
The set of boxes that participate in baseline self-alignment together depends on the layout model:
If a cell/item spans multiple rows or columns, it participates in alignment within its start-most row/column only.
The align-items and justify-items properties set the default align-self and justify-self behavior of the items contained by the element.
Name: | justify-items |
---|---|
Value: | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] | [ legacy && [ left | right | center ] ] |
Initial: | auto |
Applies to: | block containers, flex containers, and grid containers |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
Animatable: | no |
This property specifies the default justify-self for all of the boxes (including anonymous boxes) participating in this box’s formatting context. Values have the following meanings:
Otherwise, auto computes to:
<center>
element and align
attribute.
Other values have no special handling and are merely passed to justify-self.
Name: | align-items |
---|---|
Value: | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] |
Initial: | auto |
Applies to: | block-level elements |
Inherited: | no |
Media: | visual |
Computed value: | specified value |
Percentages: | n/a |
Animatable: | no |
This property specifies the default align-self for all of the boxes (including anonymous boxes) participating in this box’s formatting context. Values have the following meanings:
Other values have no special handling and are merely passed to align-self.
Changes since the First Public Working Draft include:
<CENTER>>
/align
attribute requirements
by adding legacy keyword.
Special thanks goes to Markus Mielke, Alex Mogilevsky, and the participants in the CSSWG’s March 2008 F2F alignment discussions.
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.
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.
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.
To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS.
Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.
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.
Name | Value | Initial | Applies to | Inh. | %ages | Media | Animatable | Computed value |
---|---|---|---|---|---|---|---|---|
justify-content | auto | baseline | [ <item-position> <content-position>? | <content-position> ] && <overflow-position>? | auto | block containers, flex containers, and grid containers | no | n/a | visual | no | specified value |
align-content | auto | baseline | [ <item-position> <content-position>? | <content-position> ] && <overflow-position>? | auto | block containers, flex containers, and grid containers | no | n/a | visual | no | specified value |
justify-self | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] | auto | block-level boxes, absolutely-positioned boxes, and grid items | no | n/a | visual | no | specified value |
align-self | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] | auto | block-level boxes | no | n/a | visual | no | specified value |
justify-items | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] | [ legacy && [ left | right | center ] ] | auto | block containers, flex containers, and grid containers | no | n/a | visual | no | specified value |
align-items | auto | stretch | baseline | [ <item-position> && <overflow-position>? ] | auto | block-level elements | no | n/a | visual | no | specified value |