CSS Intrinsic & Extrinsic Sizing Module Level 3

W3C Working Draft,

This version:
https://www.w3.org/TR/2017/WD-css-sizing-3-20170207/
Latest published version:
https://www.w3.org/TR/css-sizing-3/
Editor's Draft:
https://drafts.csswg.org/css-sizing-3/
Previous Versions:
https://www.w3.org/TR/2016/WD-css-sizing-3-20160512/
Issue Tracking:
Inline In Spec
GitHub Issues
Editors:
Tab Atkins (Google)
Elika J. Etemad / fantasai (Invited Expert)

Abstract

This module extends the CSS sizing properties with keywords that represent content-based "intrinsic" sizes and context-based "extrinsic" sizes, allowing CSS to more easily describe boxes that fit their content or fit into a particular layout context.

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-sizing” in the title, preferably like this: “[css-sizing] …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.

1. Introduction

This section is not normative.

CSS layout has several different concepts of automatic sizing that are used in various layout calculations. This section defines some more precise terminology to help connect the layout behaviors of this spec to the calculations used in other modules, and some new keywords for the width and height properties to allow authors to assign elements the dimensions resulting from these size calculations.

1.1. Module interactions

This module extends the width, height, min-width, min-height, max-width, max-height, and column-width features defined in [CSS21] chapter 10 and in [CSS3COL]

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 [CSS3COLOR], when combined with this module, expands the definition of the <color> value type as used in this specification.

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

size
A one- or two-dimensional measurement: a block size and/or inline size; alternatively a width and/or height.
inner size
The content-box size of a box.
outer size
The margin-box size of a box.
definite size
A size that can be determined without measuring content; that is, a <length>, a measure of text (without consideration of line-wrapping), a size of the initial containing block, or a <percentage> or other formula (such the “stretch-fit” sizing of non-replaced blocks [CSS21]) that is resolved solely against definite sizes.

Additionally, the size of the containing block of an absolutely positioned element is always definite with respect to that element.

indefinite size
A size that is not definite. Indefinite available space is essentially infinite.
available space
A size representing the space into which a box is laid out, as determined by the rules of the formatting context in which it participates. The space available to a box is usually either a measurement of its containing block (if that is definite) or an infinite size (when it is indefinite). Available space can alternatively be either a min-content constraint or a max-content constraint.
stretch fit
The stretch fit into a given size is that size, minus the element’s computed margins (not collapsed, treating auto as zero), border, and padding in the given dimension (such that the outer size is a perfect fit), and flooring at zero (so that the inner size is not negative).

Note: This is the formula used to calculate the auto widths of non-replaced blocks in normal flow in CSS2.1§10.3.3.

fallback size
Some sizing algorithms do not work well with an infinite size. In these cases, the fallback size is used instead. Unless otherwise specified, this is the size of the initial containing block.

2.1. Auto Box Sizes

stretch-fit size

stretch-fit inline size

stretch-fit block size

Roughly, the size a box would take if it filled its available space in the given axis. (See §5 Extrinsic Size Determination.)

Note: For the inline axis, this is called the “available width” in CSS2.1§10.3.5 and computed by the rules in CSS2.1§10.3.3.

max-content size

A box’s “ideal” size in a given axis when given infinite available space. Usually this is the smallest size the box could take in that axis while still fitting around its contents, i.e. minimizing unfilled space while avoiding overflow.

max-content inline size

The box’s “ideal” size in the inline axis. Usually the narrowest inline size it could take while fitting around its contents if none of the soft wrap opportunities within the box were taken. (See §4 Intrinsic Size Determination.)

Note: This is called the “preferred width” in CSS2.1§10.3.5 and the “maximum cell width” in CSS2.1§17.5.2.2.

max-content block size

The box’s “ideal” size in the block axis. Usually the block size of the content after layout.

min-content size

The smallest size a box could take that doesn’t lead to overflow that could be avoided by choosing a larger size. (See §4 Intrinsic Size Determination.)

min-content inline size

The narrowest inline size a box could take that doesn’t lead to inline-dimension overflow that could be avoided by choosing a larger inline size. Roughly, the inline size that would fit around its contents if all soft wrap opportunities within the box were taken.

Note: This is called the “preferred minimum width” in CSS2.1§10.3.5 and the “minimum content width” in CSS2.1§17.5.2.2.

min-content block size

Equivalent to the max-content block size.

Or should this be the minimum between allowed break points? It might make sense in multi-col contexts to have min-content and max-content block-sizes be different, even if they are the same elsewhere.

fit-content size

fit-content inline size

fit-content block size

If the available space in a given axis is finite, equal to min(max-content size, max(min-content size, stretch-fit size)). Otherwise, equal to the max-content size in that axis.

Note: This is called the “shrink-to-fit” width in CSS2.1§10.3.5 and CSS Multi-column Layout § 3.4.

2.2. Intrinsic Size Contributions

max-content contribution
The size that a box contributes to its containing block’s max-content size.
min-content contribution
The size that a box contributes to its containing block’s min-content size.

Intrinsic size contributions are based on the outer size of the box; for this purpose auto margins are treated as zero.

2.3. Intrinsic Size Constraints

max-content constraint
A sizing constraint imposed by the box’s containing block that causes it to produce its max-content contribution.
min-content constraint
A sizing constraint imposed by the box’s containing block that causes it to produce its min-content contribution.

3. New Sizing Keywords

3.1. New Keywords for width and height

Name: width, min-width, max-width, height, min-height, max-height
New values: stretch | max-content | min-content | fit-content | fit-content(<length-percentage>)

There are four types of automatically-determined sizes in CSS (which are represented in the width and height properties by the keywords defined above):

stretch
Use the stretch-fit inline size or stretch-fit block size, as appropriate to the writing mode.

NOTE: This is the formula used to calculate auto widths for non-replaced blocks in normal flow, see CSS2.1§10.3.3. It allows re-using this formula for boxes that are otherwise shrink-wrapped, like tables.

max-content
If specified for the inline axis, use the max-content inline size; otherwise compute to auto.
min-content
If specified for the inline axis, use the min-content inline size; otherwise compute to auto.
fit-content
If specified for the inline axis, use the max-content inline size i.e. min(max-content size, max(min-content size, stretch-fit size)); otherwise compute to auto.
fit-content(<length-percentage>)
If specified for the inline axis, use the fit-content formula with the available space replaced by the specified argument, i.e. min(max-content size, max(min-content size, <length-percentage>)); otherwise compute to auto.

Is stretch stable enough or should we defer to L4?

Note: To size an element such that it avoids overlapping sibling floats, make sure it’s a formatting context root. For some layout modes, such as Grid and Flexbox, this is true automatically. For Block layout, this means using display: flow-root;.

Note that percentages resolved against the intrinsic sizes (max-content, min-content, fit-content) will compute to auto, as defined by CSS 2.1. [CSS21]

3.2. Containing Floats

Note: To ensure that a container sizes itself to contain any descendant floats, make sure it’s a formatting context. For some layout modes, such as Grid and Flexbox, this is true automatically. For Block layout, this means using display: flow-root;.

3.3. Column Sizing Keywords

Name: column-width
New values: stretch | max-content | min-content | fit-content | fit-content(<length-percentage>)

When used as values for column-width, the new keywords specify the optimal column width:

stretch
Specifies the optimal column width as the stretch-fit inline size of the multi-column element.
max-content
Specifies the optimal column width as the max-content inline size of the multi-column element’s contents.
min-content
Specifies the optimal column width as the min-content inline size of the multi-column element’s contents.
fit-content
Specifies the optimal column width as min(max-content inline size, max(min-content inline size, stretch-fit inline size)).
fit-content(<length-percentage>)
Specifies the optimal column width as min(max-content size, max(min-content size, <length-percentage>))

4. Intrinsic Size Determination

Intrinsic sizing determines sizes based on the contents of an element, without regard for its context.

4.1. Intrinsic Sizes

The min-content size of a box in each axis is the size it would have as an auto-sized (in that axis) float in a zero-sized containing block. (In other words, the minimum size it has when sized as “shrink-to-fit”.)

The max-content size of a box in each axis is the size it would have as an auto-sized (in that axis) float in an infinitely-sized containing block. (In other words, the maximum size it has when sized as “shrink-to-fit”.)

Note: This specification does not define how to determine these sizes. Please refer to [CSS21], the relevant CSS specification for that display type, and/or existing implementations for further details.

Intrinsic sizes of images without an intrinsic size are weird and undefined, and should probably get some love

4.2. Intrinsic Contributions

A box’s min-content contribution/max-content contribution in each axis is the size of the content box of a hypothetical auto-sized float that contains only that box, if that hypothetical float’s containing block is zero-sized/infinitely-sized.

Note: This specification does not define how to determine these sizes. Please refer to [CSS21], the relevant CSS specification for that display type, and/or existing implementations for further details.

5. Extrinsic Size Determination

Extrinsic sizing determines sizes based on the context of an element, without regard for its contents.

5.1. Stretch-fit Sizing

The inner stretch-fit inline size of a box is…

…less the box’s inline-axis margins (after any margin collapsing, and treating auto margins as zero), borders, and padding, flooring at zero.

Shouldn’t this be ignoring margin collapsing?

The stretch-fit block size of a box is defined analogously, but in the other dimension.

This definition might end up skipping further up the ancestor chain than we’d like in some cases. Example. Maybe it should stop at each formatting root, or something similar?

5.2. Percentage Sizing

Percentages specify sizing of a box with respect to the box’s containing block.

Although this may require an additional layout pass to re-resolve percentages in some cases, the auto, min-content, max-content, and fit-content values of min-width and min-height do not prevent the resolution of percentage sizes of the box’s contents. However, in order to prevent cyclic sizing in the general case, percentages do not otherwise resolve against indefinite sizes, and instead are treated as auto.

Note: See definition of percentage width and height in [CSS21].

Changes

Changes since the 12 May 2016 Working Draft:

Acknowledgments

Special thanks go to Aaron Gustafson, L. David Baron for their contributions to this module.

Privacy and Security Considerations

This specification introduces no new privacy or security considerations.

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 https://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-DISPLAY-3]
Tab Atkins Jr.; Elika Etemad. CSS Display Module Level 3. 15 October 2015. WD. URL: https://www.w3.org/TR/css-display-3/
[CSS-FLEXBOX-1]
Tab Atkins Jr.; Elika Etemad; Rossen Atanassov. CSS Flexible Box Layout Module Level 1. 26 May 2016. CR. URL: https://www.w3.org/TR/css-flexbox-1/
[CSS-MULTICOL-1]
CSS Multi-column Layout Module Level 1 URL: https://www.w3.org/TR/css3-multicol/
[CSS-VALUES-3]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 29 September 2016. CR. URL: https://www.w3.org/TR/css-values-3/
[CSS-WRITING-MODES-4]
CSS Writing Modes Module Level 4 URL: https://www.w3.org/TR/css-writing-modes-4/
[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
[CSS22]
Bert Bos. Cascading Style Sheets Level 2 Revision 2 (CSS 2.2) Specification. 12 April 2016. WD. URL: https://www.w3.org/TR/CSS22/
[CSS3COL]
Håkon Wium Lie. CSS Multi-column Layout Module. 12 April 2011. CR. URL: https://www.w3.org/TR/css3-multicol
[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

Informative References

[CSS3COLOR]
Tantek Çelik; Chris Lilley; David Baron. CSS Color Module Level 3. 7 June 2011. REC. URL: https://www.w3.org/TR/css3-color

Property Index

No properties defined.

Issues Index

Or should this be the minimum between allowed break points? It might make sense in multi-col contexts to have min-content and max-content block-sizes be different, even if they are the same elsewhere.
Is stretch stable enough or should we defer to L4?
Intrinsic sizes of images without an intrinsic size are weird and undefined, and should probably get some love
Shouldn’t this be ignoring margin collapsing?
This definition might end up skipping further up the ancestor chain than we’d like in some cases. Example. Maybe it should stop at each formatting root, or something similar?