NEW321928
Subgrid ignores the space justify-content adds when comparing its gap to the parent's
https://bugs.webkit.org/show_bug.cgi?id=321928
Summary Subgrid ignores the space justify-content adds when comparing its gap to the ...
Karl Dubost
Reported 2026-08-17 06:22:26 PDT
Created attachment 481077 [details] testcase.html A subgrid that sets its own gap ends up in the wrong place when the parent grid spreads leftover space with justify-content. <div class="grid"> <div>one</div><div>two</div><div>three</div> <div class="subgrid"><div>a</div><div>b</div><div>c</div></div> </div> .grid { width: 400px; display: grid; grid-template-columns: 100px 100px 100px; gap: 16px; justify-content: space-between; } .subgrid { grid-column: 1 / -1; display: grid; grid-template-columns: subgrid; gap: 16px; } The subgrid's three columns should start at 0, 133 and 283. Safari puts them at 0, 116 and 232. Chrome and Firefox both give 0, 133 and 283. The parent's gap is 16px as written, but 50px as used, because space-between adds 34px to each one. Safari compares the subgrid's 16px against the 16px instead of against the 50px, so the adjustment that should pull the inner columns 17px outward never happens.
Attachments
testcase.html (13.03 KB, text/html)
2026-08-17 06:22 PDT, Karl Dubost
no flags
Radar WebKit Bug Importer
Comment 1 2026-08-17 06:22:33 PDT
Karl Dubost
Comment 2 2026-08-17 06:31:47 PDT
Ran the attached testcase in three browsers. * Chrome 153 and Firefox 156 pass all four cases. * Safari 27.0 passes both controls and fails both of the others. Chrome 153 Firefox 156 Safari 27.0 control 1 no subgrid, parent spreads PASS PASS PASS control 2 subgrid gap:16px, no spreading PASS PASS PASS case 1 subgrid gap:16px, parent spreads PASS PASS FAIL case 2 subgrid gap:0, parent spreads PASS PASS FAIL Case 1, start and width of each of the subgrid's three columns: Chrome 153 0/117 133/134 283/117 Firefox 156 0/117 133/134 283/117 Safari 27.0 0/100 116/100 232/100 Case 2, the subgrid at gap:0, where its columns should meet flush and fill all 400px: Chrome 153 0/125 125/150 275/125 Firefox 156 0/125 125/150 275/125 Safari 27.0 0/108 108/116 224/108 The other two engines agree with each other to the pixel on every case, widths included. Both controls pass in all three.
Karl Dubost
Comment 3 2026-08-17 06:35:16 PDT
Where the numbers come from. The spec says the space justify-content adds between tracks counts as part of the gap: https://drafts.csswg.org/css-grid-2/#gutters and that half the difference between the subgrid's gap and the parent's is applied as extra, possibly negative, margin to the items that are not at the subgrid's own edges: https://drafts.csswg.org/css-grid-2/#subgrid-gaps So 1. the parent's gap is 50px. 2. The subgrid asks for 16px. 3. The difference is 34px, half of that is 17px, 4. and each inner column edge grows 17px outward into the parent's gap. Then 150 becomes 133 and 300 becomes 283. The comparison itself works; only its input is wrong. Case 2 in the testcase sets the subgrid to gap:0 and the columns come out 108, 116, 108 wide. Those are 100 plus 8, and 8 is half of 16. So the right sum is being done against the wrong number. Against the 50px the parent actually used, half would be 25, giving 125 and 150. That rules out the adjustment simply being missing. The comparison is in GridTrackSizingAlgorithm::copyUsedTrackSizesForSubgrid(), a single subtraction of the parent's gap from the subgrid's. Both other engines compare against the gap the parent used. * Gecko reads it back out of the parent's finished column positions, in nsGridContainerFrame::Tracks::AlignJustifyContent(). * Blink adds the spread space into its gap value first, in GridTrackSizingAlgorithm::ComputeFirstSetGeometry(), and compares against that. Not fixed by bug 321024. That one is the parent's spread space never reaching the subgrid at all, and its patch is scoped to the case where the subgrid has no gap of its own, where there is nothing to compare. With that patch applied the case here gives 0, 150, 300 (still wrong, but wrong by one steady amount instead of an error that grew across the columns.) No WPT covers this. css-grid/subgrid/grid-gap-006.html is closest, but its container overflows on purpose, so nothing is left over to spread and the gap never grows.
Note You need to log in before you can comment on or make changes to this bug.