-
Notifications
You must be signed in to change notification settings - Fork 647
/
Overview.bs
2906 lines (2544 loc) · 114 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class=metadata>
Title: CSS Containment Module Level 2
Level: 2
Shortname: css-contain
Status: ED
Work Status: stable
Group: csswg
ED: https://drafts.csswg.org/css-contain-2/
Previous Version: https://www.w3.org/TR/2022/WD-css-contain-2-20220917/
Previous Version: https://www.w3.org/TR/2020/WD-css-contain-2-20201216/
Previous Version: https://www.w3.org/TR/2020/WD-css-contain-2-20200603/
Previous Version: https://www.w3.org/TR/2019/WD-css-contain-2-20191111/
Previous Version: https://www.w3.org/TR/2019/WD-css-contain-2-20191015/
TR: https://www.w3.org/TR/css-contain-2/
Editor: Tab Atkins, Google, http://xanthir.com/contact/, w3cid 42199
Editor: Florian Rivoal, On behalf of Bloomberg, https://florian.rivoal.net/, w3cid 43241
Editor: Vladimir Levin, Google, [email protected], w3cid 75295
Abstract: This CSS module describes the 'contain' property, which indicates that the element's subtree is independent of the rest of the page. This enables heavy optimizations by user agents when used well.
Test Suite: https://test.csswg.org/harness/results/css-contain-1_dev/
WPT Path Prefix: css/css-contain/
</pre>
<pre class=link-defaults>
spec:css-display-3; type:property; text:display
spec:css-ui-3; type:property; text:text-overflow
spec:css-ui-4; type:property; text:pointer-events
spec:css-grid-1; type:property; text:grid
spec:css-break-3; type:dfn;
text:forced break
text:fragmentation
text:fragmentation container
text:fragmentation context
text:fragmented flow
spec:css-sizing-4; type:property;
text:contain-intrinsic-size
text:aspect-ratio
spec:intersection-observer; type:dfn; text:intersection root
spec:css-sizing-3;
type:value;
for:height; text:auto
for:width; text:min-content
for:width; text:max-content
type:property;
text:width
text:height
</pre>
<h2 id='intro'>
Introduction</h2>
Efficiently rendering a website relies on the user agent being able to detect what parts of the page are being displayed,
which parts might affect the currently-displayed section,
and what can be ignored.
There are various heuristics that can be used to guess when a given sub-tree is independent of the rest of the page in some manner,
but they're fragile,
so innocuous changes to a page may inadvertently make it fail such heuristic tests,
causing rendering to fall into a slow code path.
There are also many things that would be good to isolate which are difficult or impossible to detect in a heuristic manner.
To alleviate these problems
and allow strong, predictable isolation of a subtree from the rest of the page,
this specification defines a 'contain' property.
To allow even further optimization of off-screen contents,
this spec also defines a 'content-visibility' property,
enabling the user agent to skip an element's layout and painting <em>entirely</em> when not needed.
<h3 id="interaction">
Module Interactions</h3>
This document defines new features not present in earlier specifications.
In addition, it aims to replace and supersede [[!CSS-CONTAIN-1]]
once stable.
<h3 id="values">
Value Definitions</h3>
This specification follows the <a href="https://www.w3.org/TR/CSS2/about.html#property-defs">CSS property definition conventions</a> from [[!CSS2]]
using the <a href="https://www.w3.org/TR/css-values-3/#value-defs">value definition syntax</a> from [[!CSS-VALUES-3]].
Value types not defined in this specification are defined in CSS Values & Units [[!CSS-VALUES-3]].
Combination with 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 <a>CSS-wide keywords</a> as their property value.
For readability they have not been repeated explicitly.
<h2 id='contain-property'>
Strong Containment: the 'contain' property</h2>
<wpt title="The following tests are crash tests
that relate to general usage
of this feature
but are not particularly tied to any particular normative statement.">
contain-chrome-thcrash-001.html
contain-crash.html
contain-flexbox-outline.html
crashtests/contain-nested-crash-001.html
crashtests/contain-nested-crash-002.html
crashtests/contain-nested-crash-003.html
crashtests/contain-nested-crash-004.html
crashtests/contain-nested-relayout-boundary.html
quote-scoping-shadow-dom-crash.html
contain-style-remove-element-crash.html
</wpt>
<pre class='propdef'>
Name: contain
Value: none | strict | content | [ [size | inline-size] || layout || style || paint ]
Initial: none
Inherited: no
Applies to: See <a href="#contain-applies">below</a>
Computed value: the keyword ''contain/none'' or one or more of ''size'', ''layout'', ''paint''
Animation type: not animatable
</pre>
<wpt>
contain-animation-001.html
inheritance.html
parsing/contain-computed.html
parsing/contain-invalid.html
parsing/contain-valid.html
parsing/contain-computed-children.html
contain-layout-size-003.html
contain-paint-size-001.html
contain-paint-size-002.html
contain-paint-size-003.html
</wpt>
<p class=all-media>User agents are expected to support this property on all media, including non-visual ones.</p>
The 'contain' property allows an author to indicate that an element and its contents are,
as much as possible,
<em>independent</em> of the rest of the document tree.
This allows user agents to utilize much stronger optimizations when rendering a page using 'contain' properly,
and allows authors to be confident that their page won't accidentally fall into a slow code path
due to an innocuous change.
<dl dfn-type=value dfn-for=contain>
<dt><dfn>none</dfn>
<dd>
This value indicates that the property has no effect.
The element renders as normal,
with no containment effects applied.
<dt><dfn>strict</dfn>
<dd>
This value computes to ''size layout paint style'',
and thus turns on all forms of <a>containment</a> for the element.
<wpt>
contain-strict-001.html
contain-strict-002.html
contain-strict-003.html
contain-strict-011.html
contain-flexbox-outline.html
</wpt>
<dt><dfn>content</dfn>
<dd>
This value computes to ''layout paint style'',
and thus turns on all forms of <a>containment</a> <em>except</em> <a>size containment</a> for the element.
<wpt>
contain-content-001.html
contain-content-002.html
contain-content-003.html
contain-content-004.html
contain-content-011.html
</wpt>
Note: ''contain: content'' is reasonably "safe" to apply widely;
its effects are fairly minor in practice,
and most content won't run afoul of its restrictions.
However, because it doesn't apply <a>size containment</a>,
the element can still respond to the size of its contents,
which can cause layout-invalidation to percolate further up the tree than desired.
Use ''contain: strict'' when possible,
to gain as much containment as you can.
<dt><dfn>size</dfn>
<dd>
The value turns on <a>size containment</a> for the element.
This ensures that the [=size containment box|containment box=] can be laid out
without needing to examine its descendants.
<wpt>
contain-size-001.html
contain-size-002.html
contain-size-003.html
contain-size-004.html
contain-size-005.html
contain-size-006.html
contain-size-007.html
contain-size-008.html
contain-size-009.html
contain-size-010.html
contain-size-011.html
contain-size-012.html
contain-size-012b.html
contain-size-013.html
contain-size-021.html
contain-size-023.html
contain-size-025.html
contain-size-027.html
contain-size-041.html
contain-size-042.html
contain-size-051.html
contain-size-052.html
contain-size-056.html
contain-size-061.html
contain-size-062.html
contain-size-063.html
contain-size-baseline-001.html
contain-size-borders.html
contain-size-breaks-001.html
contain-size-button-001.html
contain-size-fieldset-001.html
contain-size-fieldset-002.html
contain-size-flexbox-001.html
contain-size-flexbox-002.html
contain-size-grid-001.html
contain-size-grid-002.html
contain-size-grid-003.html
contain-size-grid-004.html
contain-size-grid-005.html
contain-size-grid-006.html
contain-size-monolithic-001.html
contain-size-monolithic-002.html
contain-size-multicol-001.html
contain-size-multicol-as-flex-item.html
contain-size-replaced-001.html
contain-size-replaced-002.html
contain-size-replaced-003a.html
contain-size-replaced-003b.html
contain-size-replaced-003c.html
contain-size-replaced-004.html
contain-size-replaced-005.html
contain-size-replaced-006.html
contain-size-replaced-007.html
contain-size-select-001.html
contain-size-select-002.html
contain-size-scrollbars-001.html
contain-size-scrollbars-002.html
contain-size-scrollbars-003.html
contain-size-scrollbars-004.html
contain-layout-size-003.html
contain-paint-size-001.html
contain-paint-size-002.html
contain-paint-size-003.html
contain-size-removed.html
contain-size-dynamic-001.html
contain-size-grid-indefinite-height-min-height-flex-row.html
contain-size-grid-stretches-auto-rows.html
</wpt>
<dt><dfn>inline-size</dfn>
<dd>
This value turns on [=inline-size containment=] for the element.
This prevents the [=inline-size=] of its [=principal box=]
from directly depending on its contents.
<wpt>
contain-inline-size-bfc-floats-001.html
contain-inline-size-bfc-floats-002.html
contain-inline-size-fieldset.html
contain-inline-size-flex.html
contain-inline-size-flexitem.html
contain-inline-size-grid.html
contain-inline-size-intrinsic.html
contain-inline-size-legend.html
contain-inline-size-multicol.html
contain-inline-size-regular-container.html
contain-inline-size-removed.html
contain-inline-size-replaced.html
contain-inline-size-table.html
contain-inline-size-vertical-rl-.html
contain-inline-size-grid-indefinite-height-min-height-flex-row.html
contain-inline-size-grid-stretches-auto-rows.html
</wpt>
Note: There can still be indirect dependencies,
see [[#containment-inline-size]].
<dt><dfn>layout</dfn>
<dd>
This value turns on <a>layout containment</a> for the element.
This ensures that the [=layout containment box|containment box=] is <em>totally opaque</em> for layout purposes;
nothing outside can affect its internal layout,
and vice versa.
<wpt>
contain-layout-001.html
contain-layout-002.html
contain-layout-003.html
contain-layout-004.html
contain-layout-005.html
contain-layout-006.html
contain-layout-007.html
contain-layout-009.html
contain-layout-010.html
contain-layout-011.html
contain-layout-012.html
contain-layout-013.html
contain-layout-014.html
contain-layout-016.html
contain-layout-017.html
contain-layout-018.html
contain-layout-019.html
contain-layout-020.html
contain-layout-021.html
contain-layout-baseline-001.html
contain-layout-baseline-002.html
contain-layout-baseline-003.html
contain-layout-baseline-004.html
contain-layout-baseline-005.html
contain-layout-breaks-001.html
contain-layout-breaks-002.html
contain-layout-cell-001.html
contain-layout-cell-002.html
contain-layout-flexbox-001.html
contain-layout-grid-001.html
contain-layout-ifc-022.html
contain-layout-independent-formatting-context-001.html
contain-layout-independent-formatting-context-002.html
contain-layout-independent-formatting-context-003.html
contain-layout-ink-overflow-013.html
contain-layout-ink-overflow-014.html
contain-layout-ink-overflow-015.html
contain-layout-ink-overflow-016.html
contain-layout-ink-overflow-017.html
contain-layout-ink-overflow-018.html
contain-layout-ink-overflow-019.html
contain-layout-ink-overflow-020.html
contain-layout-size-003.html
contain-subgrid-001.html
contain-layout-dynamic-001.html
contain-layout-dynamic-004.html
contain-layout-dynamic-005.html
</wpt>
<dt><dfn>style</dfn>
<dd>
This value turns on <a>style containment</a> for the element.
This ensures that,
for properties which can have effects on more than just an element and its descendants,
those effects don't escape the element.
<wpt>
contain-style-baseline-001.html
contain-style-breaks-001.html
contain-style-breaks-002.html
contain-style-breaks-003.html
contain-style-breaks-004.html
contain-style-breaks-005.html
contain-style-counters-001.html
contain-style-counters-002.html
contain-style-counters-003.html
contain-style-counters-004.html
contain-style-counters-005.html
contain-style-ol-ordinal-li-container.html
contain-style-ol-ordinal-pseudo-reversed.html
contain-style-ol-ordinal-pseudo.html
contain-style-ol-ordinal-reversed.html
contain-style-ol-ordinal-start-reversed.html
contain-style-ol-ordinal-start.html
contain-style-ol-ordinal.html
counter-scoping-001.html
counter-scoping-002.html
counter-scoping-003.html
counter-scoping-004.html
quote-scoping-001.html
quote-scoping-002.html
quote-scoping-003.html
quote-scoping-004.html
quote-scoping-invalidation-001.html
quote-scoping-invalidation-002.html
quote-scoping-invalidation-003.html
quote-scoping-invalidation-004.html
contain-style-dynamic-001.html
</wpt>
<dt><dfn>paint</dfn>
<dd>
This value turns on <a>paint containment</a> for the element.
This ensures that the descendants of the [=paint containment box|containment box=] don't display outside its bounds,
so if an element is off-screen or otherwise not visible,
its descendants are also guaranteed to be not visible.
<wpt>
contain-paint-001.html
contain-paint-002.html
contain-paint-004.html
contain-paint-005.html
contain-paint-006.html
contain-paint-007.html
contain-paint-008.html
contain-paint-009.html
contain-paint-010.html
contain-paint-011.html
contain-paint-012.html
contain-paint-014.html
contain-paint-015.html
contain-paint-016.html
contain-paint-017.html
contain-paint-018.html
contain-paint-019.html
contain-paint-020.html
contain-paint-021.html
contain-paint-022.html
contain-paint-023.html
contain-paint-024.html
contain-paint-025.html
contain-paint-047.html
contain-paint-048.html
contain-paint-baseline-001.html
contain-paint-cell-001.html
contain-paint-cell-002.html
contain-paint-clip-011.html
contain-paint-clip-012.html
contain-paint-clip-013.html
contain-paint-clip-014.html
contain-paint-clip-015.html
contain-paint-clip-016.html
contain-paint-clip-017.html
contain-paint-clip-018.html
contain-paint-clip-019.html
contain-paint-ifc-011.html
contain-paint-independent-formatting-context-001.html
contain-paint-independent-formatting-context-002.html
contain-paint-independent-formatting-context-003.html
contain-paint-size-001.html
contain-paint-size-002.html
contain-paint-size-003.html
contain-paint-table-001.html
contain-paint-table-002.html
contain-subgrid-001.html
contain-paint-change-opacity.html
contain-paint-dynamic-001.html
contain-paint-dynamic-002.html
contain-paint-dynamic-003.html
contain-paint-dynamic-004.html
contain-paint-dynamic-005.html
</wpt>
</dl>
<span id="contain-applies">This property generally applies to all elements (including [[css-pseudo-4#generated-content]])</span>,
although some types of containment have no effect on some elements,
as detailed in [[#containment-types]].
In addition, in the case of [[SVG2]],
the 'contain' property only applies to <{svg}> elements that have an associated CSS layout box.
<div class='example'>
'contain' is useful when used widely on a page,
particularly when a page contains a lot of "widgets" which are all independent.
For example, assume a micropost social network had markup something like this:
<pre><code highlight=markup>
<body>
<aside>...</aside>
<section>
<h2>Messages</h2>
<article>
Lol, check out this dog: images.example.com/jsK3jkl
</article>
<article>
I had a ham sandwich today. #goodtimes
</article>
<article>
I have political opinions that you need to hear!
</article>
…
</section>
</body>
</code></pre>
There are probably a <em>lot</em> of messages displayed on the site,
but each is independent and won't affect anything else on the site.
As such, each can be marked with ''contain: content'' to communicate this to the user agent,
so it can optimize the page and skip a lot of computation for messages that are off-screen.
If the size of each message is known ahead of time,
''contain: strict'' can be applied to communicate further restrictions.
</div>
Additionally, when any [=containments=] are active
on either the HTML <{html}> or <{body}> elements,
propagation of properties
from the <{body}> element
to the [=initial containing block=], the viewport, or the [=canvas background=],
is disabled.
Notably, this affects:
* 'writing-mode', 'direction', and 'text-orientation' (see [[CSS-WRITING-MODES-3#principal-flow]])
* 'overflow' and its longhands (see [[CSS-OVERFLOW-3#overflow-propagation]])
* 'background' and its longhands (see [[CSS-BACKGROUNDS-3#body-background]])
<wpt>
contain-body-bg-001.html
contain-body-bg-002.html
contain-body-bg-003.html
contain-body-bg-004.html
contain-body-dir-001.html
contain-body-dir-002.html
contain-body-dir-003.html
contain-body-dir-004.html
contain-body-overflow-001.html
contain-body-overflow-002.html
contain-body-overflow-003.html
contain-body-overflow-004.html
contain-body-t-o-001.html
contain-body-t-o-002.html
contain-body-t-o-003.html
contain-body-t-o-004.html
contain-body-w-m-001.html
contain-body-w-m-002.html
contain-body-w-m-003.html
contain-body-w-m-004.html
contain-html-bg-001.html
contain-html-bg-002.html
contain-html-bg-003.html
contain-html-bg-004.html
contain-html-dir-001.html
contain-html-dir-002.html
contain-html-dir-003.html
contain-html-dir-004.html
contain-html-overflow-001.html
contain-html-overflow-002.html
contain-html-overflow-003.html
contain-html-overflow-004.html
contain-html-t-o-001.html
contain-html-t-o-002.html
contain-html-t-o-003.html
contain-html-t-o-004.html
contain-html-w-m-001.html
contain-html-w-m-002.html
contain-html-w-m-003.html
contain-html-w-m-004.html
</wpt>
Note: Propagation
to the [=initial containing block=], the viewport, or the [=canvas background=],
of properties set on the <{html}> element itself
is unaffected.
Note: Several properties beyond 'contain' can turn on various [=containments=] for an element.
These do not affect the value of 'contain';
an element can have ''contain: none''
but still have [=layout containment=] turned on by 'content-visibility',
for example.
<h2 id='containment-types'>
Types of Containment</h2>
There are several varieties of <dfn export>containment</dfn> that an element can be subject to,
restricting the effects that its descendants can have on the rest of the page in various ways.
<a>Containment</a> enables much more powerful optimizations by user agents,
and helps authors compose their page out of functional units,
as it limits how widely a given change can affect a document.
Advisement: Specification authors introducing new properties or mechanisms
need to consider whether and how the various types of containment
affect what they are introducing,
and include in their specification any effect not described here.
<h3 id='containment-size'>
Size Containment</h3>
Giving an element <dfn export>size containment</dfn>
makes its [=principal box=] a <dfn>size containment box</dfn>
and has the following effects:
1.
The [=intrinsic sizes=] of the [=size containment box=]
are determined as if the element had no content,
following the same logic as when [=sizing as if empty=].
Note: This affects explicit invocations of the ''min-content'' or ''max-content'' keywords,
as well as any calculation that depends on these measurement,
such as sizing <span class=informative>[=grid tracks=]</span> into which a size contained item is placed,
or if [=fit-content sizing=] the containment box's parent.
<wpt>
contain-size-013.html
contain-size-041.html
contain-size-042.html
contain-size-replaced-001.html
contain-size-replaced-002.html
contain-size-replaced-003a.html
contain-size-replaced-003b.html
contain-size-replaced-003c.html
contain-size-replaced-004.html
contain-size-replaced-005.html
contain-size-replaced-006.html
contain-size-block-001.html
contain-size-block-002.html
contain-size-block-003.html
contain-size-block-004.html
contain-size-button-002.html
contain-size-fieldset-003.html
contain-size-flex-001.html
contain-size-grid-005.html
contain-size-grid-006.html
contain-size-inline-block-001.html
contain-size-inline-block-002.html
contain-size-inline-block-003.html
contain-size-inline-block-004.html
contain-size-inline-flex-001.html
contain-size-multicol-002.html
contain-size-multicol-003.html
contain-size-select-elem-001.html
contain-size-select-elem-002.html
contain-size-select-elem-003.html
contain-size-select-elem-004.html
contain-size-select-elem-005.html
</wpt>
2. Laying out a [=size containment box=] and its content is conceptually done in two phases:
<dl>
<dt><dfn>Sizing as if empty</dfn>
<dd>
The [=used value|used=] 'width' and 'height' of the [=size containment box|containment box=]
are determined as if performing a normal layout of the box,
except that it is treated as having no content--
not even through pseudo elements such as ''::before'', ''::after'', or ''::marker''.
<wpt>
contain-size-021.html
contain-size-023.html
contain-size-025.html
contain-size-027.html
contain-size-061.html
contain-size-062.html
contain-size-063.html
contain-size-borders.html
contain-size-fieldset-001.html
contain-size-fieldset-002.html
contain-size-select-001.html
contain-size-select-002.html
contain-size-scrollbars-001.html
contain-size-scrollbars-002.html
contain-size-scrollbars-003.html
contain-size-scrollbars-004.html
contain-size-button-001.html
contain-size-flexbox-001.html
contain-size-flexbox-002.html
contain-size-grid-001.html
</wpt>
<a>Replaced elements</a> must be treated as having a [=natural dimensions|natural=] width and height of 0
and no [=natural aspect ratio=].
Note: Size containment only suppresses the [=natural aspect ratio=],
so properties like 'aspect-ratio' which affect that [=preferred aspect ratio=] directly
are honored.
<wpt>
contain-size-013.html
contain-size-041.html
contain-size-042.html
contain-size-replaced-001.html
contain-size-replaced-002.html
contain-size-replaced-003a.html
contain-size-replaced-003b.html
contain-size-replaced-003c.html
contain-size-replaced-004.html
contain-size-replaced-005.html
</wpt>
<wpt pathprefix=css/css-sizing/aspect-ratio/>
replaced-element-023.html
replaced-element-025.html
replaced-element-027.html
</wpt>
All CSS properties of the [=size containment box=] are taken into account
as they would be when performing layout normally.
Other specifications may make specific exemptions.
<wpt>
contain-size-replaced-006.html
</wpt>
Note: Even when the element's [=sizing properties=] specify an intrinsic size,
this does not necessarily make the element zero-sized:
properties set on the element itself
continue to be taken into account,
which can cause it to be larger.
<wpt>
contain-size-grid-002.html
contain-size-grid-003.html
contain-size-multicol-001.html
contain-size-multicol-as-flex-item.html
</wpt>
<dt><dfn>Laying out in-place</dfn>
<dd>
The [=size containment box|containment box=]'s content
(including any pseudo-elements)
must then be laid out into
the now fixed-size [=size containment box|containment box=] normally.
<wpt>
contain-size-064.html
<wpt>
</dl>
Note: [=Size containment=] does not suppress baseline alignment.
See [=layout containment=] for that.
<wpt>
contain-size-baseline-001.html
contain-size-fieldset-004.html
contain-size-inline-block-001.html
contain-size-inline-block-002.html
contain-size-inline-block-003.html
contain-size-inline-block-004.html
contain-size-inline-flex-001.html
</wpt>
3. [=Size containment boxes=] are <a spec=css-break-3>monolithic</a> (See [[CSS-BREAK-3#possible-breaks]]).
<wpt>
contain-size-breaks-001.html
contain-size-monolithic-001.html
contain-size-monolithic-002.html
contain-size-multicol-004.html
</wpt>
<div class=example>
Given the following markup and style, the image would be sized to 100px by 100px,
as the aspect ratio set by the 'aspect-ratio' property takes effect.
<pre><code class=lang-css>
img {
width: 100px;
aspect-ratio: 1/1;
contain: size;
}
</code><code class=lang-html>
<img src="https://www.example.com/300x100.jpg">
</code></pre>
If the 'aspect-ratio' property had not been declared,
the image would have been 100px by 0px,
as its [=natural aspect ratio=] is suppressed,
and its [=natural height=] is treated as 0.
</div>
However, giving an element [=size containment=]
has no effect if any of the following are true:
* if the element does not generate a <a>principal box</a>
(as is the case with ''display: contents'' or ''display: none'')
* if its [=inner display type=] is ''display/table''
<wpt>
contain-size-012.html
contain-size-012b.html
</wpt>
* if its [=principal box=] is
an <a spec="css-display-3">internal table box</a>
<wpt>
contain-size-006.html
contain-size-007.html
contain-size-008.html
contain-size-009.html
contain-size-010.html
contain-size-051.html
contain-size-052.html
</wpt>
* if its [=principal box=] is
an <a spec="css-display-3">internal ruby box</a>
or a <a spec="css-display-3" lt="atomic inline">non-atomic</a> <a spec="css-display-3">inline-level</a> box
<wpt>
contain-size-002.html
contain-size-003.html
contain-size-004.html
contain-size-005.html
contain-size-001.html
</wpt>
Note: Internal table boxes,
which do not include table captions,
are excluded,
because the table layout algorithm
does not allow boxes to become smaller than their inflow content.
Sizing a table cell as if it was empty and then laying out its content inside without changing the size
is effectively an undefined operation.
Manually setting the 'width' or 'height' properties to ''0''
cannot make it smaller than its content.
This concern does not apply to table captions,
which are perfectly capable of having a fixed size
that is independent of their content.
<wpt>
contain-size-011.html
contain-size-056.html
contain-size-table-caption-001.html
</wpt>
<h4 id='containment-size-opt' class="no-toc">
Possible Size-Containment Optimizations</h4>
<em>This section is non-normative.</em>
By itself, <a>size containment</a> does not offer much optimization opportunity.
Its primary benefit on its own is that tools which want to lay out the [=size containment box|containment box=]'s contents
based on the [=size containment box|containment box=]'s size
(such as a JS library implementing the "container query" concept)
can do so without fear of "infinite loops",
where having a child's size respond to the size of the [=size containment box|containment box=]
causes the [=size containment box|containment box=]'s size to change as well,
possibly triggering <em>further</em> changes in how the child sizes itself
and possibly thus more changes to the [=size containment box|containment box=]'s size,
ad infinitum.
When paired with <a>layout containment</a>, though,
possible optimizations that can be enabled include (but are not limited to):
1. When the style or contents of a descendant of the [=size containment box|containment box=] is changed,
calculating what part of the DOM tree is "dirtied" and might need to be re-laid out
can stop at the [=size containment box|containment box=].
2. When laying out the page,
if the [=size containment box|containment box=] is off-screen or obscured,
the layout of its contents (i.e. "[=laying out in-place=]") can be delayed or done at a lower priority.
<h3 id='containment-inline-size'>
Inline-Size Containment</h3>
Giving an element <dfn export>inline-size containment</dfn>
applies [=size containment=] to the [=inline-axis=] sizing of its [=principal box=].
This means the [=inline-axis=] [=intrinsic sizes=] of the [=principal box=]
are determined as if the element had no content.
However, content continues to impact the box’s [=block-axis=] [=intrinsic sizes=] as usual,
and the box is allowed to [=fragmentation|fragment=] normally in the [=block axis=].
<div class=note>
<span class="marker">Note:</span> In some cases,
a box’s [=block-axis=] [=intrinsic sizes=]
can impact layout in the parent [=formatting context=]
in ways that affect the box’s [=inline size=]
(e.g. by triggering scrollbars on an ancestor element),
creating a dependency of the box’s [=inline size=] on its own content.
If this changed [=inline size=] results in a different [=block size=],
that new [=block size=] can loop into further impacting the parent formatting context,
but not in a way that reverts it to the previously-problematic layout.
For example, if scrollbars were introduced,
they are not then removed,
even if the consequent [=block size=] is small enough to not need them;
or if a box’s logical height collides with a lower-placed float and is cleared down
to where it also has more available inline space
and thus becomes short enough to not have collided,
it is not them moved back up to its previous problematic size and position.
Thus, although [=inline-size containment=] prevents
the box’s content from directly affecting its [=inline size=]
through its [=inline-axis=] [=intrinsic sizes=],
its [=inline size=] can still indirectly depend on its contents
by their effect on its [=block size=].
</div>
ISSUE:
In general, the relationship between an element's inline size
and it's block size
is unpredictable and non-monotonic,
with the block size capable of shifting up and down arbitrarily
as the inline size is changed.
Infinite cycles are prevented
by ensuring that layout does not revert to a previous (known-problematic) state,
even if a naive analysis of the constraints would allow for such;
in other words, layout always “moves forward”.
We believe that current CSS layout specifications incorporate such rules,
but to the extent that they don't,
please <a href="https://github.com/w3c/csswg-drafts/issues">inform the CSSWG</a>
so that these errors can be corrected.
<div class=example>
Consider this example,
where float placement creates a dependency of block sizes on inline sizes:
<xmp class=lang-markup>
<section style="width: 200px; border: solid; display: flow-root;">
<!-- floated elements that impact the available space -->
<div style="float: left; width: 50px; height: 80px; background: blue;"></div>
<div style="float: right; width: 50px; height: 80px; background: blue;"></div>
<div style="float: left; width: 160px; height: 80px; background: navy;"></div>
<!-- parent layout, determining context -->
<article style="border: solid orangered; display: flow-root; min-width: min-content">
<div style="background: orange; aspect-ratio: 1/1;">
Article
</div>
</article>
</section>
</xmp>
<figure style="float: left; margin: 1em 0.5em">
<section style="width: 200px; border: solid; display: flow-root;">
<!-- floated elements that impact the available space -->
<div style="float: left; width: 50px; height: 80px; background: blue;"></div>
<div style="float: right; width: 50px; height: 80px; background: blue;"></div>
<div style="float: left; width: 160px; height: 80px; background: navy;"></div>
<!-- parent layout, determining context -->
<article style="border: solid orangered; display: flow-root; min-width: 50px">
<div style="background: orange; aspect-ratio: 1/1;">
Article
</div>
</article>
</section>
</figure>
The block layout algorithm will first place the floating boxes,
with the first two sitting in the left and right corners of the container,
and the third, being too wide to fit between, being pushed below them.
The following <code>article</code> will then be laid out.
Because it is ''display: flow-root'',
it cannot intersect any floats,
and thus must take them into account
when figuring out how to size and position itself.
The layout engine first attempts to place the <code>article</code>
flush with the top of the container,
resulting a ''100px'' width,
plenty wide enough to accommodate its [=min-content size=].
However, due to the 'aspect-ratio' of its child,
this would cause the <code>article</code> to be ''100px'' tall as well,
which would intersect the third float 80px below,
so this layout opportunity is discarded.
It then attempts to position the <code>article</code>
flush with the top of the third float,
in the narrow ''40px''-wide space to its right.
However, since the <code>article</code>’s 'min-width' makes it too large
to fit in the 40px-wide space beside the third float,
it shifts below that one as well,
forming a 200px square below all the floated boxes.
<figure style="float: right; margin: 1em 0.5em">
<section style="width: 200px; border: solid; display: flow-root;">
<!-- floated elements that impact the available space -->
<div style="float: left; width: 50px; height: 80px; background: blue;"></div>
<div style="float: right; width: 50px; height: 80px; background: blue;"></div>
<div style="float: left; width: 160px; height: 80px; background: navy;"></div>
<!-- parent layout, determining context -->
<article style="border: solid orangered; display: flow-root;">
<div style="background: orange; aspect-ratio: 1/1;">
Article
</div>
</article>
</section>
</figure>
If the 'min-width' is removed from the <code>article</code>,
or if [=inline-size containment=] is added to
either the <code>article</code> or <code>header</code>
(causing ''min-width: min-content'' to resolve to zero),
then the <code>article</code> will fit as a 40px square
next to the final floated <code>div</code>
(possibly with some of its content overflowing).
At this point, the width and height of the <code>article</code>
(''40px'' each)
<em>would</em> fit back in the first considered space,
flush with the top of the container.
However, the box is not returned to the previous position,
because the layout engine knows already
that this position would result in an invalid layout.
</div>
Giving an element [=inline-size containment=]
has no effect if any of the following are true:
* if the element does not generate a <a>principal box</a>
(as is the case with ''display: contents'' or ''display: none'')
* if its [=inner display type=] is ''display/table''
* if its [=principal box=] is
an <a spec="css-display-3">internal table box</a>
* if its [=principal box=] is
an <a spec="css-display-3">internal ruby box</a>
or a <a spec="css-display-3" lt="atomic inline">non-atomic</a> <a spec="css-display-3">inline-level</a> box
<h3 id='containment-layout'>
Layout Containment</h3>
Giving an element <dfn export>layout containment</dfn>
makes its [=principal box=] a <dfn>layout containment box</dfn>
and has the following effects:
1. The [=layout containment box=] [=establishes an independent formatting context=].