CSS Intrinsic & Extrinsic Sizing Module Level 4

Editor’s Draft,

Specification Metadata
This version:
https://drafts.csswg.org/css-sizing
Latest published version:
https://www.w3.org/TR/css-sizing/
Previous Versions:
Issue Tracking:
Inline In Spec
GitHub Issues
Editors:
Tab Atkins (Google)
Elika J. Etemad / fantasai (Invited Expert)
Jen Simmons (Mozilla)
Suggest an Edit for this Spec:
GitHub Editor
Not Ready For Implementation

This spec is not yet ready for implementation. It exists in this repository to record the ideas and promote discussion.

Before attempting to implement this spec, please contact the CSSWG at www-style@w3.org.


Abstract

This is a delta spec over CSS Sizing Level 3.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

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 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 February 2018 W3C Process Document.

1. Introduction

This is a diff spec from CSS Sizing Level 3. It is currently an Exploratory Working Draft: if you are implementing anything, please use Level 3 as a reference. We will merge the Level 3 text into this draft once it reaches CR.

1.1. Module interactions

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

1.2. Values

This specification follows the CSS property definition conventions from [CSS2]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Other CSS modules may expand the definitions of these value types.

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords keywords as their property value. For readability they have not been repeated explicitly.

2. Aspect Ratios

This section is a rough draft proposal. See discussion in Issue 333 and Issue 1173.

2.1. Intrinsic Aspect Ratios: the aspect-ratio property

Name: aspect-ratio
Value: auto | <ratio>
Initial: auto
Applies to: all elements except inline boxes and internal ruby or table boxes
Inherited: no
Percentages: n/a
Computed value: specified keyword or a pair of numbers
Canonical order: per grammar
Animation type: discrete

This property sets an intrinsic aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions. The box will essentially size the same as a replaced element with an intrinsic aspect ratio, see e.g. CSS2§10.

auto
Replaced elements with an intrinsic aspect ratio use that aspect ratio; otherwise the box has no aspect ratio.
<ratio>
The box’s aspect ratio is the specified ratio.

Note: If a box has both a specified width and a specified height, then the aspect ratio has no effect: at least one of these sizes must be auto for the aspect ratio to have an impact on sizing.

This example sets each item in the grid to render as a square, determining the number of items and their widths by the available space.
<ul>
  <li><li><li><li></ul>
ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(12em, 1fr));
}
li {
  aspect-ratio: 1/1;
  overflow: auto;
}
This example uses the iframe element’s width and height attributes to set the aspect-ratio property, giving the iframe an aspect ratio to use for sizing so that it behaves exactly like an image with that aspect ratio.
<iframe
  src="https://www.youtube.com/embed/0Gr1XSyxZy0"
  width=560
  height=315>
@supports (aspect-ratio: attr(width px) / 1) {
  iframe {
    aspect-ratio: attr(width px) / attr(height px);
    width: 100%;
    height: auto;
  }
}

Can we just slip this aspect-ratio rule into the UA default style sheet for images, so that they have an aspect ratio while they load? This would avoid the need for extra reflows after loading.

On non-replaced elements with an auto inline size (or any replaced element whose intrinsic block size depends on its inline size), the intrinsic block sizes of the box are assumed to be zero when applying min-block-size to the aspect ratio calculations, and assumed to be infinity when applying max-block-size to the aspect ratio calculations. These limits take effect as usual for non-replaced elements once the inline size is resolved.

For example, given a (horizontal writing mode) element with
width: auto;
min-height: min-content;
aspect-ratio: 1/1;

using the aspect ratio to resolve the width would require knowing the min-content height of the element.

Since for a non-replaced element, this value is not known until the width is resolved, we assume the min-height value is zero while resolving the width.

Then, once the width is known, we resolve the height, taking into account the min-content minimum.

2.2. Aspect Ratio Limits Option A: the from-ratio

Name: min-width, min-height, max-width, max-height
New values: from-ratio
Computed value: keyword as specified

The from-ratio keyword specifies that the used value of the property is calculated from the used size of the opposite dimension converted through the aspect ratio. If the box has no aspect ratio, then an aspect ratio of 1:1 is assumed.

Define a table of all the ways this creates conflicts and cycles and break them.

In the following example, the box is as wide as the container (as usual), and its height is as tall as needed to contain its content but at least as tall as it is wide.
div {
  aspect-ratio: 1/1;
  min-height: from-ratio;
  height: max-content;
}
+----------+  +----------+  +----------+
| ~~~~~~~~ |  | ~~~~~~~~ |  | ~~~~~~~~ |
| ~~~~~~~~ |  | ~~~~~~~~ |  | ~~~~~~~~ |
| ~~~~~~~  |  | ~~~~~~~~ |  | ~~~~~~~~ |
|          |  | ~~~      |  | ~~~~~~~~ |
+----------+  +----------+  | ~~~~~~~~ |
                            | ~~~~~~   |
                            +----------+

2.3. Aspect Ratio Limits Option B: the tr unit

Name: min-width, min-height, max-width, max-height
New values: <transfer-ratio>
Computed value: tr dimension value

The <transfer-ratio> value, which is a dimension with the unit tr, specifies that the used value of the property is calculated from the used size of the opposite dimension multiplied by the tr value.

Define a table of all the ways this creates conflicts and cycles and break them.

In the following example, the box is as wide as the container (as usual), and its height is as tall as needed to contain its content but at least as tall as it is wide.
div {
  min-height: 1tr;
}
+----------+  +----------+  +----------+
| ~~~~~~~~ |  | ~~~~~~~~ |  | ~~~~~~~~ |
| ~~~~~~~~ |  | ~~~~~~~~ |  | ~~~~~~~~ |
| ~~~~~~~  |  | ~~~~~~~~ |  | ~~~~~~~~ |
|          |  | ~~~      |  | ~~~~~~~~ |
+----------+  +----------+  | ~~~~~~~~ |
                            | ~~~~~~   |
                            +----------+

3. Intrinsic Size Determination

Need to Sync This Section With Level 3 It is Super Out Of Date

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

3.1. Intrinsic Sizes of Replaced Elements

For replaced elements, the min-content size and max-content size are equivalent and correspond to the appropriate dimension of the concrete object size returned by the default sizing algorithm [CSS3-IMAGES] of the element, calculated with an unconstrained specified size.

The min-content contribution and max-content contribution in each axis is the element’s specified outer size in that axis, if definite; otherwise, they are the min-content size, as specified above, plus the element’s margin/border/padding in that axis, clamped by the element’s min and max size properties in that axis.

3.2. Intrinsic Sizes of Non-Replaced Inlines

The min-content inline size of an inline box is the length of the largest unbreakable sequence of inline content. The min-content inline-size contribution of an inline box is its min-content inline size, plus any inline-axis margin, border, and padding adjacent to that sequence.

The max-content inline size of an inline box is the length of the largest sequence of inline content on a single line when only forced line breaks are taken. The max-content inline-size contribution of an inline box is its max-content inline size, plus any inline-axis margin, border, and padding adjacent to that sequence.

The min-content block size, max-content block size, min-content block-size contribution, and max-content block-size contribution of an inline box are the distance from the head edge of the first line box to the foot edge of the last line box on which the inline appears.

3.3. Intrinsic Sizes of Non-Replaced Blocks

The min-content inline size of a block container box is the largest min-content inline-size contribution of its in-flow or floated children.

The max-content inline size of a block container box is the inline-size of the box after layout, if all children are sized under a max-content constraint.

If the computed inline-size of a block-level box is min-content, max-content, or a definite size, its min-content inline-size contribution is that size plus any inline-axis margin, border, and padding. Otherwise, if the computed inline-size of the block is fit-content, auto, or stretch, its min-content inline-size contribution is its min-content inline size plus any inline-axis margin, border, and padding.

If the computed inline-size of a block-level box is min-content, max-content, or a definite size, its max-content inline-size contribution is that size plus any inline-axis margin, border, and padding. Otherwise, if the computed inline-size of the block is fit-content, auto, or stretch, its max-content inline-size contribution is its max-content inline size plus any inline-axis margin, border, and padding.

The min-content block size and max-content block size of a block container box is the content block-size as defined (for horizontal writing modes) in CSS2.1§10.6.3 and CSS2.1§17.5.3 for elements with height: auto, and analogously for vertical writing modes.

The min-content block-size contribution and max-content block-size contribution of a block-level box is the block-size of the block after layout, plus any block-axis margin, border, and padding.

Need to handle floats. See Greg’s issue and dbaron’s response.

3.4. Intrinsic Sizes in Table Layout

¯\_(ツ)_/¯

3.5. Intrinsic Sizes in Multi-column Layout

3.5.1. Min-content Sizes in Multi-column Layout

The min-content inline size of a multi-column container with a computed column-width not auto is the smaller of its column-width and the largest min-content inline-size contribution of its contents.

The min-content inline size of a multi-column container with a computed column-width of auto is the largest min-content inline-size contribution of its contents multiplied by its column-count (treating auto as 1), plus its column-gap multiplied by column-count minus 1.

3.5.2. Max-content Sizes in Unconstrained-height Multi-column Layout

The max-content inline size of a multi-column container with unrestrained column heights and a computed column-count not auto is its column-count multiplied by the larger of its column-width (treating auto as zero) and the largest min-content inline-size contribution of its contents, plus its column-gap multiplied by column-count minus 1.

Note that the contents of the multi-column container can still grow to be wider and shorter if the resulting column width is still smaller than the largest max-content inline-size contribution of its contents.

The max-content inline size of a multi-column container with unrestrained column heights and a computed column-count of auto is its column-width multiplied by the number of columns obtained by taking all allowed column breaks [CSS3-BREAK], plus its column-gap multiplied by that same number of columns minus 1.

3.5.3. Max-content Sizes in Constrained-height Multi-column Layout

The max-content inline size of a multi-column container with restrained-height columns (i.e. a specified height or max-height, or whichever properties map to the block size of the element) is the inline size that would exactly include all of its columns. It may be approximated by:

or by some more accurate method.

This approximation can result in some slack, but avoids overflow in the most common cases, where the balanced height of the columns above spanning elements are approximately equal.

In the common case of no column-spanning elements, this approximation collapses to simply doing a layout, and measuring the resulting columns.

4. Extrinsic Size Determination

4.1. Stretch-fit Sizing

4.2. Contain-fit Sizing: stretching while maintaining an aspect ratio

Contain-fit sizing essentially applies stretch-fit sizing, but reduces the size of the box in one axis to maintain the box’s intrinsic aspect ratio, similar to the contain keyword of the object-fit and background-size properties.

First, a target rectangle is determined:

  1. The initial target rectangle is the size of the box’s containing block, with any indefinite size assumed as infinity. If both dimensions are indefinite, the initial target rectangle is set to match the outer edges of the box were it stretch-fit sized.
  2. Next, if the box has a non-none max-width or max-height, the target rectangle is clamped in the affected dimension to less than or equal to the “maximum size” of the box’s margin box, i.e. the size its margin box would be if the box was sized at its max-width/height. (Note that, consistent with normal box-sizing rules, this “maximum size” is floored by the effects of the box’s min-width/min-height.)
  3. Last, the target rectangle is reduced in one dimension by the minimum necessary for it to match the box’s intrinsic aspect ratio.

The contain-fit size in each dimension is the size that would result from stretch-fitting into the target rectangle.

Copy whatever stretch-fit ends up doing wrt margin collapsing.

If there is a minimum size in one dimension that would cause overflow of the target rectangle if the aspect ratio were honored, do we honor the aspect ratio or skew the image? If the former, we need a step similar to #2 that applies the relevant minimums.

4.3. Percentage Sizing

Changes

Changes since the September 2012 Working Draft include:

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 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-BACKGROUNDS-3]
Bert Bos; Elika Etemad; Brad Kemper. CSS Backgrounds and Borders Module Level 3. 17 October 2017. CR. URL: https://www.w3.org/TR/css-backgrounds-3/
[CSS-DISPLAY-3]
Tab Atkins Jr.; Elika Etemad. CSS Display Module Level 3. 28 August 2018. CR. URL: https://www.w3.org/TR/css-display-3/
[CSS-IMAGES-4]
Tab Atkins Jr.; Elika Etemad; Lea Verou. CSS Image Values and Replaced Content Module Level 4. 13 April 2017. WD. URL: https://www.w3.org/TR/css-images-4/
[CSS-LOGICAL-1]
Rossen Atanassov; Elika Etemad. CSS Logical Properties and Values Level 1. 27 August 2018. WD. URL: https://www.w3.org/TR/css-logical-1/
[CSS-SIZING-3]
Tab Atkins Jr.; Elika Etemad. CSS Intrinsic & Extrinsic Sizing Module Level 3. 4 March 2018. WD. URL: https://www.w3.org/TR/css-sizing-3/
[CSS-VALUES-3]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 14 August 2018. CR. URL: https://www.w3.org/TR/css-values-3/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 10 October 2018. WD. URL: https://www.w3.org/TR/css-values-4/
[CSS-WRITING-MODES-4]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 4. 24 May 2018. CR. URL: https://www.w3.org/TR/css-writing-modes-4/
[CSS2]
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-IMAGES]
Elika Etemad; Tab Atkins Jr.. CSS Image Values and Replaced Content Module Level 3. 17 April 2012. CR. URL: https://www.w3.org/TR/css3-images/
[CSS3COL]
Florian Rivoal; Rachel Andrew. CSS Multi-column Layout Module Level 1. 28 May 2018. WD. URL: https://www.w3.org/TR/css-multicol-1/
[MEDIAQUERIES-4]
Florian Rivoal; Tab Atkins Jr.. Media Queries Level 4. 5 September 2017. CR. URL: https://www.w3.org/TR/mediaqueries-4/
[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

[CSS3-BREAK]
Rossen Atanassov; Elika Etemad. CSS Fragmentation Module Level 3. 4 December 2018. CR. URL: https://www.w3.org/TR/css-break-3/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value
aspect-ratio auto | <ratio> auto all elements except inline boxes and internal ruby or table boxes no n/a discrete per grammar specified keyword or a pair of numbers

Issues Index

This is a diff spec from CSS Sizing Level 3. It is currently an Exploratory Working Draft: if you are implementing anything, please use Level 3 as a reference. We will merge the Level 3 text into this draft once it reaches CR.
This section is a rough draft proposal. See discussion in Issue 333 and Issue 1173.
Can we just slip this aspect-ratio rule into the UA default style sheet for images, so that they have an aspect ratio while they load? This would avoid the need for extra reflows after loading.
Define a table of all the ways this creates conflicts and cycles and break them.
Define a table of all the ways this creates conflicts and cycles and break them.
Need to Sync This Section With Level 3 It is Super Out Of Date
Need to handle floats. See Greg’s issue and dbaron’s response.
¯\_(ツ)_/¯
Copy whatever stretch-fit ends up doing wrt margin collapsing.
If there is a minimum size in one dimension that would cause overflow of the target rectangle if the aspect ratio were honored, do we honor the aspect ratio or skew the image? If the former, we need a step similar to #2 that applies the relevant minimums.