CSS grid: fr units, gaps and responsive columns
Reviewed 23 November 2026 · 7 min read · Written and maintained by Toolnexo
CSS grid lays items out in rows and columns at the same time, which makes it the tool for galleries, dashboards and whole page layouts. Its main trick is the fr unit, a share of the free space, and its best-known pattern is a responsive grid that needs no media queries. This guide explains both with sizes measured in a real browser, then covers gaps, row height and alignment. The values come from the CSS grid generator on this site, which runs in your browser, so nothing you build is uploaded.
Tracks, gaps and items
Setting display: grid turns an element into a grid container. grid-template-columns lists the width of each column, which grid calls tracks, and the children fill the cells from left to right and then row by row. Rows are created as needed. The gap property sets the space between rows and columns, and it is written row first, so gap: 8px 24px means 8 pixels between rows and 24 between columns. One value sets both.
The fr unit
One fr is a share of the space left after fixed sizes and gaps are taken out. Take a container 900 pixels wide with a 16-pixel gap and the columns 1fr 2fr 1fr. The two gaps use 32 pixels, so 868 pixels are left, which makes one fr 217 pixels. The columns measure 217, 434 and 217 pixels.
Fixed and flexible tracks mix freely. With 200px 1fr 2fr in the same container, 900 − 200 − 32 = 668 pixels are left. One fr is 222.67 pixels, so the columns are 200, 222.67 and 445.33 pixels wide. A fixed column keeps its size and the flexible ones share what remains.
repeat() and minmax()
repeat(3, 1fr) is shorthand for 1fr 1fr 1fr, which gives three equal columns, and it is what the generator writes in Equal columns mode. minmax(100px, 1fr) says a track is never narrower than 100 pixels and otherwise takes a share of the free space. Together they are the heart of the next pattern.
Responsive columns without media queries
The rule grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)) asks the browser to fit as many columns of at least 160 pixels as the width allows and to share out whatever is left. With a 16-pixel gap, here is what a real browser measured. At 900 pixels wide it makes 5 columns of about 167 pixels, since 5 columns and 4 gaps need 864 pixels and a sixth column would need 1,040. At 600 pixels it makes 3 columns of about 189 pixels. At 390 pixels, a phone width, it makes 2 columns of 187 pixels.
No media query is involved. The columns reflow every time the container width changes, which is why this pattern is a favourite for card lists and galleries. The Responsive mode of the generator writes it for you and lets you set the smallest column width.
auto-fit or auto-fill
Both create as many columns as fit. They differ when there are fewer items than columns. With auto-fill the empty columns are kept, so two items in a 900-pixel container stay small at about 167 pixels each, with empty tracks beside them. With auto-fit the empty tracks collapse to nothing, so the two items stretch to about 442 pixels each and fill the row. Choose auto-fit when you want items to grow into the free space, and auto-fill when you want them to stay on the same grid lines whatever the number of items.
Row height and alignment
- By default rows are as tall as their tallest item. Setting grid-auto-rows: 90px gives every row the same fixed height.
- justify-items places each item inside its cell along the row, and align-items places it down the column. The values are stretch (the default), start, center and end.
- Items stretch to fill their cell unless you change these, so a card with little text will still be as tall as its row.
Grid or flexbox?
Use grid when you need to control rows and columns together, as in a gallery, a dashboard or a page layout. Use flexbox when you have a single row or column of items that should size themselves along that line, such as a toolbar or a row of buttons. The two combine well: a grid for the page and flexbox inside each card.
Using the generator
- Choose Equal columns, Responsive or Custom. For Custom, type a track list such as 200px 1fr 2fr. A list the tool cannot read is rejected with a message.
- Set the column and row gaps, a fixed row height if you want one, and the alignment.
- Copy the CSS or the HTML. The preview updates as you type, and resizing the window shows the responsive columns reflow.
Put it into practice
CSS Grid Generator
Build a CSS grid with equal, responsive or custom columns, set the gaps, row height and alignment, see it live, and copy the CSS and HTML.
Runs in your browserCSS Flexbox Generator
Start from 6 everyday layouts like navbar or wrapping cards, or set direction, wrap, justify, align and gap yourself, then copy the CSS a…
Runs in your browserCSS Border Radius Generator
Round each corner with sliders or shape an organic blob, start from 11 presets like pill, leaf and egg, then copy the CSS or a Tailwind c…
Runs in your browserPX to REM Converter
Convert pixels to rem and back, plus em, pt, pc, inches, cm, mm, percent, vw and vh, using your own root font size, parent size and viewp…
Runs in your browserHow this guide is maintained
This page is reviewed when the related tools change. It describes what the published tools actually do: the tools run in your browser tab, so your files stay on your device. If you find something inaccurate, email [email protected] and it will be corrected.
Other guides
- How to merge PDFs without uploading them6 min · 17 November 2026
- OCR a scanned PDF: accuracy, limits and privacy7 min · 17 November 2026
- Compress a PDF without destroying readability6 min · 17 November 2026