Senior 5 min · March 05, 2026

CSS Grid — `min-width: auto` Breaks Your fr Units

Grid items overflow containers because fr units ignore min-width:auto defaults.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • CSS Grid defines a two-dimensional layout system where you create columns and rows, then place items into cells.
  • Grid container: element with display: grid; Grid items: direct children.
  • Track sizing: grid-template-columns, grid-template-rows accept px, fr, %, auto, minmax(), etc.
  • Item placement: grid-column, grid-row using line numbers or spans.
  • Performance: Grid avoids layout reflows for most changes — browser caches the grid structure.
  • Production pitfall: Implicit grid auto-generates rows/columns with auto size, often causing overflow when content is larger than expected.
  • Biggest mistake: Forgetting to set min-height: 0 on grid items when using 1fr inside nested grids — causes item to grow beyond container.
Plain-English First

Imagine you're arranging furniture in a room. You first draw a floor plan on graph paper — marking out rows and columns — then you decide which piece of furniture goes in which square. CSS Grid is exactly that floor plan for your webpage. You define the 'grid' (the columns and rows), then you place your content (the furniture) into it. Unlike older CSS methods that stack things like a single filing cabinet, Grid lets you control both directions — horizontal AND vertical — at the same time.

You've hit the same wall too many times. It's time to stop fighting floats. CSS Grid was built for this.

Every web developer hits the same wall: you have a beautiful design mockup with a sidebar, a hero image, a content area, and a footer — and you spend three hours fighting floats, clearfixes, and position hacks trying to make it real. That wall exists because CSS was originally designed for documents, not applications. Modern UIs need two-dimensional control, and for the longest time, we were building them with one-dimensional tools. CSS Grid was built specifically to tear that wall down. It's the first CSS layout system that was designed with complex, two-dimensional layouts in mind from day one — not bolted on as an afterthought.

Here's the catch: senior engineers know that Grid's raw power comes with sharp edges. The fr unit looks simple until your items overflow. Implicit tracks behave differently than you expect. And grid-auto-flow: dense can silently break your tab order. This guide doesn't just repeat the docs — it walks through the production failures that surprise even experienced developers and shows you how to avoid them.

What is CSS Grid?

CSS Grid is a two-dimensional layout system that lets you control both columns and rows simultaneously. Unlike Flexbox, which is one-dimensional, Grid gives you a graph-paper-like grid where you can place items precisely. It's the first CSS layout system designed for complex page layouts, not just documents.

simple-grid.htmlHTML
1
2
3
4
5
<div class="container" style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px;">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
Key insight
Any element with display: grid becomes a grid container. Its direct children become grid items. That's it. No extra wrappers needed.
Production Insight
Grid is supported in all modern browsers since 2017.
But watch out: older browsers (IE11) require the -ms- prefix.
Use Autoprefixer in your build step to handle that.
Otherwise, your layout breaks for 5% of users.
Key Takeaway
Grid is the only native two-dimensional layout system.
Think in terms of rows AND columns at the same time.
That's the mental shift from Flexbox.

Defining the Grid: Columns, Rows & Track Sizing

Every CSS Grid starts with a container element set to display: grid. Then you define the column and row tracks using grid-template-columns and grid-template-rows. You can use absolute units (px, em), flexible units (fr — fraction of free space), or content-based sizing (auto, min-content, max-content). The minmax() function combines minimum and maximum sizes — essential for responsive layouts without media queries. For example, grid-template-columns: repeat(3, minmax(200px, 1fr)) creates three columns that each shrink to 200px before wrapping (with auto-fill) or staying flexible.

grid-container.cssCSS
1
2
3
4
5
6
.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 16px;
}
Mental model: graph paper
  • Each value creates one track (column or row) in order.
  • 1fr means 'give this track one fraction of the remaining space' — tracks with more fr get proportionally bigger.
  • auto sizes to fit the content — like a wildcard.
  • minmax(200px, 1fr) ensures the track never goes below 200px but grows up to fill space.
  • Implicit tracks (automatic) default to auto — you'll see them later.
Production Insight
You drop a 300px button into a 1fr column. The column won't shrink below 300px. Layout break.
Root cause: min-width: auto on grid items.
Fix: set min-width: 0 on the item.
We saw this on a dashboard — a 'Submit' button pushed the sidebar off-screen.
Key Takeaway
The fr unit is a fraction of free space — not a percentage.
Implicit tracks (columns/rows you didn't define) default to auto size.
Always set min-width: 0 on items when using fr to avoid overflow.
Choosing track sizing
IfFixed layout with known column widths
UseUse px, em, or % — no flexibility needed.
IfDashboard with flexible sidebars and main area
UseUse fr units: 1fr 3fr 1fr for sidebar-main-sidebar.
IfCard grid that should wrap to fit container width
UseUse repeat(auto-fill, minmax(250px, 1fr)) — responsive without media queries.
IfHeader and footer fixed height, content fills remaining space
UseUse grid-template-rows: auto 1fr auto — classic holy grail.

Placing Grid Items with Line Numbers and Span

Once you have a grid, every direct child becomes a grid item. By default, items auto-place one per cell, left-to-right, top-to-bottom. But you can explicitly place items using grid-column and grid-row. The syntax is grid-column: start-line / end-line. Lines are numbered from 1 at the top-left. Negative numbers count from the end (-1 is the last line). To span multiple tracks, use the span keyword: grid-column: span 2. You can also use named lines (defined in grid-template-columns). Overlapping items are allowed — create layered layouts by setting z-index. When items overlap, the browser renders them in DOM order by default.

grid-placement.cssCSS
1
2
3
4
5
6
7
8
9
10
11
.layout {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
}

.header { grid-column: 1 / -1; }
.sidebar { grid-row: 2 / 3; }
.main { grid-column: 2 / 3; }
.footer { grid-column: 1 / -1; }
Common line number mistake
Line numbers start at 1, not 0. grid-column: 1 / span 2 means start at line 1 and cover the next two tracks (columns 1 and 2). grid-column: 1 / 3 is the same. Many devs write grid-column: 0 / 2 — that's invalid and the property is ignored.
Production Insight
Overlapping grid items can cause accessibility issues — screen readers follow DOM order, not visual order.
On small screens, explicit placements may break the logical flow.
Don't rely on visual order for key interactions.
Alternative: Use auto-placement and reorder with order property only if semantics allow.
Key Takeaway
Explicit placement gives pixel-perfect control but breaks when content changes.
Auto-placement is more resilient — use it for dynamic content.
Rule: For layout structure (header, footer, etc.), be explicit. For content, be implicit.
When to use explicit placement vs auto-placement
IfKnown layout structure (header, sidebar, main, footer)
UseExplicit placement: set grid-column and grid-row on structural elements.
IfUnknown number of cards or dynamic list
UseAuto-placement: just set grid-template-columns and let items flow in.
IfNeed to overlap two items (e.g., badge on top of image)
UseExplicit placement: place both items in same cell using grid-column: 1 / 2; grid-row: 1 / 2.

Named Grid Areas: Cleaner Layouts

When you have a well-defined layout, naming grid areas makes your CSS read like a blueprint. Use grid-template-areas on the container with a visual grid of strings. Each string represents one row, each word represents a cell. Use a period . to leave a cell empty. Then assign each item to an area with grid-area: name. This approach is much easier to maintain than line numbers — you can rearrange the entire layout by swapping words in the template.

grid-areas.cssCSS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Quick tip: responsive grid areas
Use @media queries to redefine grid-template-areas. For mobile, change to a single column: "header" "main" "sidebar" "footer". No need to move items — just update the container's template.
Production Insight
Named areas are forgiving if you misspell — the browser ignores the rule entirely.
The template must be a perfect rectangle — each row must have the same number of columns.
Ragged grids (staggered spans) can't be expressed with areas; use line numbers instead.
Key Takeaway
grid-template-areas is the most readable way to define layouts.
It forces a rectangular grid — any hole must be filled with a period ..
For complex spanning, fall back to line numbers.

Auto-Placement and the Implicit Grid

When you define only columns but not rows, or the number of items exceeds defined tracks, the browser creates implicit tracks — auto-generated rows or columns. The sizing of these implicit tracks is controlled by grid-auto-rows and grid-auto-columns. By default, grid-auto-rows: auto sizes rows to fit content, which can cause uneven heights in a grid of cards. Set grid-auto-rows: 1fr to force equal height, but beware of overflow again. grid-auto-flow controls the placement direction: row (default, fill left-to-right then wrap), column (top-to-bottom then wrap), dense (pack items to fill gaps — but can reorder DOM).

auto-placement.cssCSS
1
2
3
4
5
6
7
8
9
10
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-auto-rows: 1fr;  /* all cards same height */
  gap: 16px;
}

.card {
  /* items placed automatically */
}
Dense packing and source order
grid-auto-flow: dense reorders items to fill gaps in the grid. This improves visual density but breaks tab order on the page. Screen readers follow DOM order, not visual order. Never use dense for content that needs logical reading order (e.g., articles, forms).
Production Insight
Implicit tracks default to auto height — if one card has more text, that row stretches.
To make all cards equal height, set grid-auto-rows: 1fr but add min-height: 0 to each card or use align-items: stretch (default).
For cards with images, the image may overflow its cell — wrap it in a container with overflow: hidden and height: 100%.
Key Takeaway
Implicit rows are created automatically — control their size with grid-auto-rows.
dense packing fills gaps but reorders DOM — use only for decorative content.
For equal height cards, use 1fr on grid-auto-rows and handle overflow.
Auto-placement strategy
IfCards of varying content length that should all be same height
Usegrid-auto-rows: 1fr + min-height: 0 on items.
IfGallery of images with same aspect ratio
Usegrid-auto-rows: 200px (fixed height) + object-fit: cover on images.
IfContent in natural reading order, gaps accepted
UseDefault grid-auto-flow: row — no dense packing.
IfFluid layout that packs items tightly regardless of order
Usegrid-auto-flow: dense — but only if visual density trumps accessibility.

Alignment: Items, Content, and Tracks

CSS Grid provides powerful alignment along both axes. On the container, justify-items (horizontal) and align-items (vertical) align all items within their cells. Default is stretch — items fill the cell. Use start, end, center to change. justify-content and align-content align the entire grid within the container — useful when the total grid size is smaller than the container (e.g., content tracks smaller than container height). For individual items, use justify-self and align-self to override the container setting. These properties work exactly the same as in Flexbox but apply to the cell, not the main axis.

grid-alignment.cssCSS
1
2
3
4
5
6
7
8
9
10
11
12
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;         /* vertical center within cells */
  justify-items: start;        /* horizontal left-align */
  height: 400px;
}

.special-item {
  align-self: end;             /* override for this item */
  justify-self: stretch;
}
Justify vs Align — which is which?
  • justify-items: horizontal alignment of items within cells.
  • align-items: vertical alignment of items within cells.
  • justify-content: horizontal alignment of the whole grid within the container.
  • align-content: vertical alignment of the whole grid within the container.
  • place-items: center center; sets both justify and align in one property.
Production Insight
Using align-items: stretch (default) works well unless you have fixed-height rows.
When you set grid-template-rows: 200px 200px, items inside are stretched to 200px — if content is taller, it overflows.
Fix: set align-items: start if you want items to size to content, or use min-height: 0 and overflow: auto on items.
Key Takeaway
Alignment properties are the same as Flexbox but applied per cell.
place-items is shorthand for both axes — use it cleanly.
Stretch default causes overflow when content is larger than the cell — anticipate that.

Responsive Grids Without Media Queries

One of CSS Grid's greatest features is creating responsive layouts that adapt to available space without a single @media rule. Use repeat(auto-fill, minmax(250px, 1fr)) — now the number of columns depends on the container width. auto-fill creates as many tracks as possible, even if some are empty. auto-fit collapses empty tracks, allowing items to stretch wider. For a card layout that goes from 1 column on mobile to 4 on desktop, just set minmax(250px, 1fr) — the browser does the math. Combine with grid-auto-rows to keep cards equal height.

responsive-grid.cssCSS
1
2
3
4
5
6
7
8
.responsive-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
  padding: 16px;
}

/* No media queries needed */
auto-fill vs auto-fit
auto-fill keeps empty column tracks — if container is 600px and minmax is 250px, you get 2 columns + leftover space (maybe 100px). auto-fit collapses leftover space into the existing columns, making them wider. Use auto-fit when you want items to grow, auto-fill when you want strict column widths.
Production Insight
Responsive grids with minmax() are amazing, but they can cause unexpected wrapping.
If the container is just a few pixels narrower than 2 * min width, you go from 2 columns to 1 column suddenly.
Set a @media breakpoint to increase the minmax minimum at larger screens for smoother transitions.
Key Takeaway
repeat(auto-fill, minmax(250px, 1fr)) is the single most powerful responsive pattern.
auto-fit collapses empty tracks, auto-fill preserves them — know the difference.
Precise control? Use media queries. Fluid flexibility? Use auto-fill.

Grid Layout Performance and Browser Rendering

CSS Grid is highly optimised in modern browsers, but it's not free. When you change grid properties like grid-template-columns, the browser must recalculate the entire grid layout, which triggers a layout reflow. Unlike Flexbox, where a single item change may only affect its immediate siblings, a grid change can affect all items in the grid. For static grids (those that don't change dynamically), Grid performs better than older methods because the browser precomputes track sizes. However, if you programmatically add or remove items in a grid with auto-fill or auto-fit, the browser re-evaluates the number of columns, which can be expensive on large grids. The best practice: avoid changing grid properties in rapid succession (e.g., during animations or scroll events). If you need to animate item positions, use transform or opacity instead of changing grid placement.

grid-performance.cssCSS
1
2
3
4
5
6
7
8
9
10
11
12
13
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  transition: grid-template-columns 0.3s; /* Not recommended — causes full reflow */
}

/* Better: animate individual items with transforms */
.grid-item {
  transition: transform 0.3s;
}
.grid-item:hover {
  transform: scale(1.05);
}
Performance tip for dynamic grids
If you must change grid template at runtime (e.g., user toggles between 2 and 3 columns), do it via adding/removing a CSS class instead of inline style modifications. The browser optimises class-based changes better because it can batch the style recalc.
Production Insight
Changing grid-template-columns on a grid with hundreds of cards can freeze the UI for hundreds of milliseconds.
On mobile devices, this is even worse — each layout recalculation is more expensive due to limited CPU.
Rule: keep grid structure static; mutate content, not layout.
Key Takeaway
Grid layout recalculations are expensive — avoid changing grid properties often.
Animate with transforms, not grid properties.
For large grids, prefer CSS class toggling over inline style changes.

Subgrid and Nested Grid Alignment

Subgrid lets a grid item that is itself a grid container inherit the track definitions from its parent. This means nested items can align with the parent's grid lines. It's ideal for repeating patterns like product cards where you want the title of each card to align horizontally across rows, or for tabular data where headers and cells should align. However, subgrid is only supported in Firefox and Safari as of 2026. Chrome has it behind a flag. For production use, you may need a fallback using explicit line numbers and matching track sizes. The subgrid value is applied to grid-template-rows or grid-template-columns on a nested grid.

subgrid-example.cssCSS
1
2
3
4
5
6
7
8
9
10
11
.parent-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto 1fr auto;
}

.card {
  display: grid;
  grid-template-rows: subgrid; /* inherits parent's row tracks */
  grid-row: span 1; /* each card occupies one row */
}
How subgrid works
  • Subgrid inherits the size of the parent's track, not the content of the nested item.
  • You can subgrid columns, rows, or both.
  • The nested grid's gap is applied on top of the parent's gap.
  • Subgrid does not work with non-rectangular shapes (like spanning partial tracks).
Production Insight
Subgrid is not fully supported in all browsers. If you rely on it, test in Firefox first.
In Chrome, you'll need to enable the Experimental Web Platform features flag.
Fallback: manually replicate parent track sizes using grid-template-rows: repeat(3, minmax(0, 1fr)) and hope content doesn't overflow.
Key Takeaway
Subgrid lets nested grids align with parent tracks.
It's for alignment, not layout — use it sparingly.
Check browser support before shipping.
When to use subgrid
IfNeed precise alignment of nested items across parent rows (e.g., card titles align horizontally)
UseUse subgrid for the nested grid's rows.
IfYou must support Chrome without flags
UseAvoid subgrid. Use explicit line numbers and matching track definitions.
IfNested grid only needs alignment within its own cell, not across cells
UseNo need for subgrid. Normal grid nesting works fine.

Grid and Accessibility: Source Order vs Visual Order

One of the most overlooked patterns when using CSS Grid is the impact on accessibility. Screen readers and keyboard navigation follow the DOM order, not the visual order. When you use explicit placement or grid-auto-flow: dense, you can create a visual layout that is completely different from the source order. This breaks tab order for keyboard users and confuses screen reader users. The rule: if the content has a meaningful reading order (articles, forms, navigation), keep the DOM order matching the visual order. Use Grid's placement only for decorative reordering or when you've tested with a screen reader.

accessible-grid.htmlHTML
1
2
3
4
5
6
<div class="card-grid">
  <!-- Source order matches visual order: left to right, top to bottom -->
  <article class="card">First story</article>
  <article class="card">Second story</article>
  <article class="card">Third story</article>
</div>
Dense breaks accessibility
grid-auto-flow: dense reorders items to fill gaps — it's the #1 grid property that breaks tab order. Never use it on content that has a logical sequence. For photo galleries without captions, it's acceptable.
Production Insight
We had a client dashboard where the 'Save' button visually appeared first but was last in DOM.
Keyboard users tabbed through all form fields before reaching Save — they kept submitting forms that weren't complete.
Fix: reorder the DOM to match the visual layout, not the other way around.
Grid's visual reordering is for design, not for interaction.
Key Takeaway
DOM order determines keyboard and screen reader order — not visual order.
Use explicit placement only when the source order is already correct.
Test with keyboard navigation before shipping any grid layout.
● Production incidentPOST-MORTEMseverity: high

Grid Items Overflowing Outside Container

Symptom
Grid items spill outside the container's boundary, horizontal scrollbar appears even though overflow: hidden is set.
Assumption
Developers assume fr units automatically respect the container's size limits, but 1fr means 'take remaining space', and if content inside the item is larger than that space, it overflows.
Root cause
The grid item's content has a fixed width or min-width larger than the available space. The fr unit distributes free space, but items themselves can grow beyond via min-width: auto (default).
Fix
Add min-width: 0 to the overflowing grid item. This overrides the default min-width: auto for grid items, allowing the item to shrink below its content size.
Key lesson
  • Always set min-width: 0 on grid items when using fr units if the item contains content with intrinsic width (images, long text, inputs).
  • Use overflow: hidden on the grid container only as a last resort — it hides the symptom, doesn't fix the cause.
  • Test layouts with real content, not placeholder text.
Production debug guideSymptom → Action for common grid failures6 entries
Symptom · 01
Items are not placed where expected, they stack in a single column.
Fix
Check that display: grid is set on the parent container. Verify grid-template-columns has multiple values, e.g. repeat(3, 1fr).
Symptom · 02
Gaps between items are larger or smaller than expected.
Fix
Inspect the gap property on the grid container. Shorthand gap sets both row-gap and column-gap. Ensure you haven't accidentally used grid-gap (old name, still works but confusing).
Symptom · 03
Item overflows the grid cell and overlaps adjacent items.
Fix
Add min-width: 0 or overflow: hidden to the overflowing item. Check if the item has a fixed width, padding, or border that pushes it out.
Symptom · 04
Grid rows don't resize with content — content clips or gaps appear.
Fix
If using grid-template-rows: 1fr 1fr, the rows share equal height regardless of content. Switch to auto or use minmax() for flexible sizing.
Symptom · 05
Responsive grid doesn't change number of columns on smaller screens.
Fix
If using repeat(auto-fill, minmax(250px, 1fr)), ensure the container has a width less than the minmax threshold. Also check that auto-fill vs auto-fit is appropriate — auto-fit collapses empty tracks.
Symptom · 06
Named grid areas have no effect — items are placed incorrectly.
Fix
Check spelling of area names: the string must match exactly between grid-template-areas and grid-area. Also ensure the number of columns in each row string is identical.
★ CSS Grid Debugging Cheat SheetUse browser DevTools to inspect and fix grid layouts fast.
Grid layout not applying at all.
Immediate action
Open DevTools and inspect the container element. Look for `display: grid` in the Styles panel.
Commands
Check for CSS overrides: Inspect the computed styles to see if another rule overwrites `display`.
Look for parent container with `display: block` that breaks grid context.
Fix now
Set display: grid !important; temporarily to verify.
Grid lines are not visible — hard to debug placement.+
Immediate action
In DevTools, find the grid container in the Elements panel. Click the grid badge (dotted square icon).
Commands
Under Layout tab, enable 'Show grid line numbers' and 'Show track sizes'.
Also enable 'Area names' if you use named areas.
Fix now
Toggle 'Hide grid overlays' to see the grid structure overlaying the page.
Item placed incorrectly with `grid-column` or `grid-row`.+
Immediate action
Check the line numbers: grid lines start at 1, not 0. Negative line numbers count backwards.
Commands
In DevTools Layout panel, read the line numbers for the grid.
Add temporary borders to grid items: `outline: 1px solid red`.
Fix now
Set grid-column: 1 / -1; to force full-width — if it works, the line values were wrong.
`minmax()` causes unexpected wrapping.+
Immediate action
Check the container's width in DevTools. The minmax minimum + gap may be just enough to force fewer columns.
Commands
Temporarily set `grid-template-columns: repeat(auto-fill, minmax(1px, 1fr))` to see if containers are being created.
Use the Layout panel to see track sizes and how many columns actually exist.
Fix now
Adjust the minmax minimum value to a lower value or use auto-fit instead of auto-fill.
Grid vs Flexbox
ScenarioCSS GridFlexbox
2D layout (rows & columns)Perfect — designed for both axesWorkaround — nested flexes needed, fragile
One-dimensional row of itemsOverkill — you get full 2D control but simpler tools existNatural fit — display: flex with gap
Centering a single elementPossible (place-items: center) but verboseTrivial — align-items: center; justify-content: center
Card grid with unknown number of itemsauto-fill handles it beautifullyManual wrapping via flex-wrap and width calc
Holy grail layout (header, sidebar, main, footer)Ideal — grid-template-areas defines structure clearlyPossible but requires nested flexes and explicit order
Content dictates column sizes (shrink-to-fit)Not its strength — Grid prefers distributing spaceBetter — flex: 1 0 auto gives flexibility
Overlapping itemsNative — overlapping is allowed and controllableNot possible without absolute positioning

Key takeaways

1
CSS Grid is the only native two-dimensional layout system
structure before content.
2
Always set `min-width
0 on items when using fr` units to prevent overflow.
3
auto-fill preserves empty tracks; auto-fit collapses them
use appropriately.
4
Explicit placement for structure; auto-placement for dynamic content.
5
`grid-auto-flow
dense` breaks keyboard accessibility — use only for decorative grids.
6
Subgrid is powerful but not fully supported
have a fallback plan.
7
Test with real content and keyboard navigation before shipping any grid layout.

Common mistakes to avoid

7 patterns
×

Memorising syntax before understanding the concept

Symptom
You can write grid-template-columns: repeat(3, 1fr) but can't explain why items overflow or how to make equal-height cards.
Fix
Start with the mental model (graph paper). Then practice by changing values in DevTools and observing the effect.
×

Skipping practice and only reading theory

Symptom
You know the property names but when given a real layout, you spend hours guessing which combination works.
Fix
Build a simple page (like a profile card grid) with Grid. Then convert a Flexbox layout to Grid to understand differences.
×

Not setting `min-width: 0` on grid items using `fr`

Symptom
Items overflow the grid container, especially when they contain long text, images, or inputs.
Fix
Add min-width: 0 to the grid item's CSS. This overrides the default min-width: auto for grid items.
×

Using `grid-auto-flow: dense` without considering accessibility

Symptom
The visual order is clean, but screen readers jump around unpredictably, and tab order is broken.
Fix
Only use dense for purely decorative galleries or when there's no meaningful source order (e.g., image wall). For content, use row or column.
×

Forgetting that `gap` only works inside a grid or flex container

Symptom
You set gap: 16px on a div that is not a grid or flex, but no spacing appears.
Fix
Ensure the parent has display: grid or display: flex before using gap.
×

Using `1fr` for all columns without considering minimum content sizes

Symptom
Columns become too narrow on small screens, and text overflows or wraps awkwardly, or the layout breaks entirely.
Fix
Use minmax() for each column: grid-template-columns: minmax(200px, 1fr) minmax(300px, 2fr) minmax(200px, 1fr). This ensures a minimum width before fr takes over.
×

Confusing `auto-fill` and `auto-fit`

Symptom
With auto-fill, you see empty column tracks that leave whitespace; with auto-fit, items unexpectedly stretch wider than expected.
Fix
If you want strict column count with potentially empty space, use auto-fill. If you want items to expand to fill the available space, use auto-fit. Inspect DevTools to see the difference.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
Explain the difference between `justify-items`, `align-items`, `justify-...
Q02JUNIOR
How do you create a responsive grid that shows one column on mobile and ...
Q03SENIOR
What is the implicit grid and how do you control its sizing?
Q04SENIOR
How do you center a grid item both horizontally and vertically inside it...
Q05SENIOR
How does `subgrid` work and when would you use it?
Q06JUNIOR
What's the difference between `auto-fill` and `auto-fit` in `repeat()`?
Q07SENIOR
How does CSS Grid handle overflow when content is larger than the grid c...
Q01 of 07SENIOR

Explain the difference between `justify-items`, `align-items`, `justify-content`, and `align-content` in CSS Grid.

ANSWER
justify-items aligns items along the inline (horizontal) axis within their cell. align-items aligns along the block (vertical) axis. justify-content aligns the entire grid along the inline axis when the grid is smaller than the container, distributing extra space between tracks. align-content does the same for the block axis. The -items properties align items inside cells, the -content properties align the tracks themselves. place-items is shorthand for both align-items and justify-items.
FAQ · 5 QUESTIONS

Frequently Asked Questions

01
Is CSS Grid supported in all browsers?
02
Does CSS Grid replace Flexbox?
03
How do I make a grid item span multiple rows?
04
Should I use `grid-gap` or `gap`?
05
Why are my grid columns not wrapping on smaller screens?
🔥

That's HTML & CSS. Mark it forged?

5 min read · try the examples if you haven't

Previous
CSS Flexbox Complete Guide
4 / 16 · HTML & CSS
Next
Responsive Design and Media Queries