Skip to content

Commit 72911ee

Browse files
authored
Update responsive-iframes-explainer.md (w3c#12212)
Fix links. Add example.
1 parent 5910ff2 commit 72911ee

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

css-sizing-4/responsive-iframes-explainer.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
## Problem description
44

5-
[Iframes]([url](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/iframe)) are very useful for sandboxing web content into different documents. The options currently available are:
6-
1. A [same-origin]([url](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)) iframe, which provides full style &
5+
[url](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/iframe) are very useful for sandboxing web content into different documents. The options currently available are:
6+
1. A [same-origin](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) iframe, which provides full style &
77
layout isolation, and by-default script isolation. This prevents the parent and child documents from accidentally interfering with each other.
88
2. A cross-origin iframe, which additionally provides robust security and privacy against untrusted embedders or embedees.
99

1010
In addition, iframes participate in the layout of the parent document, via the style and layout of their owning `<iframe>` element.
11-
However, unlike other HTML elements such as `<div>`, iframe elements do not have the ability to support *responsive layout*, i.e. size themselves automatically to contain the [natural dimensions]([url](https://drafts.csswg.org/css-images-3/#natural-dimensions)) of the contents of the iframe. Instead, browsers automatically add scrollbars to iframes as necessary, so that users can access the content.
11+
However, unlike other HTML elements such as `<div>`, iframe elements do not have the ability to support *responsive layout*, i.e. size themselves automatically to contain the [natural dimensions](https://drafts.csswg.org/css-images-3/#natural-dimensions) of the contents of the iframe. Instead, browsers automatically add scrollbars to iframes as necessary, so that users can access the content.
1212

1313
This is a problem:
1414
* From a user's perspective, iframe scrollbars are a worse UX in cases where the iframe contents are important to them and can fit reasonably in the visible viewport without these additional scrollbars.
@@ -19,19 +19,19 @@ Embedded SVG documents already support responsive layout. *Responsive iframes* a
1919
## Use cases
2020

2121
Use cases include:
22-
* 3P comment widgets ([example]([url](https://github.com/whatwg/html/issues/555#issuecomment-177836009)))
23-
* Embedding self-contained worked examples in teaching UI ([example]([url](https://browser.engineering/layout.html)))
22+
* 3P comment widgets [example](https://github.com/whatwg/html/issues/555#issuecomment-177836009)
23+
* Embedding self-contained worked examples in teaching UI (https://browser.engineering/layout.html)
2424

2525
In general, there is a lot of demand for this feature, as evidenced by:
26-
* [Stack overflow]([url](https://stackoverflow.com/search?q=resize+iframe))
27-
* Many comments and positive reactions on the [issue proposing the feature for HTML]([url](https://github.com/whatwg/html/issues/555)), and [the same for CSS]([url](https://github.com/w3c/csswg-drafts/issues/1771)).
26+
* [Stack overflow](https://stackoverflow.com/search?q=resize+iframe)
27+
* Many comments and positive reactions on the [issue proposing the feature for HTML](https://github.com/whatwg/html/issues/555), and [the same for CSS](https://github.com/w3c/csswg-drafts/issues/1771).
2828
* Existence of multiple polyfills
2929

3030
## Solution
3131

3232
The embedding document opts in via the `contain-intrinsic-size: from-element` CSS property on the `<iframe>` element, and the embedded document opts in via a new `<meta name="responsive-embedded-sizing">` tag.
3333

34-
When the meta tag is present at the time the `load` event on the embedded document fires, the embedding document is notified with the new [natural height]([url](https://drafts.csswg.org/css-images-3/#natural-height)) of the embedded (iframe) document. If `contain-intrinsic-size` is set on the `<iframe>` element, it takes this heigh into consideration in the same way as any other replaced element's layout, along with other constraints specified by the HTML and CSS of the embedding document. Subsequent changes to content, styling or layout fo the embedded document do not affect the `<iframe>` sizing.
34+
When the meta tag is present at the time the `load` event on the embedded document fires, the embedding document is notified with the new [natural height](https://drafts.csswg.org/css-images-3/#natural-height) of the embedded (iframe) document. If `contain-intrinsic-size` is set on the `<iframe>` element, it takes this heigh into consideration in the same way as any other replaced element's layout, along with other constraints specified by the HTML and CSS of the embedding document. Subsequent changes to content, styling or layout fo the embedded document do not affect the `<iframe>` sizing.
3535

3636
The double opt-in preserves:
3737
* Backward compatibility for existing content
@@ -41,6 +41,32 @@ The "one-shot" sizing to natural dimensions avoids:
4141
* Performance issues and CLS due to changing iframe sizing (To further mitigatge performance risks, limitations on levels of `<iframe>` nesting may be imposed.)
4242
* Potential infinite layout loops
4343

44+
## Example:
45+
46+
The following example wil display an iframe of content `width `300px` (the default) and height `1000px` (the height of the `<div>`),
47+
plus a 1px border.
48+
49+
Embedding document:
50+
51+
```html
52+
<style>
53+
iframe {
54+
contain-intrinsic-size: from-element;
55+
border: 1px solid black;
56+
}
57+
</style>
58+
<iframe src="my-iframe.html">
59+
```
60+
61+
`my-iframe.html`:
62+
```html
63+
<style>
64+
* { margin: 0; }
65+
</style>
66+
<meta name="responsive-embedded-sizing">
67+
<div style="height: 100px">
68+
```
69+
4470
## Future extensions
4571

4672
A JavaScript API could be added in the future that would allow embedded documents to request relayout in their embedding document context.

0 commit comments

Comments
 (0)