Skip to content

Commit 633c049

Browse files
committed
[css-grid-1][masonry] Make intrinsic sizing not depend on placement #8206 #9041
1 parent fae0709 commit 633c049

File tree

1 file changed

+40
-79
lines changed

1 file changed

+40
-79
lines changed

css-grid-3/Overview.bs

+40-79
Original file line numberDiff line numberDiff line change
@@ -168,51 +168,36 @@ Line Name Resolution</h3>
168168
The [[css-grid-2#line-placement|line name resolution]]
169169
works exactly is in <a href="https://www.w3.org/TR/css-grid/">CSS Grid</a>.
170170

171-
<h3 id="grid-item-placement">
172-
Grid Item Placement</h3>
173-
174-
[=Grid items=] are placed using these steps:
175-
176-
<ol>
177-
<li>
178-
Place the items as if the [=masonry axis=]
179-
had ''grid-template-columns/none'' specified in that axis.
171+
<h3 id="track-sizing">
172+
Grid Axis Track Sizing</h3>
180173

181-
<li>
182-
For any items that were placed at the first (hypothetical) implicit line
183-
in the [=masonry axis=] in step 1,
184-
those items keep their placement in both axes as their final placement.
185-
They will be laid out first in each [=grid axis=] track
186-
and their [=grid axis=] placement is considered definite henceforth.
187-
All other items ignore their placement from step 1.
188-
Any item with a specified definite placement in the [=masonry axis=]
189-
that doesn't result in it being placed at the first implicit line
190-
in the [=masonry axis=]
191-
will be treated as having ''grid-area/auto''-placement in that axis.
174+
Track sizing works the same as in [[css-grid-2#algo-track-sizing|CSS Grid]],
175+
except that when considering which items contribute to intrinsic sizes:
176+
* All items explicitly placed in that track contribute, and
177+
* All items without an explicit placement contribute
192178

193-
<li>
194-
Place items using the [[#masonry-layout-algorithm|Masonry layout algorithm]] below.
195-
</ol>
179+
<div class="example">
180+
For example, suppose there are two columns in the [=grid axis=]
181+
and that
182+
* Items A, B, and C have no explicit placement.
183+
* Item D is explicitly placed into the first column.
196184

197-
(The reason for step 1 above is to determine which items contribute
198-
to intrinsic track sizing in the [=grid axis=];
199-
see [[#track-sizing]] below.)
185+
In this case, items A, B, C, and D all contribute to sizing the first column,
186+
<em>and</em> A, B, and C (but not D) contribute to the second column.
187+
</div>
200188

201-
<h3 id="track-sizing">
202-
Grid Axis Track Sizing</h3>
189+
In the case of spanning items with no explicit placement,
190+
their size contribution is divided by their spanning size.
203191

204-
Track sizing works the same as in [[css-grid-2#algo-track-sizing|CSS Grid]],
205-
except that that the algorithm is run only with
206-
the subset of items that have a known [=grid axis=] placement.
207-
This subset contains the items placed at the first implicit line in the [=masonry axis=]
208-
in <a href="#grid-item-placement">Grid Item Placement</a> above,
209-
plus the items that have a specified definite placement in the [=grid axis=],
210-
plus the items that span all [=grid axis=] tracks
211-
(which obviously will be placed at the first implicit line
212-
since there is no other way to place them).
213-
Other items will be placed in the [=grid axis=] by the [[#masonry-layout-algorithm]] below
214-
and don't contribute to intrinsic track sizing
215-
(because that would create circular dependencies).
192+
Note: This algorithm ensures that each track is at least big enough
193+
to accommodate every item that is ultimately placed in it,
194+
and does not create dependency cycles between placement and track sizing.
195+
However, depending on the variation in sizes,
196+
tracks could be larger than necessary:
197+
an exact fit is only guaranteed if
198+
all items are explicitly placed in the [=grid axis=]
199+
or all items are the same size
200+
(or matching multiples of that size, in the case of spanning items).
216201

217202
<h4 id="repeat-auto-fit">
218203
repeat(auto-fit)</h3>
@@ -234,60 +219,39 @@ Masonry Layout Algorithm</h3>
234219
Items are placed in [=order-modified document order=],
235220
but items with a definite placement are placed
236221
before items with an indefinite position (as in regular grid layout).
222+
237223
For each of the tracks in the [=grid axis=],
238224
keep a <dfn>running position</dfn> initialized to zero.
239-
For each item placed at the first implicit line in the [=masonry axis=]
240-
in <a href="#grid-item-placement">Grid Item Placement</a> step 1 above:
241-
225+
First for each item with a definite placement in the [=grid axis=],
226+
then for each item with an indefinite placement:
242227
<ol>
243228
<li>
244-
Position the item at the [=content edge=] of the [=grid container=] in the [=masonry axis=]
245-
and at its start track in the [=grid axis=].
246-
247-
<li>
248-
Calculate the size of the <a href="#containing-block">containing block</a>
249-
and then layout the item.
250-
Then, calculate its resulting [=margin box=] in the [=masonry axis=].
251-
Set the [=running position=] of the [=grid axis=] tracks the item spans
252-
to the maximum of <code>margin-box-end + grid-gap</code>
253-
and the current [=running position=] of those tracks.
254-
</ol>
255-
256-
Note: This means that items with a definite placement at line 1 in the [=masonry axis=]
257-
by <a href="#grid-item-placement">Grid Item Placement</a> step 1
258-
can be made to intentionally overlap.
259-
260-
The remaining items get their final placement using the following steps:
261-
<ol>
262-
<li>
263-
If the item has an indefinite placement in the [=grid axis=],
264-
then resolve its definite placement in the [=grid axis=] using these substeps:
229+
If the item has an definite placement in the [=grid axis=],
230+
use that placement.
231+
Otherwise, resolve its [=grid axis=] placement using these substeps:
265232

266233
<ol>
267-
<li>Starting at the first [=grid axis=] line in the implicit grid...
234+
<li>Starting at the first [=grid axis=] line in the [=implicit grid=].
268235
<li>Find the largest [=running position=] of the [=grid axis=] tracks
269236
that the item would span if it were placed at this line,
270-
and call this position <code>max_pos</code>.
237+
and call this position <var>max_pos</var>.
271238
<li>Increment the line number and repeat step 2
272239
until the item would no longer fit inside the grid.
273-
<li>Pick the line that resulted in the smallest <code>max_pos</code>
240+
<li>Pick the line that resulted in the smallest <var>max_pos</var>
274241
as the item's definite placement in the [=grid axis=].
275242
</ol>
276243

277-
Otherwise, a definite placement in the [=grid axis=] is honored
278-
the same as in a regular grid.
279-
280244
<li>
281-
Position the item at its [=grid axis=] start track
282-
and the maximum of the [=running position=]s
283-
of the tracks it spans.
245+
Place the item in its [=grid axis=] tracks
246+
at the maximum of the [=running position=]s
247+
of the tracks it spans.
284248

285249
<li>
286250
Calculate the size of the item's <a href="#containing-block">containing block</a>
287251
and then layout the item.
288252
Then calculate its resulting [=margin box=] in the [=masonry axis=].
289-
Set the [=running position=] of the [=grid axis=] tracks the item spans
290-
to <code>max_pos + margin-box-end + grid-gap</code>.
253+
Set the [=running position=] of the spanned [=grid axis=] tracks
254+
to <code><var>max_pos</var> + <var>margin-box-end</var> + <var>grid-gap</var></code>.
291255
</ol>
292256

293257
<h3 id="masonry-auto-flow">
@@ -363,10 +327,7 @@ The Implicit Grid</h2>
363327
The [=implicit grid=] is formed in the same way as for a regular [=grid container=].
364328
However, it's only used in the [=grid axis=].
365329
The flow axis specified by ''grid-auto-flow'' is ignored:
366-
items are always placed by filling the [=grid axis=]
367-
(though note that the ''grid-auto-flow'' property's ''grid-auto-flow/dense'' keyword
368-
does have an effect in determining which items end up at line 1 in the [=masonry axis=],
369-
in [[#grid-item-placement]] step 1).
330+
items are always placed by filling the [=grid axis=].
370331
''direction:rtl'' reverses the grid if the [=inline axis=] is the [=grid axis=]
371332
(as usual for a regular [=grid container=])
372333
and it makes items flow from right to left if the [=inline axis=] is the [=masonry axis=].

0 commit comments

Comments
 (0)