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 section describes the status of this document at the time of
its publication. Other documents may supersede this document. A list of
current W3C publications and the latest revision of this technical report
can be found in the W3C technical reports
index at https://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C
Membership. This is a draft document and may be updated, replaced or
obsoleted by other documents at any time. It is inappropriate to cite this
document as other than 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.

“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 CSS 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.

1.1. Module interactions

This module adds some new alignment capabilities
to the block layout model described in [CSS2]chapters 9 and 10 and defines the interaction of these properties
with the alignment of table cell content using vertical-align,
as defined in [CSS2]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 [CSS2].
Value types not defined in this specification are defined in CSS Level 2 Revision 1 [CSS2].
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.

1.3. Partial Implementations

Since it is expected that support for the features in this module
will be deployed in stages corresponding to the various layout models affected,
it is hereby clarified that
the rules for partial implementations that require treating as invalid any unsupported feature
apply to any alignment keyword
which is not supported across all layout modules to which it applies
for layout models in which the implementation supports the property in general.

For example,
if an implementation supports align-self in [CSS-GRID-1] and [CSS-FLEXBOX-1],
then it must treat start as invalid
unless it is supported in both grid and flex containers.
However if that same implementation does not support align-self for block-level elements at all,
then a lack of implementation of align-self: start does not trigger this requirement to treat it as invalid.

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:

whether they control the position of the box within its parent, or the box’s content within itself.

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
(including CSS Flexbox 1 §2 Flex Layout Box Model and Terminology)

Some alignments can only be fulfilled in certain situations
or are limited in how much space they can consume;
for example, space-between can only operate when there is more than one alignment subject,
and baseline alignment, once fulfilled, might not be enough to absorb all the excess space.
In these cases a fallback alignment takes effect
(as defined below)
to fully consume the excess space.

4. Alignment Keywords

All of the alignment properties use a common set of keyword values,
which are defined in this section.
Keywords fall into three categories:

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.

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.

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. [CSS2]

Behaves as normal if the box has no parent,
or when determining the actual position of an absolutely positioned box.
It behaves as the computed justify-items value of the parent box
(minus any legacy keywords)
otherwise
(including when determining the static position
of an absolutely positioned box).

normal

Represents the “default” alignment for the layout mode.
Its behavior depends on the layout mode, as described below.

stretch

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

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

6.1.1. Block-Level Boxes

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?

In terms of CSS2.1 block-level formatting [CSS2],
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 therefore 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.

In terms of CSS2.1 formatting [CSS2],
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.

Behaves as normal if the box has no parent,
or when determining the actual position of an absolutely positioned box.
It behaves as the computed align-items value of the parent box
(minus any legacy keywords)
otherwise
(including when determining the static position
of an absolutely positioned box).

normal

Represents the “default” alignment for the layout mode,
as defined below.

In terms of CSS2.1 formatting [CSS2],
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.

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 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?

A baseline set is
a set of baselines (alphabetic, central, etc.)
associated with a common baseline table.
Typically, a typesetting tradition will use only one of these,
but different writing systems use different baselines,
and mixing writing systems can result in using more than one
within a single line.
Refer to CSS Writing Modes 3 §4.1 Introduction to Baselines for more information on baselines and writing modes.

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

block containers

The first (last) baseline set of a block container
is generated from the dominant first (last) baseline
of the first (last) in-flow line box in the block container,
or is 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 baseline set.

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 line-under line,
and the central baseline by averaging the positions of the two edges or lines.
See CSS Inline Layout 3 § Synthesizing Baselines for rules on synthesizing additional baselines.

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

For the purposes of finding the baselines of a box,
it and all its in-flow descendants with a scrolling mechanism (see the overflow property)
must be considered as if scrolled to their origin.
Furthermore, if, in the case of a box with non-visible overflow,
the resulting position of a first (last) baseline
is past a box’s end (start) border edge,
its position is clamped to that border edge.

This reflects the latest CSS2.1 errata, however see also discussion of an alternate solution that was previously drafted here.

8.2. Baseline Alignment Terminology

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

Share an alignment context along an axis perpendicular to the axis they’re being baseline-aligned in.
(For example, grid items with align-self: baseline are baseline-aligning along the grid’s block axis,
and therefore participate with other items in their row.)

Boxes share an alignment context,
along a particular axis,
and established by a particular box,
when they are:

table cells in the same row, along the table’s row (inline) axis, established by the row box

table cells in the same column, along the table’s column (block) axis, established by the column box

grid items in the same row, along the grid’s row (inline) axis, established by the grid container

grid items in the same column, along the grid’s colum (block) axis, established by the grid container

flex items in the same flex line, along the flex container’s main axis, established by the flex container

Note: Conceptually,
the inline-level boxes in a line box also share a self-alignment context
and participate in a baseline-sharing group;
however they only baseline-align in response to the vertical-align property,
not any of the properties defined in this module.
See [CSS-INLINE-3].

Clamped baselines of scrollable boxes to the border edge, rather than margin edge.
(Issue 766)

10. 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:

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

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.

Issues Index

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 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 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? ↵