@@ -836,65 +836,80 @@ Stretch-fit Sizing: filling the containing block</h3>
836
836
to make its outer size as close to filling the [=containing block=] as possible
837
837
while still respecting the constraints imposed by min-height/min-width/max-height/max-width.
838
838
839
- Formally, its behavior is the same as specifying an [=automatic size=]
840
- together with a [=self-alignment property=] value of ''width/stretch''
841
- (in the relevant axis),
842
- except that the resulting box,
843
- which can end up not exactly fitting its [=alignment container=] ,
844
- can be subsequently aligned by its actual [=self-alignment property=] value.
845
-
846
- Additionally,
847
- in [=formatting contexts=] and axes in which the relevant [=self-alignment property=] does not apply
848
- (such as the block axis in Block Layout, or the main axis in Flex Layout),
849
- in cases where a percentage size in that axis would resolve to a definite value,
850
- a [=stretch-fit size=]
851
- causes the box to attempt to fill its containing block--
852
- behaving as ''100%''
853
- but applying the resulting size to its margin box
854
- instead of the box indicated by 'box-sizing' .
855
- For this purpose, ''margin/auto'' margins are treated as zero,
856
- and furthermore, for [=block-level boxes=] in particular,
857
- if its block-start/block-end [=margin=]
858
- would be adjoining to its parent's block-start/block-end [=margin=]
859
- if its parent’s [=sizing properties=] all had their [=initial values=] ,
860
- then its block-start/block-end [=margin=] is treated as zero.
861
-
862
- Note: Consequently, if neither ''align-self/stretch'' alignment applies
863
- nor percentage sizing can resolve,
864
- then the box will resolve to its [=automatic size=] .
839
+ : If used in an axis where the relevant [=self-alignment property=] applies to the element
840
+ ::
841
+ Produces the same size that would be obtained
842
+ by the [=self-alignment property=] in that axis
843
+ being set to ''justify-self/stretch''
844
+ and all [=sizing properties=] and 'aspect-ratio'
845
+ being set to their initial values.
846
+
847
+ : Otherwise, if used in an axis where percentage sizes can resolve to a [=definite=] value
848
+ ::
849
+ Behaves as ''100%'' ,
850
+ except it sizes the [=margin box=]
851
+ regardless of the value of 'box-sizing' .
852
+
853
+ If this is a [=block axis=] size,
854
+ and the element is in a Block Layout [=formatting context=] ,
855
+ and the parent element does not have a [=block-start=] 'border' or 'padding'
856
+ and is not an [=independent formatting context=] ,
857
+ treat the element's [=block-start=] margin as zero
858
+ for the purpose of calculating this size.
859
+ Do the same for the [=block-end=] margin.
860
+
861
+ Note: This simulates the effect of margins collapsing with the parent's margin.
862
+ It doesn't actually suppress the margins,
863
+ so if anything prevents the element from actually collapsing with its parent,
864
+ the [=stretch-fit size=] might actually be too large
865
+ to fit in the parent perfectly.
866
+
867
+ <!--
868
+ Importantly, this *does* ensure that the stretch-fit size
869
+ doesn't depend on the presence or absence of other elements,
870
+ or the element's position among its siblings.
871
+ Multiple children with 'stretch' will all end up the same size.
872
+ -->
873
+
874
+ : Otherwise
875
+ ::
876
+ Behaves as the property's [=initial value=] .
865
877
866
878
<div class="example">
867
879
For example, given the following HTML representing two [=block boxes=] :
868
- <pre class=html>
869
- <div class="outer">
870
- <div class="inner"></div>
871
- </div>
872
- </pre>
880
+
881
+ <xmp class=html>
882
+ <div class="parent">
883
+ <div class="child"> text</div>
884
+ </div>
885
+ </xmp>
873
886
874
887
In the following case,
875
- the [=outer height=] of the inner box
876
- will exactly match the height of the outer box (200px),
888
+ the [=outer height=] of the child box
889
+ will exactly match the height of the parent box (200px),
877
890
but its [=inner height=] will be 20px less, to account for its margins.
878
891
879
892
<pre>
880
- .outer { height: 200px; border: solid; }
881
- .inner { height: stretch; margin: 10px; }
893
+ .parent { height: 200px; border: solid; }
894
+ .child { height: stretch; margin: 10px; }
882
895
</pre>
883
896
884
- In the following case,
885
- the height of the inner box
886
- will exactly match the height of the outer box (200px).
887
- The top margins will collapse,
888
- but the bottom margins do not collapse
889
- (because the bottom margin of a box is not adjoining
890
- to the bottom margin of a parent with a non-''height/auto'' height,
891
- see [[CSS2/box#collapsing-margins]] ),
892
- and therefore the inner box’s bottom margin will be truncated.
897
+ On the other hand,
898
+ in this case we can assume that the child's margins will collapse with the parent,
899
+ so the inner box will be ''200px'' tall,
900
+ exactly filling the parent.
893
901
894
902
<pre>
895
903
.outer { height: 200px; margin: 0; }
896
904
.inner { height: stretch; margin: 10px; }
897
905
</pre>
906
+
907
+ (The top margins will in fact collapse,
908
+ but the bottom margins do not collapse,
909
+ because the bottom margin of a box is not adjoining
910
+ to the bottom margin of a parent with a non-''height/auto'' height,
911
+ see [[CSS2/box#collapsing-margins]] .
912
+ Luckily, an overflowing bottom margin doesn't have any visible effect.)
898
913
</div>
899
914
900
915
<div class="example">
@@ -903,15 +918,15 @@ Stretch-fit Sizing: filling the containing block</h3>
903
918
(due to the 10px margin):
904
919
905
920
<pre class=html>
906
- <div class="outer ">
907
- <div class="inner ">text</div>
921
+ <div class="parent ">
922
+ <div class="child ">text</div>
908
923
</div>
909
924
some more text
910
925
</pre>
911
926
912
927
<pre>
913
- .outer { float: left; margin: 0; }
914
- .inner { width: stretch; margin: 10px; }
928
+ .parent { float: left; margin: 0; }
929
+ .child { width: stretch; margin: 10px; }
915
930
</pre>
916
931
</div>
917
932
@@ -922,8 +937,8 @@ Stretch-fit Sizing: filling the containing block</h3>
922
937
so ''height: stretch'' [=behaves as auto=] as well.
923
938
924
939
<pre>
925
- .outer { height: auto; margin: 0; }
926
- .inner { height: stretch; margin: 10px; }
940
+ .parent { height: auto; margin: 0; }
941
+ .child { height: stretch; margin: 10px; }
927
942
</pre>
928
943
</div>
929
944
0 commit comments