CSS Intrinsic & Extrinsic Sizing Module Level 3

Shortname: css-sizing
Level: 3
Status: WD
Work Status: Revising
Group: csswg
ED: https://drafts.csswg.org/css-sizing-3/
TR: https://www.w3.org/TR/css-sizing-3/
Editor: Tab Atkins, Google, http://xanthir.com/contact/
Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact, w3cid 35400
Previous version: https://www.w3.org/TR/2016/WD-css-sizing-3-20160512/
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.
Ignored Terms: block-level box

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.

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

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.

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 performing layout; that is, a <>, a measure of text (without consideration of line-wrapping), a size of the initial containing block, or a <> or other formula (such the “stretch-fit” sizing of non-replaced blocks [[CSS2]]) 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 ''margin/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 ''width/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.

Auto Box Sizes

: stretch-fit size : stretch-fit inline size : stretch-fit block size :: Roughly, the size a box would take if it filled the available space in the given axis. (See [[#extrinsic]].) 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 [[#intrinsic]].) 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 [[#intrinsic]].) : 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. Issue: 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.

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 ''margin/auto'' margins are treated as zero.

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.

New Sizing Keywords

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(<>)
	

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 ''width/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 the property’s initial value.
min-content
If specified for the inline axis, use the min-content inline size; otherwise compute to the property’s initial value.
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 the property’s initial value.
fit-content(<>)
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, <>)); otherwise compute to the property’s initial value.
Issue: Is ''width/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: Percentages resolved against the intrinsic sizes (''width/max-content'', ''width/min-content'', ''width/fit-content'') will behave as ''width/auto''. To have a common term for both when 'width'/'height' computes to ''width/auto'' and when it is defined to behave as ''width/auto'' (as in the case above), the property is said to behave as auto in both of these cases. Note: Legacy spec prose defining layout behavior might explicitly refer to 'width'/'height' having a computed value of ''width/auto'' as a condition; these should be interpreted as meaning behaves as auto, and reported to the CSSWG for updating.

Containing Floats

Note: To ensure that a container sizes itself to contain any descendant 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;''.

Column Sizing Keywords

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

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(<>)
Specifies the optimal column width as min(max-content size, max(min-content size, <>))

Intrinsic Size Determination

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

Intrinsic Sizes

The min-content size of a box in each axis is the size it would have as an ''width/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 ''width/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”.) If this size would depend on the size of the containing block (as it does for images with an intrinsic aspect ratio but no intrinsic size) and therefore can't be calculated against an infinitely-sized containing block then:
For boxes with an intrinsic aspect ratio, but no intrinsic size:
* If the available space is definite in the inline axis, use the stretch fit into that size for the inline size and calculate the block size using the aspect ratio. * Otherwise use a width of ''300px'' (height of ''150px'' in vertical writing modes) and calculate the other dimension using the aspect ratio.
For boxes without an intrinsic aspect ratio:
* If the available space is definite in the appropriate dimension, use the stretch fit into that size in that dimension. * Otherwise, use ''300px'' for the width and/or ''150px'' for the height as needed.
Note: This specification does not define how to determine the size of a float. Please refer to [[CSS2]], the relevant CSS specification for that display type, and/or existing implementations for further details.

Intrinsic Contributions

A box’s min-content contribution/max-content contribution in each axis is the size of the content box of a hypothetical ''width/auto''-sized float that contains only that box, if that hypothetical float's containing block is zero-sized/infinitely-sized. However, in the case of a [=replaced element|replaced=] box with a percentage-based 'width'/'max-width'/'height'/'max-height', the percentage is resolved to zero when calculating the min-content contribution in the corresponding axis.
For example, an <{input}> assigned ''width: calc(50% + 50px)'' has a min-content contribution of ''50px'', plus any horizontal margin/border/padding.
Note: We are not 100% sure if zeroing out a percentage 'max-width' on form controls is web-compatible. See Issue 765. Note: This specification does not define how to determine these sizes. Please refer to [[CSS2]], the relevant CSS specification for that display type, and/or existing implementations for further details.

Extrinsic Size Determination

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

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 ''margin/auto'' margins as zero), borders, and padding, flooring at zero. Issue: 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?

Percentage Sizing

Percentages specify sizing of a box with respect to the box’s containing block.
For example, in the following markup: <article style="height: 60em"> <aside style="height: 50%;"> </aside> </article> the <aside> would be 30em tall.
Sometimes the size of a percentage-sized box's containing block depends on the intrinsic size contribution of the box itself, creating a cyclic dependency. When calculating the containing block's size, the percentage behaves as auto. Then, unless otherwise specified, when calculating the used sizes and positions of the containing block’s contents: * If the cyclic dependency was introduced due to a 'block-size' or 'max-block-size' on the containing block that causes it to depend on the size of its contents, the box’s percentage is not resolved and instead behaves as auto. Note: Grid containers (and flex items?) do allow percentages to resolve in this case. * Otherwise, the percentage is resolved against the containing block’s size. (The containing block’s size is not re-resolved based on the resulting size of the box; the contents might thus overflow or underflow the containing block). Note: These rules specify the previously-undefined behavior of this cyclic case in CSS2§10.2. Note also, the behavior in CSS2§10.5 is superseded in their respective specifications for layout modes (such as flex layout) not described in CSS2. Similarly, percentage margins and padding behave as zero in such cyclic cases when calculating the containing block's size, and then resolve when calculating the used sizes and positions of its content. (This defines the previously-undefined behavior of this cyclic case in CSS2§8.3 and CSS2§8.4.
For example, in the following markup: <article style="width: min-content"> <aside style="width: 50%;"> LOOOOOOOOOOOOOOOOOOOONG </aside> </article> When calculating the width of the outer <article>, the inner <aside> behaves as ''width: auto'', so the <article> sets itself to the width of the long word. Since the <article>’s width didn't depend on "real" layout, though, it's treated as definite for resolving the <aside>, whose width resolves to half that of the <article>.
In this example, <article style="height:auto"> <aside style="height: 50%;"> <div class=block style="height: 150px;"></div> </aside> <section style="height: 30px;"></section> </article> because the percentage block size ('height', in this case) on block-level elements is defined to not resolve inside content-sized containing blocks, the percentage height on the <aside> is ignored, that is, it behaves exactly as if ''height/auto'' were specified.
Issue: Letting %s still resolve against a definite 'height' when the min-height is intrinsic is an open issue. (CSS2 has a general statement about "height depending on contents", which this technically is, even though CSS2 didn't have content-dependent keywords for 'min-height'. Since this is new, we think we could have this different behavior.) The following examples illustrate how block-axis percentages resolve against a containing block whose size depends on its contents. <article style="height:100px; min-height: min-content;"> <aside style="height: 50%;"> <div style="height: 150px;"></div> </aside> <section style="height: 30px;"></section> </article> The initial height of the <article> is 100px, as specified, which would make the <aside> 50px tall when it resolved its percentage. However, we must calculate the min-height, by substituting it in for 'height'. This causes the percentage on the <aside> to behave as auto, so the <aside> ends up 150px tall. The total height of the contents is thus 180px. This is larger than the specified 100px height, so the <article> gets adjusted to 180px tall. Then, since the percentage could originally resolve against the (100px) height, it now resolves against the 180px height, so the <aside> ends up being 90px tall. <article style="height:auto; min-height: min-content;"> <aside style="height: 50%;"> <div class=block style="height: 150px;"></div> </aside> <section style="height: 30px;"></section> </article> In this case, the percentage on the <aside> won't normally resolve, because the containing block's height is ''height/auto'' (and thus depends on the size of its contents). Instead it behaves as auto, resulting in a height of 150px for the <aside>, and an initial height of 180px for the <article> The 'min-height' doesn't change this; ''height: min-content;'' acts similarly to ''height: auto;'' and results in the same sizes. <article style="height:100px; min-height: min-content;"> <aside style="height: 200%;"> <div style="height: 150px;"></div> </aside> <section style="height: 30px;"></section> </article> This is a variation on the first code block, and follows a similar path; the <aside> initially wants to compute to 200px tall (200% of the 100px containing block height). When we calculate the effects of 'min-height', the percentage behaves as auto, causing it to become 150px tall, and the total ''height/min-content'' height of the containing block to be 180px tall. Since this is larger than 100px, the <article> gets clamped to 180px, the percentage resolves against this new height, and the <aside> ends up being 360px tall, overflowing the <article>

Changes

Significant changes since the 7 February 2017 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.