Content-Based Sizing with min-content
You'll learn how to create grid tracks that automatically size to fit the smallest possible width their content can take without breaking or overflowing.
What is min-content?
When you use min-content as a track size in CSS Grid, you're telling the browser: "Make this track only as wide (or tall) as the smallest it can be while still fitting its content without overflow."
Think of it like shrink-wrapping: the track compresses to the absolute minimum size needed. For text, this means wrapping at every possible opportunity—words break onto new lines rather than stretching the track wider. For images or elements with inherent width, it's their smallest dimension.
How to Use It
You apply min-content in your grid template definitions:
.grid-container {
display: grid;
grid-template-columns: min-content 1fr;
gap: 20px;
}
In this example, the first column will shrink to the narrowest possible width its content allows. If that column contains text like "Hello World", it might stack vertically as:
Hello
World
...taking only as much width as the longest single word needs.
When to Use min-content
This is perfect for sidebars with icons or labels, status badges, or navigation items where you want the track to hug its content tightly, giving maximum space to other tracks (like those using fr units).
Key Takeaway: min-content creates grid tracks that shrink to the absolute minimum width needed to display their content without overflow, wrapping text aggressively and yielding space to other tracks.