Content-Based Sizing with max-content
What you'll learn: How to size grid tracks so they expand to fit the widest possible layout of their content without wrapping.
What is max-content?
In the last lesson, you learned about min-content, which shrinks tracks to the smallest possible size. Now meet its opposite: max-content tells a grid track to be as wide (or tall) as it needs to display its content on a single line without any wrapping or breaking.
Think of it like this: if min-content is a compressed suitcase, max-content is that same suitcase fully unpacked and spread out on your bed—taking up all the space the items naturally want.
How It Works
When you set a column to max-content, the browser looks at all the content in that column and asks: "What's the widest this content could possibly be if it never had to wrap?" That becomes the track size.
.grid-container {
display: grid;
grid-template-columns: max-content 1fr;
gap: 20px;
}
In this example, the first column will be exactly as wide as its longest piece of content (like a long word or sentence displayed in full). The second column gets whatever remaining space is left (thanks to 1fr).
When to Use max-content
Use max-content when you want content to determine its own width naturally, without forcing line breaks. This is perfect for sidebar navigation labels, table headers, or any content where you want to prevent awkward wrapping.
Key Takeaway: max-content sizes tracks to accommodate the maximum intrinsic width of their content, displaying everything on one line without wrapping.