/    Sign up×
Community /Pin to ProfileBookmark

CSS Grid – same number of children per row

Is is possible in CSS to ensure you always have the same number of items per grid row. I have the below code which fills the first row then spills the remaining items onto then next row. This leaves a gap as per this example.  https://jsfiddle.net/nzkiwis/zt4xepbf/9/

grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@AliyaaiiJul 14.2023(updated) — Hi guys The CSS Grid layout allows for flexible and dynamic positioning of elements on a webpage. By using the "grid-template-columns" property, you can ensure that each row has an equal number of children. This provides consistency and helps in creating responsive designs.
Copy linkTweet thisAlerts:
@SempervivumJul 16.2023 — I'm afraid that this isn't possible: If the number of items is a primary number it cannot distributed among more than one line without leaving a gap.
I order to beautify the look you might apply the item's background to the empty cells either.
Copy linkTweet thisAlerts:
@alicja12Jul 18.2023 — I order to beautify the look you might apply the item's background to the empty cells either.
Copy linkTweet thisAlerts:
@AliyaaiiJul 28.2023 — Hi guys
In CSS, it is indeed possible to ensure a consistent number of items per grid row by using the grid-auto-flow property. By setting it to dense, the grid will attempt to fill in any empty spaces with subsequent items, optimizing the layout and reducing gaps.
Copy linkTweet thisAlerts:
@anniesteuberMar 28.2024 — Yes, it is possible to ensure that you always have the same number of items per grid row in CSS. To achieve this, you can use the grid-auto-flow: dense; property in combination with grid-template-columns.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...