DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

CSS Grid auto-fill vs auto-fit: Keep Empty Slots or Expand Cards?

CSS Grid auto-fill keeps empty repeated tracks; auto-fit collapses them after placement, allowing occupied flexible tracks to use the freed space.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a responsive CSS Grid seems to have invisible columns, empty repeated tracks may still be taking up room. The key difference in auto-fill vs auto-fit is what happens after grid items are placed: auto-fill keeps empty slots, while auto-fit collapses them so occupied flexible tracks can use the freed space.

How auto-fill and auto-fit choose the number of columns

Both keywords are used inside repeat() and initially calculate repeated tracks the same way. When the grid container has a definite size—or a maximum size that can be used for the calculation—the browser fits the largest number of tracks that will not overflow. When the relevant size is indefinite in the cases defined by the Grid specification, the auto-repeat count falls back to one.

As an Amazon Associate I earn from qualifying purchases.

The count is only the first part of the behavior. Once grid items are placed, auto-fill retains unused repeated tracks; auto-fit collapses them. MDN describes the distinction this way: “The auto-fit value behaves the same as auto-fill, except that after placing the grid items any empty repeated tracks are collapsed.” (MDN Web Docs: repeat() CSS function)

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What happens to empty tracks and gaps?

Behavior after placement auto-fill auto-fit
Empty repeated tracks Remain in the grid and reserve their space. Collapse if empty.
Gutters beside collapsed tracks Remain as part of the grid’s gaps. Collapse along with the track.
Occupied flexible tracks Do not gain space from empty tracks, because those tracks remain. Can expand into space no longer occupied by collapsed tracks.

For this rule, an empty track has no in-flow grid item placed in it or spanning across it. A collapsed track behaves as a fixed 0px track, and its neighboring gutters collapse too. These details explain why the two layouts can look identical when all fitted tracks have content but diverge when there are fewer items than available slots. (W3C CSS Grid Layout Module Level 1)

Which keyword should you use?

Choose auto-fill to reserve open slots

Use auto-fill when keeping the grid’s potential columns matters more than stretching the current items. Empty tracks remain available as reserved space, which can be useful when the layout is meant to preserve slots for items that may appear later.

Choose auto-fit to spread the current items

Use auto-fit when you want the items already present to make fuller use of the available width. Once empty tracks collapse, occupied tracks with a flexible maximum can expand into the space they free. Neither keyword is universally better: choose based on whether empty slots should remain visible as reserved layout space.

A responsive card-grid example

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: 1rem;
}

Here, minmax(12rem, 1fr) gives each repeated track a minimum size and a flexible maximum. The browser determines how many tracks fit from the available sizing; this does not promise that every card will be exactly 12rem wide. With auto-fit, any empty repeated tracks collapse after placement, so occupied flexible tracks can grow. To reserve the empty slots instead, replace auto-fit with auto-fill.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the auto-repeat track-size syntax

Auto-repeat works only with allowed fixed-size forms in the repeated track list. The permitted syntax depends on the track-size function and, for minmax(), whether its minimum and maximum satisfy the auto-repeat constraints. Line names can also affect the valid form. If a declaration is rejected or does not behave as expected, check the MDN repeat() reference for the grammar and examples rather than assuming every track definition can be placed inside an auto-repeat.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.