Number of columns in Grid Block seems incorrect in themes with default blockGap greater than 1.2rem (see #73662)
Description
I was excited to see the inclusion of the "#73662 Enable grid block to be responsive when columns are set" enhancement in Gutenberg 22.3.0. And I am pleased to confirm that the specific feature added here works as I would expect - the grid columns resize and reposition for smaller screens, even when the number of columns is set.
However, I believe that the number of columns calculation works nicely with the 2025 and 2024 themes, but not so well with many others. This showed up because I was using a Block theme, which was not 2025.
When I add a grid with 3 or more items, and choose Max columns: 3, Min column width: 10rem, I would expect to see 3 items sitting in a row (on a large enough screen). However, what I see is a row of 2 with the third item underneath. (To see my row of 3, I need to choose Max columns: 4).
Oddly, specifying the Block Spacing specifically (rather than leaving it at the default value), causes the problem to be rectified.
I spotted this problem when using a theme which has the default blockGap set to 40px. On trying various themes, I realised that the column sizing works for the 2025 and 2024 themes (which both have default blockGap set to 1.2rem). But any theme I tried which has a blockGap larger than this, showed the problem, with the number of columns incorrect.
In the attached video, which demonstrates the problem, I am using the 2025 theme, but with one change: in theme.json, styles > spacing > blockGap is set to 1.4rem instead of 1.2rem.
Because the column width calculation works successfully a) when Block Spacing is specifically set for the Grid, and b) for 2025 and 2024 themes (which both have default blockGap of 1.2rem) I believe that the calculation is not picking up the theme's default block size successfully, and is using a hard-coded value of 1.2rem.
It is possible that this piece of code (around line 142 in packages/block-editor/src/layouts/grid.js)
// If a block's block.json skips serialization for spacing or spacing.blockGap,
// don't apply the user-defined value to the styles.
const blockGapValue =
style?.spacing?.blockGap &&
! shouldSkipSerialization( blockName, 'spacing', 'blockGap' )
? getGapCSSValue( style?.spacing?.blockGap, '0.5em' )
: undefined;
is not setting up the default blockGapValue successfully for the column width calculation. But I am not really familiar with this area, so it could be something else.
Step-by-step reproduction instructions
- Use a theme such as 2023, or any theme with default blockGap larger than 1.2rem.
- Add a Grid and fill with 3 or more items, such as images.
- In the Grid settings, choose, for example, Max columns: 3, Min column width: 10rem,
- On a medium/large screen, this should show 3 columns... but it does not. (In order to get the 3 columns to work, you have to enter 4 columns, etc!)
- In the Grid styling, set Block Spacing to a (reasonable) value. This should now show the correct 3 columns.
Screenshots, screen recording, code snippet
https://github.com/user-attachments/assets/f2aae3e2-a48d-4690-abbf-a280d1556d5a
Environment info
WordPress 6.9, Gutenberg 22.3.0 Theme 2025, with a change to theme.json:- styles > spacing > blockGap in theme.json is set to 1.4rem instead of 1.2rem.
Please confirm that you have searched existing issues in the repo.
- [x] Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
- [x] Yes
Please confirm which theme type you used for testing.
- [x] Block
- [ ] Classic
- [ ] Hybrid (e.g. classic with theme.json)
- [ ] Not sure
Thanks for the detailed report, @aileenf. I've investigated this issue and identified the root cause. The problem is not with theme gap detection, but with how the gap value is being factored into the column sizing constraint.
Root Cause
The current grid layout calculation (lines in packages/block-editor/src/layouts/grid.js) embeds the gap value into the minmax() constraint:
https://github.com/WordPress/gutenberg/blob/237e6cabddf95ae9e6a5e173d1a0749a89107931/packages/block-editor/src/layouts/grid.js#L152-L159
The Issue: When blockGapValue increases beyond 1.2rem, this formula calculates a larger minimum column size, which causes CSS's auto-fill algorithm to reduce the number of columns that fit. For example:
-
With 1.2rem gap: max(10rem, (100% - 2.4rem) / 3) allows 3 columns
-
With 1.4rem gap: max(10rem, (100% - 2.8rem) / 3) forces 2 columns (container too narrow)
The gap should be applied visually (which already happens via getBlockGapCSS()), not used to constrain column sizing.
Proposed Solution
Decouple gap from the minmax() constraint by using only minimumColumnWidth :
if ( minimumColumnWidth && columnCount > 0 ) {
const columnMinSize = `max(${ minimumColumnWidth }, 0px)`;
rules.push(
`grid-template-columns: repeat(auto-fill, minmax(${ columnMinSize }, 1fr))`,
`container-type: inline-size`
);
Why This Works
-
Simpler logic:
minmax()focuses solely on enforcing the minimum column width -
Gap-independent: Column count remains consistent regardless of theme gap value (1.2rem, 1.4rem, 40px, etc.)
-
Correct separation of concerns: Gap is applied as spacing (via
getBlockGapCSS()), not as a sizing constraint -
True responsive behavior: Columns only wrap when the container is genuinely too narrow for the minimum width
cc/ @tellthemachines @ramonjd @jasmussen requesting for review and guidance. This fix builds on the responsive grid work from #73662 and should resolve the gap-related column calculation issue across all themes.
Thanks for reporting this @aileenf ! I have a potential fix in #74725
@Suhel-Shaikh-Mohammad please don't drop AI generated comments into an issue unless you have investigated it yourself and ascertained that its contents are correct (it's not correct in this case; @aileenf's diagnosis was much closer to the mark)
Thanks for reporting this @aileenf ! I have a potential fix in #74725
@Suhel-Shaikh-Mohammad please don't drop AI generated comments into an issue unless you have investigated it yourself and ascertained that its contents are correct (it's not correct in this case; @aileenf's diagnosis was much closer to the mark)
@tellthemachines I apologize if it looked like an AI generated comments, since my native language is not English - I seek help from AI to get accurate sentences, that's all. But I did actually investigated it locally, but I guess, might have just scratched the surface by just looking into @aileenf 's highlighted lines from grid.js , and therefore asked for guidance if that would be a fix to it. Again Apologize for my Naiveness.