Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contain-intrinsic-size - improve docs #20912

Merged
merged 9 commits into from
Sep 23, 2022
53 changes: 50 additions & 3 deletions files/en-us/web/css/contain-intrinsic-size/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ browser-compat: css.properties.contain-intrinsic-size

{{CSSRef}}

The **`contain-intrinsic-size`** [CSS](/en-US/docs/Web/CSS) property controls the natural size of an element specified by [`content-visibility`](/en-US/docs/Web/CSS/content-visibility).
The **`contain-intrinsic-size`** [CSS](/en-US/docs/Web/CSS) [shorthand property](/en-US/docs/Web/CSS/Shorthand_properties) sets the size of an element that will be used for layout when it is subject to [size containment](/en-US/docs/Web/CSS/CSS_Containment#size_containment).

The property is commonly applied alongside elements that can trigger size containment, such as [`contain: size`](/en-US/docs/Web/CSS/contain) and [`content-visibility`](/en-US/docs/Web/CSS/content-visibility).

## Constituent properties

This property is a shorthand for the following CSS properties:

- `contain-intrinsic-width`
- `contain-intrinsic-height`

## Syntax

Expand All @@ -15,8 +24,14 @@ The **`contain-intrinsic-size`** [CSS](/en-US/docs/Web/CSS) property controls th
contain-intrinsic-size: 1000px;
contain-intrinsic-size: 10rem;

/* <percentage> value */
contain-intrinsic-size: 10%;
/* width | height */
contain-intrinsic-size: 1000px 1.5em;

/* auto <length> */
contain-intrinsic-size: auto 300px;

/* auto width | auto height */
contain-intrinsic-size: auto 300px auto 4rem;

/* Global values */
contain-intrinsic-size: inherit;
Expand All @@ -25,6 +40,38 @@ contain-intrinsic-size: revert;
contain-intrinsic-size: unset;
```

### Values

Either one or two of the following values may be specified for an element.
If two values are specified, the first value applies to the width, and the second to the height.
If a single value is specified, it applies to both width and height.

- `none`
- : The element has no intrinsic size in the given dimension(s).
- `<length>`
- : The element has the specified {{cssxref("&lt;length&gt;")}} in the given dimension(s).
- `auto <length>`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @estelle , I find auto a little tricky. The theory is pretty simple - it saves the natural/rendered size of the element whenever it is outside of size containment and then uses that instead of the stated length when appropriate.

What I don't get is why you would bother - the spec indicates this is only really used offscreen and will switch to using length for on-screen. I'm in discussion here w3c/csswg-drafts#7769 (comment) - might just be being stupid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I get the auto thing now; it makes sense and this documentation is correct.

The auto option is mostly to get the benefits of size containment for offscreen elements without developers having to be too precise about the size of the element. It makes sense when you're skipping rendering the child elements. You don't have to worry about the fact that the length will be used when you're not skipping child elements because in this case as the element becomes visible you leave size containment and start rendering them.

If you're on screen and deliberately getting the benefits of size containment then it is up to the developer to make sure that the size is properly calculated.

- : A remembered value of the "normally rendered" element size if one exists and the element is skipping its contents (for example, when it is offscreen); otherwise the specified `<length>`.

## Description

Size containment allows a user agent to layout an element as though it had a fixed size, preventing unnecessary reflows by avoiding the re-rendering of child elements to determine the actual size (thereby improving user experience).
By default, size containment treats elements as though they had no contents, and may collapse the layout in the same way as if the contents had no width or height.
The `contain-intrinsic-size` property allows authors to specify an appropriate value to be used as the size for layout.

Determining the correct size to specify for an element can be difficult, and odd layout effects may result if an incorrect value is used.
The `auto <length>` value can help.
If the element is ever rendered with all its child elements (if the element is ever outside of size containment), then setting `auto` saves the size, which can be used instead of the `<length>`.
In particular, this is recommended with [`content-visibility: auto`](/en-US/docs/Web/CSS/content-visibility), as elements are only in size containment when offscreen, and hence may have a remembered value.

## Formal definition

{{cssinfo}}

## Formal syntax

{{csssyntax}}

## Specifications

{{Specifications}}
Expand Down
1 change: 1 addition & 0 deletions files/en-us/web/css/shorthand_properties/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ See [Cascade and inheritance](/en-US/docs/Learn/CSS/Building_blocks/Cascade_and_
- {{cssxref("border-width")}}
- {{cssxref("column-rule")}}
- {{cssxref("columns")}}
- {{cssxref("contain-intrinsic-size")}}
- {{cssxref("flex")}}
- {{cssxref("flex-flow")}}
- {{cssxref("font")}}
Expand Down