Grid-Template-Areas: Building Intuitive Two-Dimensional Layouts
Learn how grid-template-areas lets you visualize and code complex page structure with named regions instead of counting columns and rows.
Build galleries that truly respond to screen size. Learn the difference between auto-fit and auto-fill, and when each approach works best for your content.
Creating a gallery that looks great on every device isn’t just about making images smaller on mobile. It’s about fundamentally rethinking how your grid flows. When you’re designing for Hong Kong’s diverse user base—from desktop workstations to mobile phones—you need CSS Grid functions that adapt intelligently.
The challenge: You don’t know how many images will fill a row until the browser renders it. You don’t know the exact viewport width your user’s got. And you definitely don’t want to write a hundred media queries just to handle different screen sizes. That’s where auto-fit and auto-fill come in. They’re the CSS Grid equivalent of “just figure it out.”
Auto-fit collapses empty tracks. Auto-fill keeps them. That single difference changes everything about how your gallery behaves when items don’t perfectly fill the row.
Auto-fit is your friend when you want a truly responsive gallery that fills the available width. It creates as many columns as will fit in your container, then collapses any empty tracks. This means your images stretch to fill the remaining space when there aren’t enough items to complete a full row.
Let’s say you’re displaying product images on a portfolio site. You’ve set your grid to create columns of 250 pixels each. On a 1200px container, that gives you 4 columns. But you’ve only got 3 products. With auto-fit, those 3 items expand to fill the entire width evenly. No awkward gaps. No empty columns sitting there looking lonely.
The CSS looks like this:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr))
. You’re telling the browser: “Create as many 250-pixel-minimum columns as will fit, and if we’ve got fewer items than columns, stretch them out proportionally.”
Auto-fill keeps those empty tracks around. It creates as many column tracks as will fit in your container, period. If you’ve got 3 items but space for 4 columns, you get 4 columns—the 4th one just sits there empty.
This sounds like a limitation, but it’s actually perfect for certain designs. Imagine you’re building a gallery where consistency matters more than filling space. Each image should be exactly 280 pixels wide. No stretching. No awkward proportions. With
grid-template-columns: repeat(auto-fill, minmax(280px, 280px))
, every item maintains that exact width, and empty space just happens naturally.
It’s the difference between “make these items fit perfectly” and “these items are this size, period.” Auto-fill is your choice when size consistency beats space optimization.
The beauty of auto-fit and auto-fill is that they eliminate the need for complicated media queries just to handle different screen sizes. You’re not writing breakpoints for every possible device. You’re letting CSS do the intelligent work of fitting items into available space.
Start by asking yourself: Do I want my items to stretch and fill available space? That’s auto-fit. Do I want my items to maintain consistent dimensions? That’s auto-fill. Neither is wrong. They’re just different tools for different design problems.
Test both approaches on your projects. You’ll quickly develop an intuition for which one fits your layout philosophy. And your galleries will respond gracefully to whatever screen size your Hong Kong users throw at them.
This article provides educational information about CSS Grid functions. Browser support for auto-fit and auto-fill is excellent in modern browsers (Chrome 57+, Firefox 52+, Safari 10.1+), but always test your implementations across your target devices. CSS specifications evolve, and future updates may introduce additional capabilities or changes to these functions. For the most current information, consult the official CSS Grid specification and browser documentation.