Auto-Sizing Tracks with auto
You'll learn how the auto keyword lets grid tracks size themselves based on their content and available space.
When defining grid tracks with grid-template-columns or grid-template-rows, you can use the auto keyword instead of fixed sizes or fr units. An auto track sizes itself based on its content—it grows to fit the largest item within it, but only takes what it needs.
How auto Works
Think of auto like a shrink-wrap for your content. If a column contains a long word or image, the column expands to fit it. If the content is small, the column stays small. Unlike fr units (which share available space), auto sizes to the content's natural size.
.grid {
display: grid;
grid-template-columns: auto 200px auto;
gap: 10px;
}
In this example, the middle column is always 200px wide. The first and third columns use auto, so they'll each size themselves to fit their content. If the first column contains "Hello" and the third contains "World", each column will be just wide enough for those words.
auto vs fr
When you mix auto with fr units, auto tracks claim their space first based on content, then fr units divide what's left. So grid-template-columns: auto 1fr means "make the first column fit its content, then give all remaining space to the second column."
When to Use auto
Use auto when you want content to dictate size naturally—perfect for columns containing buttons, icons, or text that shouldn't be artificially stretched or squished.
Key Takeaway: The auto keyword sizes grid tracks based on their content's natural dimensions, taking only the space needed rather than sharing available space like fr units.