CSS Box Alignment Module Level 3

Editor’s Draft,

This version:
https://drafts.csswg.org/css-align/
Latest published version:
https://www.w3.org/TR/css-align-3/
Previous Versions:
https://www.w3.org/TR/2016/WD-css-align-3-20160519/
https://www.w3.org/TR/2014/WD-css-align-3-20141218/
https://www.w3.org/TR/2013/WD-css3-align-20130514/
https://www.w3.org/TR/2012/WD-css3-align-20120612/
Issue Tracking:
GitHub
Inline In Spec
Editors:
Elika J. Etemad / fantasai (Invited Expert)
Tab Atkins Jr. (Google)

Abstract

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 [CSS-TEXT-3] and [CSS-INLINE-3].)

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, 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.

GitHub Issues are preferred for discussion of this specification. When filing an issue, please put the text “css-align” in the title, preferably like this: “[css-align] …summary of comment…”. All issues and comments are archived, and there is also a historical archive.

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 document is governed by the 1 September 2015 W3C Process Document.

The following features are at-risk, and may be dropped during the CR period:

“At-risk” is a W3C Process term-of-art, and does not necessarily imply that the feature is in danger of being dropped or delayed. It means that the WG believes the feature may have difficulty being interoperably implemented in a timely manner, and marking it as such allows the WG to drop the feature if necessary when transitioning to the Proposed Rec stage, without having to publish a new Candidate Rec without the feature first.

1. Introduction

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 [CSS-TEXT-3] and [CSS-INLINE-3].

Inspiration for this document:

1.1. Module interactions

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 [CSS-GRID-1] and Flexible Box Layout [CSS-FLEXBOX-1] is defined in their respective modules. The property definitions here supersede those in [CSS-FLEXBOX-1] (which have a smaller, earlier subset of permissible values).

No properties in this module apply to the ::first-line or ::first-letter pseudo-elements.

1.2. Values

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.

2. Overview of Alignment Properties

The box alignment properties in CSS are a set of 6 properties that control alignment of boxes within other boxes. They can be described along two axises:

Note: This specification uses the terms “justify” and “align” to distinguish between alignment in the main/inline and cross/block 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 box alignment properties and the display types they can apply to.

Common Axis Aligns Applies to
justify-content main/inline content within element (effectively adjusts padding) block containers, flex containers, and grid containers
align-content cross/block block containers, flex containers, and grid containers
justify-self inline element within parent (effectively adjusts margins) block-level elements and grid items
align-self cross/block flex items and grid items
justify-items inline items inside element (controls child items’ justify-self: auto) grid containers
align-items cross/block flex containers and grid containers

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 *-self: auto used on the items in the container element.

3. Alignment Values

All of the alignment properties use a common set of values, defined below.

alignment subject
The alignment subject is the thing or things being aligned by the property. For justify-self and align-self, the alignment subject is the margin box of the box the property is set on. For justify-content and align-content, the alignment subject is defined by the layout mode.
alignment container
The alignment container is the rectangle that the alignment subject is aligned within. This is defined by the layout mode, but is usually the alignment subject’s containing block.

3.1. Positional Alignment: the center, start, end, self-start, self-end, flex-start, flex-end, left, and right keywords

The positional alignment keywords specify a position for an alignment subject with respect to its alignment container.

The <self-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.

<self-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:

center (self, content)
Centers the alignment subject within its alignment container.
start (self, content)
Aligns the alignment subject to be flush with the alignment container’s start edge in the appropriate axis.
end (self, content)
Aligns the alignment subject to be flush with the alignment container’s end edge in the appropriate axis.
self-start (self)
Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the alignment subject’s start side in the appropriate axis.
self-end (self)
Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the alignment subject’s end side in the appropriate axis.
flex-start (self, content)
Only used in flex layout. [CSS-FLEXBOX-1] Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the flex container’s main-start or cross-start side, as appropriate. When used on boxes that are not children of a flex container, this value behaves as start.
flex-end (self, content)
Only used in flex layout. Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the flex container’s main-end or cross-end side, as appropriate. When used on boxes that are not children of a flex container, this value behaves as end.
left (self, content)
Aligns the alignment subject to be flush with the alignment container’s line-left edge. If the property’s axis is not parallel with the inline axis, this value behaves as start.
right (self, content)
Aligns the alignment subject to be flush with the alignment container’s line-right edge. If the property’s axis is not parallel with the inline axis, this value behaves as start.

Add example images.

Make it easier to understand the dual-axis nature of "start" and "end" wrt orthogonal flows.

3.2. Baseline Alignment: the baseline and last-baseline keywords

See CSS Writing Modes 3 §4.1 Introduction to Baselines.

Baseline alignment is a form of positional alignment that aligns multiple alignment subjects within a shared alignment context (such as cells within a row or column) by matching up their alignment baselines.

The baseline alignment keywords are:

<baseline-position> = baseline | last-baseline

and are defined below:

baseline
Indicates first-baseline alignment: If the box has a natural baseline in the appropriate axis, aligns one of the baselines in the box’s first baseline set in that axis with the corresponding baseline in the first baseline sets of all the boxes in its baseline-sharing group.

If the alignment subject’s position is not fully determined by baseline alignment, or the box has no natural baseline in the appropriate axis, the content is start-aligned insofar as possible while preserving the baseline alignment. (Alignment subjects that have no inline-level content are thus start-aligned.)

last-baseline
Indicates last-baseline alignment: If the box has a natural baseline in the appropriate axis, aligns one of the baselines in the box’s last baseline set in that axis with the corresponding baseline in the last baseline sets of all the boxes in its baseline-sharing group.

If the alignment subject’s position is not fully determined by baseline alignment, or the box has no natural baseline in the appropriate axis, the content is end-aligned insofar as possible while preserving the baseline alignment. (Alignment subjects that have no inline-level content are thus end-aligned.)

Should this end fallback be safe or unsafe?

These values give a box a baseline alignment preference: either “first” or “last”, respectively.

When specified for align-content/justify-content, these values trigger baseline content-alignment, shifting the content of the box within its content box, and may also affect the sizing of the box itself. See §4.2 Baseline Content-Alignment.

When specified for align-self/justify-self, these values trigger baseline self-alignment, shifting the entire box within its container, which may affect the sizing of its container. See §5.3 Baseline Self-Alignment.

If both baseline content-alignment and baseline self-alignment are specified in the same axis on the same box, only baseline self-alignment is honored in that axis; the content-alignment in that axis must be treated as start.

Add example images here.

Note: The vertical-align property will also need a first-baseline value because per CSS2.1 for inline blocks, baseline is equivalent to last-baseline.

3.3. Distributed Alignment: the stretch, space-between, space-around, and space-evenly keywords

The distribution values are used by justify-content and align-content to distribute the alignment subjects evenly between the start and end edges of the alignment container. When the alignment subjects 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
space-between
The alignment subjects are evenly distributed in the alignment container. The first alignment subject is placed flush with the start edge of the alignment container, the last alignment subject is placed flush with the end edge of the alignment container, and the remaining alignment subjects are distributed so that the spacing between any two adjacent alignment subjects is the same. Unless otherwise specified, this value falls back to start.
space-around
The alignment subjects are evenly distributed in the alignment container, with a half-size space on either end. The alignment subjects are distributed so that the spacing between any two adjacent alignment subjects is the same, and the spacing before the first and after the last alignment subject is half the size of the other spacing. Unless otherwise specified, this value falls back to center.
space-evenly
The alignment subjects are evenly distributed in the alignment container, with a full-size space on either end. The alignment subjects are distributed so that the spacing between any two adjacent alignment subjects, before the first alignment subject, and after the last alignment subject is the same. Unless otherwise specified, this value falls back to center.
stretch
If the combined size of the alignment subjects is less than the size of the alignment container, any auto-sized alignment subjects have their size increased equally (not proportionally), while still respecting the constraints imposed by max-height/max-width (or equivalent functionality), so that the combined size exactly fills the alignment container.

Unless otherwise specified, this value falls back to flex-start. (For layout modes other than flex layout, flex-start is identical to start.)

Add even more example images.

3.4. Overflow Alignment: the safe and unsafe keywords

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. “Unsafe” 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.

<overflow-position> = unsafe | safe
safe
If the size of the alignment subject overflows the alignment container, the alignment subject is instead aligned as if the alignment mode were start.
unsafe
Regardless of the relative sizes of the alignment subject and alignment container, the given alignment value is honored.

If the overflow alignment isn’t explicitly specified, the default overflow alignment is similar to unsafe in that an overflowing alignment subject is allowed to overflow its alignment container; however if the this would cause it to also overflow the scrollable overflow region of its nearest ancestor scroll container, then its overflow in that direction is limited by biasing any remaining overflow to the end side. In other words, it is similar to safe alignment except that the limit for switching to start is triggered by overflowing the scrollable area, rather than merely overflowing the alignment container.

The figure below illustrates the difference in "safe" versus "unsafe" centering, using a column flexbox as an example:
About
Authoritarianism
Blog
About
Authoritarianism
Blog
The items in the figure on the left are set to align-self: safe center;, while those in the figure on the right are set to align-self: unsafe center;. If this column flex container was placed against the left edge of the page, the "safe" behavior would be more desirable, as the long item would be fully readable. In other circumstances, the "unsafe" centering behavior might be better, as it correctly centers all the items.

It may not be Web-compatible to implement the “smart” default behavior (though we hope so, and believe it to be likely), so UAs should pass any feedback on this point to the WG. UAs that have not implemented the “smart” default behavior must behave as unsafe.

4. Content Distribution: the justify-content and align-content properties

The content distribution properties justify-content and align-content control alignment of the box’s content within its content box.

Diagram showing that the alignment of the content within the element is affected.
Name: justify-content, align-content
Value: normal | <baseline-position> | <content-distribution> || [ <overflow-position>? && <content-position> ]
Initial: normal
Applies to: block containers, flex containers, and grid containers
Inherited: no
Percentages: n/a
Media: visual
Computed value: specified value
Canonical order: per grammar
Animatable: no

Aligns the contents of the box as a whole. Values other than normal are defined in §3 Alignment Values, above. If both a <content-distribution> and <content-position> are given, the <content-position> provides an explicit fallback alignment.

4.1. Details per Layout Mode

4.1.1. Block Containers

Alignment Container The block container’s content box.
Alignment Subject(s) The entire contents of the block, as a unit.
align-content Axis The block axis. If a <content-distribution> is specified the fallback alignment is used instead.
justify-content Axis Does not apply to and has no effect on block containers.
normal Behavior All values other than normal force the block container to establish a new formatting context.

For table cells, the behavior of the normal 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. [CSS21]

normal otherwise behaves as start.

4.1.2. Multicol Containers

Alignment Container The multi-column element’s content box.
Alignment Subject(s) The column boxes.
align-content Axis The block axis. If a <content-distribution> is specified the fallback alignment is used instead.
justify-content Axis Does not apply to and has no effect on multi-column elements.
normal Behavior normal behaves as stretch; both are defined as described in the column-sizing rules of [CSS3-MULTICOL].

4.1.3. Flex Containers

Alignment Container The flex container’s content box.
Alignment Subject(s) For justify-content, the flex items in each flex line.

For align-content, the flex lines.

align-content Axis The cross axis.
justify-content Axis The justify-content property applies along the main axis, but since flexing in the main axis is controlled by flex, stretch behaves as flex-start (ignoring the specified fallback alignment, if any).
normal Behavior normal behaves as stretch.

See [CSS-FLEXBOX-1] for details.

4.1.4. Grid Containers

Alignment Container The grid container’s content box.
Alignment Subject(s) The grid tracks.
align-content Axis The block (column) axis, aligning the grid rows.
justify-content Axis The inline (row) axis, aligning the grid columns.
normal Behavior normal behaves as stretch.

See [CSS-GRID-1] for details.

4.2. Baseline Content-Alignment

The content of boxes participating in row-like layout contexts (shared alignment contexts) can be baseline-aligned to each other. This effectively increases the padding on the box to align the alignment baseline of its contents with that of other baseline-aligned boxes in its group.

The set of boxes that participate in baseline content-alignment depends on the layout model:

Table Cells:
A table cell participates in first (last) baseline content-alignment in either its row or column (whichever matches the axises of its natural baselines) if its computed align-content is baseline (last-baseline).
Flex Items:
A flex item participates in first (last) baseline content-alignment in its flex line if its computed align-content is baseline (last-baseline), its inline axis is parallel to the main axis, and its computed align-self is stretch or self-start (self-end). For this purpose, the start, end, flex-start, and flex-end values of align-self are treated as either self-start or self-end, whichever they end up equivalent to.
Grid Items:
A grid item participates in first (last) baseline content-alignment in either its row or column (whichever matches the axises of its natural baselines) if its computed align-content is baseline (last-baseline), and its computed align-self or justify-self (whichever affects its block axis) is stretch or self-start (self-end). For this purpose, the start, end, flex-start, and flex-end values of align-self are treated as either self-start or self-end, whichever they end up equivalent to.

If a box spans multiple shared alignment contexts, it participates in first (last) baseline content-alignment within its start-most (end-most) shared alignment context along that axis.

When a box participates in first (last) baseline content-alignment it aligns its contents as follows: its alignment subject (i.e. content) is aligned to the start (end) edge of its alignment container (i.e. itself) and the minimum necessary extra space is added between its start (end) content edge and its alignment subject’s edge to align its alignment baseline in that axis with those of its baseline-sharing group. See §7.3 Aligning Boxes by Baseline. This increases the intrinsic size of the box.

Note: In other words, a box participating in baseline content-alignment acts like its padding was increased so that its alignment baseline lines up with the alignment baselines of its participating siblings. The box itself is aligned as usual for its align-self/justify-self value.

4.3. Overflow and Scroll Positions

The content distribution properties also affect the initial scroll position, setting it to display the appropriate portion of the scrollable area. In other words, the scrollable overflow region is aligned relative to the viewport as specified by the content distribution property.

For example, if a scrollable flex container is set to justify-content: flex-end (or justify-content: flex-start with flex-flow: row-reverse), it will be initially displayed scrolled all the way to the main-end edge of the scrollable area, and its content will overflow its main-start edge.

This needs to be integrated with overflow-anchor, when the property exists, so that you get the same behavior whether an elements *starts out* overflowing, or is filled element-by-element.

5. Self-Alignment: Aligning the Box within its Parent

The justify-self and align-self properties control alignment of the box within its containing block.

Diagram showing that the alignment of the element within its containing block is affected.

5.1. Inline/Main-Axis Alignment: the justify-self property

Name: justify-self
Value: auto | normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ]
Initial: auto
Applies to: block-level boxes, absolutely-positioned boxes, and grid items
Inherited: no
Percentages: n/a
Media: visual
Computed value: specified value, except for auto (see prose)
Canonical order: per grammar
Animatable: no

Justifies the box within its containing block 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 the computed value of justify-items on the parent (minus any legacy keywords), or normal if the box has no parent.

The normal keyword represents the “default” alignment for the layout mode. Its behavior depends on the layout mode, as described below.

When the box’s computed width/height (as appropriate to the axis) is auto and neither of its margins (in the appropriate axis) are auto, the stretch keyword sets the box’s used size to the length necessary to make its outer size as close to filling the alignment container as possible while still respecting the constraints imposed by min-height/min-width/max-height/max-width. Unless otherwise specified, this value falls back to flex-start.

Should stretch allow a fallback alignment, like it does on align-content and justify-content?

Note: The stretch keyword can cause elements to shrink, to fit their container.

All other values are as defined in §3 Alignment Values, above.

Note: auto margins, because they effectively adjust the size of the margin area, take precedence over justify-self.

5.1.1. Block-Level Boxes

justify-self Axis The block’s containing block’s inline axis.
Alignment Container 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.

This is the legacy behavior of HTML align. Do we want to still do this, or should we do the centering behavior of margins, which center while ignoring floats, then shift if necessary to avoid overlapping?

Alignment Subject The block’s margin box.
normal Behavior Behaves as start.
Other Details 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.

This property does not apply to floats.

The effect of these rules is that an auto-sized block-level table, for example, can be aligned while still having side margins. If the table’s max-content size is narrower than its containing block, then it is shrink-wrapped to that size and aligned as specified. If the table’s max-content size is wider, then it fills its containing block, and the margins provide appropriate spacing from the containing block edges.

5.1.2. Absolutely-Positioned Boxes

justify-self Axis The block’s containing block’s inline axis.
Alignment Container The box’s containing block, as modified by the offset properties (top/right/bottom/left).
Alignment Subject The box’s margin box.
normal Behavior For consistency with CSS 2.1, the normal keyword behaves as start on replaced absolutely-positioned boxes, and behaves as stretch on all other absolutely-positioned boxes.

If the box has non-auto offsets in this axis, and either margin in this axis is auto, an auto width (height, for vertical writing modes) is treated as fill-available and auto margins are treated as zero. (Otherwise, when justify-content is not normal, width: auto (height: auto) is treated as fit-content and auto margins are used for alignment as in in-flow block-level layout.)

Other Details 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.

Values other than stretch cause non-replaced absolutely-positioned boxes to use fit-content sizing for calculating auto inline sizes.

Note that stretch does cause replaced absolutely-positioned elements to fill their containing block just as non-replaced ones do.

If either offset property in this dimension is auto, justify-self has no effect.

5.1.3. Static Position of Absolutely-Positioned Boxes

justify-self Axis The same axis that justify-self corresponds to for in-flow children of the element’s parent
Alignment Container The element’s static-position rectangle, as defined by its parent’s layout mode.
Alignment Subject The element’s margin box after laying out the element, treated as fixed-size for the purpose of alignment.
normal Behavior Behaves as (the fallback for) stretch (because it is treated as fixed-size).

5.1.4. Table Cells

This property does not apply to table cells, because their position and size is fully constrained by table layout.

5.1.5. Flex Items

This property does not apply to flex items, because there is more than one item in the main axis. See flex for stretching and justify-content for main-axis alignment. [CSS-FLEXBOX-1]

5.1.6. Grid Items

justify-self Axis The grid’s row axis.
Alignment Container The grid area.
Alignment Subject The grid item’s margin box.
normal Behavior Behaves as stretch.

5.2. Block/Cross-Axis Alignment: the align-self property

Name: align-self
Value: auto | normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ]
Initial: auto
Applies to: flex items, grid items, and absolutely-positioned boxes
Inherited: no
Percentages: n/a
Media: visual
Computed value: specified value, except for auto (see prose)
Canonical order: per grammar
Animatable: no

Aligns the box within its containing block 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 the computed value of align-items on the parent or normal if the box has no parent.

The normal keyword represents the “default” alignment for the layout mode.

The stretch keyword is as defined in §5.1 Inline/Main-Axis Alignment: the justify-self property.

All other values are as defined in §3 Alignment Values, above.

Note: auto margins, because they effectively adjust the size of the margin area, take precedence over justify-self.

5.2.1. Block-Level Boxes

The align-self property does not apply to block-level boxes (including floats), because there is more than one item in the block axis.

5.2.2. Absolutely-Positioned Boxes

align-self Axis The block’s containing block’s block axis.
Alignment Container The box’s containing block.
Alignment Subject The box’s margin box.
normal Behavior For consistency with CSS 2.1, the normal keyword behaves as start on replaced absolutely-positioned boxes, and behaves as stretch on all other absolutely-positioned boxes.

If the box has non-auto offsets in this axis, and either margin in this axis is auto, an auto height (width, for vertical writing modes) is treated as fill-available and auto margins are treated as zero. (Otherwise, when align-content is not normal, height: auto (width: auto) is treated as fit-content and auto margins are used for alignment as in in-flow block-level layout.)

Other Details 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.

Values other than stretch cause non-replaced absolutely-positioned boxes to use shrink-to-fit sizing for calculating auto block sizes.

Note that stretch does cause replaced absolutely-positioned elements to fill their containing block just as non-replaced ones do.

If either offset property in this dimension is auto, align-self has no effect.

5.2.3. Static Position of Absolutely-Positioned Boxes

align-self Axis The same axis that align-self corresponds to for in-flow children of the element’s parent
Alignment Container The element’s static-position rectangle, as defined by its parent’s layout mode.
Alignment Subject The element’s margin box after laying out the element, treated as fixed-size for the purpose of alignment.
normal Behavior Behaves as (the fallback for) stretch (because it is treated as fixed-size).

5.2.4. Table Cells

This property does not apply to table cells, because their position and size is fully constrained by table layout.

5.2.5. Flex Items

align-self Axis The flex container’s cross axis.
Alignment Container The flex line the flex item is in.
Alignment Subject The flex item’s margin box.
normal Behavior Behaves as stretch.

See [CSS-FLEXBOX-1] for details.

5.2.6. Grid Items

align-self Axis The grid’s column axis.
Alignment Container The grid area.
Alignment Subject The grid item’s margin box.
normal Behavior Behaves as stretch.

5.3. Baseline Self-Alignment

Boxes participating in row-like layout contexts (shared alignment contexts) can be baseline-aligned to each other. This effectively increases the margins on the box to align its alignment baseline with other baseline-aligned boxes in its group.

The set of boxes that participate in baseline self-alignment depends on the layout model:

Flex Items:
A flex item participates in first (last) baseline self-alignment in its flex line if its inline axis is parallel to the main axis and its computed align-self is baseline (last-baseline). See [CSS-FLEXBOX-1] for details.
Grid Items:
A grid item participates in first (last) baseline self-alignment in either its row or column (whichever axises it has a natural baseline in) if its justify-self or align-self property (whichever affects its block axis) computes to baseline (last-baseline).

If a box spans multiple shared alignment contexts, it participates in first (last) baseline self-alignment within its start-most (end-most) shared alignment context along that axis.

When a box participates in first (last) baseline self-alignment, it is aligned as follows: the minimum necessary extra space is added between its start (end) edge and the alignment container edge to match its alignment baseline in that axis up to that of its baseline-sharing group. See §7.3 Aligning Boxes by Baseline. This may increase the intrinsic size contribution of the alignment subject.

Note: In other words, a box participating in baseline self-alignment acts like its margin was increased so that its alignment baseline lines up with the alignment baselines of its participating siblings. The box’s contents are aligned as usual for its align-content/justify-content value.

6. Default Alignment

The align-items and justify-items properties set the default align-self and justify-self behavior of the items contained by the element.

Diagram showing that the alignment of grid items within the element is affected.

6.1. Inline/Main-Axis Alignment: the justify-items property

Name: justify-items
Value: auto | normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ] | [ legacy && [ left | right | center ] ]
Initial: auto
Applies to: block containers and grid containers
Inherited: no
Percentages: n/a
Media: visual
Computed value: specified value, except for auto (see prose)
Canonical order: per grammar
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:

auto
If the inherited value of justify-items includes the legacy keyword, auto computes to the inherited value.

Otherwise, auto computes to normal.

legacy
This keyword causes the value to effectively inherit into descendants. It can only be combined with the center, left, and right positions. When justify-self:auto retrieves the value of justify-items, only the alignment keyword, not the legacy keyword, is passed to it. It exists to implement the legacy alignment behavior of HTML’s <center> element and align attribute.

Other values have no special handling and are merely passed to justify-self.

The auto keyword exists solely to implement the legacy inheritance property. We could just rename it to legacy, since it computes away to normal unless you’re using legacy anyway.

The legacy keyword acts weird, to make it behave like an inherited value even though this property is not inherited. We don’t mix inheritance and non-inheritance anywhere else, because it’s a bad code smell. Should we remove legacy and make a separate inheriting property for it? Or just drop the behavior entirely and let it remain special HTML magic?

6.2. Block/Cross-Axis Alignment: the align-items property

Name: align-items
Value: normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ]
Initial: normal
Applies to: block-level elements
Inherited: no
Percentages: n/a
Media: visual
Computed value: specified value
Canonical order: per grammar
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:

Values have no special handling and are merely passed to align-self.

7. Baseline Alignment Details

7.1. Determining the Baselines of a Box

The first baseline set (and last baseline set) of a box for a given axis are a set of baselines (alphabetic, central, etc.) nominally associated with the first (last) line of text within the box. The alignment baseline is one of these, usually the dominant baseline of the alignment container. (See alignment-baseline.) Note that boxes might not have baselines in a particular axis.

The first and last baseline sets of a box are determined differently based on the layout model, as follows:

block containers
The first (last) inline-axis baselines of a block container are generated from the dominant first (last) baseline of the first (last) in-flow line box in the block container, or are taken from the first (last) in-flow block-level child in the block container that contributes a set of first (last) baselines, whichever comes first (last). If there is no such line box or child, then the block container has no baselines. For the purposes of finding the baselines, in-flow boxes with a scrolling mechanisms (see the overflow property) must be considered as if scrolled to their origin (final) position.

A block container has no block-axis baselines.

tables
The first (last) inline-axis baselines of a table box are the first (last) baselines of its first (last) row.

When finding the baselines of an inline-block, any baselines contributed by table boxes must be skipped. (This quirk is a legacy behavior from [CSS21].)

The first (last) block-axis baselines of a table box are the first (last) baselines of its first (last) column.

table rows
If any cells in the row participate in baseline (last-baseline) alignment along the row axis, the first (last) inline-axis baselines of the row are generated from their shared alignment baseline and the row’s first available font, after alignment has been performed. Otherwise, the first (last) inline-axis baselines of the row are synthesized from the lowest and highest content edges of the cells in the row. [CSS21]

A table row has no block-axis baselines.

table columns
If any cells in the column participate in baseline (last-baseline) alignment along the column axis (by having a writing mode perpendicular to that of the table), the first (last) block-axis baselines of the column are generated from their shared alignment baseline and the column’s first available font, after alignment has been performed. Otherwise, the first (last) inline-axis baselines of the row are synthesized from the extreme content edges of the cells in the row. [CSS21]

A table column has no inline-axis baselines.

flex containers
See Flex Baselines in [CSS-FLEXBOX-1].
grid containers
See Grid Baselines in [CSS-GRID-1].

To generate baselines for a box from a single baseline, use the baseline table from the font settings and first available font of that box, and align that baseline set to the given single baseline.

To synthesize baselines from a rectangle (or two parallel lines), synthesize the alphabetic baseline from the lower line, and the central baseline by averaging the positions of the two edges or lines.

Note: The forthcoming Inline Layout Module will define synthesis rules for baselines other than alphabetic and central.

A box has a natural baseline if it derives its baseline set(s) from a font’s baseline table (whether or not the UA had to synthesize that baseline table, e.g. due to it being missing from the font). It has a synthesized baseline if its baselines are synthesized from the geometry of its box, as described above.

Maybe these things are wrong? CSS 2.1 is really weird about baseline alignment.

7.2. Baseline Alignment Terminology

A baseline-sharing group is composed of boxes that participate in baseline alignment together. This is possible only if they

Boxes share an alignment context along a particular axis when they are:

Note: The inline-level boxes on a line box also share an alignment context and participate in a baseline-sharing group; however this is detailed in the CSS Inline Layout module, not here.

Boxes in a baseline-sharing group are aligned to each other using their alignment baseline. This is the dominant baseline associated with the box that generates their shared alignment context on the first (last, for last-baseline alignment) formatted line. For example, in horizontal writing modes, specifying align-content: baseline on table cells in the same row will align the alphabetic baselines of their first formatted lines.

Note: The alignment-baseline property introduced in CSS Inline Layout Level 3 will allow changing which baseline is used for baseline alignment within a baseline-sharing group.

7.3. Aligning Boxes by Baseline

Given a set of boxes and their baselines that all belong to a single baseline-sharing group, the boxes are baseline-aligned as follows:

First, generate the alignment context’s baseline table from its first available font and overlay also the mirror of this baseline table by aligning their central baselines. These are the baseline grids to which the boxes will align.

Next, align all boxes by their specified alignment baseline to the alignment context’s baseline table or its mirror, whichever matches its line orientation. Unless otherwise specified (e.g. via the alignment-baseline property), the alignment baseline is the dominant baseline of the alignment context.

Position this aligned subtree within the alignment container as specified by the relevant keyword (baseline or last-baseline).

8. Changes

Changes since the previous Working Draft include:

9. Privacy and Security Considerations

As a simple layout spec, this introduces no new privacy or security considerations.

Acknowledgments

Special thanks goes to Javier Fernandez, Markus Mielke, Alex Mogilevsky, and the participants in the CSSWG’s March 2008 F2F alignment discussions.

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.

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.

Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

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 property 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.

Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

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-CASCADE-3]
Elika Etemad; Tab Atkins Jr.. CSS Cascading and Inheritance Level 3. 19 May 2016. CR. URL: https://drafts.csswg.org/css-cascade-3/
[CSS-CASCADE-4]
Elika Etemad; Tab Atkins Jr.. CSS Cascading and Inheritance Level 4. 14 January 2016. CR. URL: http://dev.w3.org/csswg/css-cascade/
[CSS-DISPLAY-3]
Tab Atkins Jr.; Elika Etemad. CSS Display Module Level 3. 15 October 2015. WD. URL: http://dev.w3.org/csswg/css-display/
[CSS-FLEXBOX-1]
Tab Atkins Jr.; Elika Etemad; Rossen Atanassov. CSS Flexible Box Layout Module Level 1. 26 May 2016. CR. URL: https://drafts.csswg.org/css-flexbox/
[CSS-FONTS-3]
John Daggett. CSS Fonts Module Level 3. 3 October 2013. CR. URL: http://dev.w3.org/csswg/css-fonts/
[CSS-GRID-1]
Tab Atkins Jr.; Elika Etemad; Rossen Atanassov. CSS Grid Layout Module Level 1. 19 May 2016. WD. URL: https://drafts.csswg.org/css-grid/
[CSS-MULTICOL-1]
CSS Multi-column Layout Module Level 1 URL: https://drafts.csswg.org/css-multicol-1/
[CSS-OVERFLOW-3]
David Baron; Florian Rivoal. CSS Overflow Module Level 3. 31 May 2016. WD. URL: http://drafts.csswg.org/css-overflow/
[CSS-OVERFLOW-4]
CSS Overflow Module Level 4 URL: https://drafts.csswg.org/css-overflow-4/
[CSS-POSITION-3]
Rossen Atanassov; Arron Eicholz. CSS Positioned Layout Module Level 3. 17 May 2016. WD. URL: https://drafts.csswg.org/css-position/
[CSS-SIZING-4]
CSS Intrinsic & Extrinsic Sizing Module Level 4 URL: https://drafts.csswg.org/css-sizing-4/
[CSS-TEXT-3]
Elika Etemad; Koji Ishii. CSS Text Module Level 3. 10 October 2013. LCWD. URL: http://dev.w3.org/csswg/css-text-3/
[CSS-WRITING-MODES-3]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 3. 15 December 2015. CR. URL: http://dev.w3.org/csswg/css-writing-modes-3/
[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. REC. URL: https://www.w3.org/TR/CSS2
[CSS3-MULTICOL]
Håkon Wium Lie. CSS Multi-column Layout Module. 12 April 2011. CR. URL: https://www.w3.org/TR/css3-multicol
[CSS3VAL]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 11 June 2015. CR. URL: http://dev.w3.org/csswg/css-values/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[SELECTORS-3]
Selectors Level 3 URL: https://drafts.csswg.org/selectors-3/
[SVG]
Jon Ferraiolo. Scalable Vector Graphics (SVG) 1.0 Specification. 4 September 2001. REC. URL: https://www.w3.org/TR/SVG/

Informative References

[CSS-INLINE-3]
Dave Cramer; Elika Etemad; Steve Zilles. CSS Inline Layout Module Level 3. 24 May 2016. WD. URL: https://drafts.csswg.org/css-inline/

Property Index

Name Value Initial Applies to Inh. %ages Media Ani­mat­able Canonical order Com­puted value
justify-content normal | <baseline-position> | <content-distribution> || [ <overflow-position>? && <content-position> ] normal block containers, flex containers, and grid containers no n/a visual no per grammar specified value
align-content normal | <baseline-position> | <content-distribution> || [ <overflow-position>? && <content-position> ] normal block containers, flex containers, and grid containers no n/a visual no per grammar specified value
justify-self auto | normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ] auto block-level boxes, absolutely-positioned boxes, and grid items no n/a visual no per grammar specified value, except for auto (see prose)
align-self auto | normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ] auto flex items, grid items, and absolutely-positioned boxes no n/a visual no per grammar specified value, except for auto (see prose)
justify-items auto | normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ] | [ legacy && [ left | right | center ] ] auto block containers and grid containers no n/a visual no per grammar specified value, except for auto (see prose)
align-items normal | stretch | <baseline-position> | [ <overflow-position>? && <self-position> ] normal block-level elements no n/a visual no per grammar specified value

Issues Index

Add example images.
Make it easier to understand the dual-axis nature of "start" and "end" wrt orthogonal flows.
Should this end fallback be safe or unsafe?
Add example images here.
Add even more example images.
It may not be Web-compatible to implement the “smart” default behavior (though we hope so, and believe it to be likely), so UAs should pass any feedback on this point to the WG. UAs that have not implemented the “smart” default behavior must behave as unsafe.
This needs to be integrated with overflow-anchor, when the property exists, so that you get the same behavior whether an elements *starts out* overflowing, or is filled element-by-element.
Should stretch allow a fallback alignment, like it does on align-content and justify-content?
This is the legacy behavior of HTML align. Do we want to still do this, or should we do the centering behavior of margins, which center while ignoring floats, then shift if necessary to avoid overlapping?
The auto keyword exists solely to implement the legacy inheritance property. We could just rename it to legacy, since it computes away to normal unless you’re using legacy anyway.
The legacy keyword acts weird, to make it behave like an inherited value even though this property is not inherited. We don’t mix inheritance and non-inheritance anywhere else, because it’s a bad code smell. Should we remove legacy and make a separate inheriting property for it? Or just drop the behavior entirely and let it remain special HTML magic?
Maybe these things are wrong? CSS 2.1 is really weird about baseline alignment.