π What Are auto-fit and auto-fill?
auto-fit and auto-fill are powerful CSS Grid keywords used inside the repeat() function to create responsive, automatically adjusting columns or rows. They help you build layouts where the number of grid tracks adapts based on available space β no media queries needed!
π― Syntax
Basic Syntax
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));Both are usually used with minmax() to make flexible, responsive tracks.
π§ The Difference Between auto-fit & auto-fill
| Keyword | Behavior |
|---|---|
| auto-fill | Keeps creating extra empty tracks if space is available. |
| auto-fit | Collapses empty tracks β letting existing items stretch to fill space. |
Note
β’ auto-fill β fills the row with as many tracks as possible (even empty ones)
β’ auto-fit β fits items neatly by removing empty tracks
π How auto-fill Works
Automatically creates as many columns as can fit, even if some are empty.
auto-fill Example
.grid {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}If there is space for extra 200px columns, the grid will create empty ones, maintaining a consistent grid structure.
π How auto-fit Works
Also creates as many columns as can fit β BUT empty columns collapse, letting items stretch.
auto-fit Example
.grid {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}Items expand to fill leftover space, making the layout look cleaner and more adaptive.
π Visual Explanation
Imagine a container where only 3 items exist but space fits 5 columns:
- auto-fill: creates 5 columns β last 2 are empty
- auto-fit: collapses empty tracks β items spread out
π₯ Real-World Example β Responsive Cards
Responsive Grid Using auto-fit
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}Cards automatically wrap and fill the available space β perfect for galleries, products, etc.
π§± Example With auto-fill (Good for Consistent Structure)
Gallery Using auto-fill
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 15px;
}Even when fewer items exist, grid keeps the original structure intact.
π± Responsive Behavior
Mobile to Desktop Example
.grid {
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
}Columns grow as the screen grows and collapse gracefully on smaller screens.
β‘ Which One Should You Use?
- β auto-fit when you want items to stretch and fill the remaining space (clean layout)
- β auto-fill when you want to preserve empty columns (grid-like structure)
Note
β οΈ Common Mistakes
- Using auto-fit/auto-fill without minmax() (leads to fixed tracks)
- Expecting auto-fill to collapse empty columns (it wonβt)
- Setting minimum size too large β items may overflow
- Forgetting gap also affects how many columns can fit
π₯ Summary
auto-fit and auto-fill unlock the real power of responsive CSS Grid. They automatically create or collapse grid tracks based on space, allowing you to build layouts that adapt beautifully on every screen size.
Learn more βMDN Docs π