CSS Grid is the layout system designed for two dimensions at once: rows and columns together. Flexbox arranges items along a single line, which is perfect for navbars and card rows, but page shells, image galleries and dashboards need items to line up across both directions. Grid was built for exactly that, and its basics are simpler than its reputation suggests. A container, a definition of columns, a gap, and items that place themselves.
This quick start covers the properties you will use daily, then builds three layouts: a classic page shell with header, sidebar, content and footer, a responsive card grid that needs no media queries, and a small dashboard arrangement using named areas. If you have not used flexbox yet, the examples in CSS flexbox layout examples are a good companion, because real pages use both systems side by side.
Your First Grid
A grid starts by setting display grid on a container. You then describe the columns you want, and child items flow into the cells in order, one per cell, creating new rows as needed.
.grid {
display: grid;
grid-template-columns: 200px 1fr 1fr;
gap: 16px;
}
The grid template columns property lists the width of each column in order. Here the first column is fixed at 200 pixels and the other two share the remaining space equally. The fr unit is the key idea: it represents a fraction of the free space left after fixed sizes and gaps are taken out. Two columns of 1fr each split the leftover space in half; making one of them 2fr would give it twice the share of the other. Gap sets space between rows and columns together, replacing the margin juggling older layout methods required. Rows are created automatically at the height their content needs, unless you define them explicitly.
Items need no styling at all in this basic pattern. That is a deliberate strength of grid: layout lives on the container, so markup stays clean and items can be reordered or added without each one carrying positioning rules.
Example 1: A Responsive Card Grid Without Media Queries
The most copied grid snippet on the web solves a familiar problem: a gallery of cards that should show as many columns as fit, never squeeze below a sensible width, and fill the row evenly. One line achieves it.
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 20px;
}
Breaking the value down: repeat tells the browser to create the pattern as many times as it fits. Auto fit means the browser calculates how many columns of the given size fit in the current container width and collapses empty tracks. Minmax sets a range for each column, at least 240 pixels and at most one fraction of the available space. The combined effect is a grid that shows four columns on a wide desktop, two on a tablet and one on a phone, with no breakpoints written by hand. Cards are equal width within each row, rows align across the page, and the layout responds to its container rather than only to the viewport, so the same component works in a sidebar or a full width section.
The auto fill keyword is a close relative. It keeps empty tracks in place instead of collapsing them, which matters when you want items in later rows to keep the same column positions even when the last row is not full. For typical card galleries, auto fit is the friendlier default because it stretches the visible cards to fill the row.
Example 2: A Page Shell with Named Areas
Named template areas are grid at its most readable. You draw the layout as a small text diagram, name each region, then assign elements to those names. Anyone reading the code later can see the page structure at a glance.
.page {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
gap: 0;
}
.site-header { grid-area: header; }
.site-sidebar { grid-area: sidebar; }
.site-main { grid-area: main; }
.site-footer { grid-area: footer; }
The columns give a fixed 240 pixel sidebar beside a flexible main column, and the rows size the header and footer to their content while the middle row takes all remaining height, helped by the minimum height of the full viewport on the container. The areas diagram shows the header and footer spanning both columns, with sidebar and main sharing the middle row. Each element then claims its area by name. Restructuring the page for mobile becomes a matter of redrawing the diagram inside a media query so everything stacks in one column, without touching the individual elements at all. That separation is why teams reach for areas on application shells and article templates. The breakpoint side of that work is covered in CSS responsive media queries.
Placing Items by Line Numbers
Sometimes an item needs to span several columns or start at a specific position, such as a featured article that is twice as wide as the others, or a dashboard widget that occupies a two by two block. Grid lines give you that control. Columns and rows create numbered lines, and an item can be told which lines it sits between.
.featured {
grid-column: span 2;
}
.wide-banner {
grid-column: 1 / -1;
}
.tall-widget {
grid-row: span 2;
}
Span 2 means the item covers two tracks from wherever it is placed, the simplest way to make one card stand out in a gallery. The 1 to -1 pattern means start at the first column line and end at the last, because negative line numbers count from the end of the grid; that makes an element full width regardless of how many columns the grid currently has, which stays correct when the column count changes at different screen sizes. Row spans work the same way vertically. Use these tools sparingly: most items should flow automatically, with explicit placement reserved for the few elements whose size carries meaning.
Example 3: A Small Dashboard Layout
Dashboards combine the earlier ideas: a flexible column pattern, a couple of spanning widgets, and alignment inside cells. The structure below creates four equal columns, lets a chart widget span two of them, and keeps statistics cards compact at the top.
.dashboard {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 16px;
}
.chart {
grid-column: span 2;
}
.stat {
padding: 16px;
border: 1px solid #ddd;
border-radius: 10px;
}
With repeat and four equal fractions, the grid divides neatly for statistics, charts and tables. The chart claims two columns, so it sits beside two single width widgets in the same row. On a smaller screen, a media query can change the template to two columns and let the chart span both, keeping the information hierarchy without redesigning each widget. Inside each widget, flexbox is often the better tool for arranging a title, a value and a small label, which illustrates the normal division of labour: grid for the page structure, flexbox for the contents of each box.
Alignment Inside the Grid
Grid includes alignment properties that echo the flexbox ones. Justify items aligns items inside their cells horizontally, align items does the same vertically, and the default stretch makes items fill their cell, which is why equal height cards happen automatically in grid. Place items is a shorthand that sets both at once, so place items center on a container centers every item in its cell, a trick that is also one of the simplest answers in centering a div. When the whole grid is smaller than its container, justify content and align content distribute the tracks themselves, using familiar values such as start, center, end and space between.
Common Grid Mistakes
Three mistakes account for most beginner frustration. The first is defining far more structure than the layout needs, with explicit rows and placements for everything, when auto placement would do the job and adapt better to new content. The second is mixing fixed pixel columns with content that cannot shrink, causing overflow on narrow screens; pairing a minmax with a flexible maximum, or switching the template at a breakpoint, avoids it. The third is forgetting implicit rows: items beyond the defined ones create automatic rows sized by content, which is usually helpful, but can surprise you if you expected a strict pattern; grid auto rows lets you set a consistent size for those generated rows.
Also keep an eye on source order. Grid can place items anywhere visually, but keyboard focus and screen readers follow the HTML order. Build the document in a logical reading order first, then use grid to present it, rather than rearranging meaning with placement properties. Defining your spacing and colour tokens once with CSS variables also helps, because grid layouts tend to repeat the same gap and card styling across many components.
Example 4: A Simple Image Gallery with Uniform Tiles
A gallery is a good final test because it combines column sizing, row consistency and image fitting in one small component. The goal is tiles that stay the same shape even when the uploaded images have different proportions, so the page never jumps around or shows awkward gaps between rows.
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 12px;
}
.gallery figure {
margin: 0;
aspect-ratio: 4 / 3;
overflow: hidden;
}
.gallery img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
The gallery reuses the auto fit and minmax pattern from the card grid, with a smaller minimum because thumbnails can sit comfortably at narrower widths. Each figure is given an aspect ratio, which reserves a consistent tile shape before the image loads and keeps every row level. Object fit cover then fills that tile completely, cropping the edges of images that do not match the ratio instead of stretching them out of shape. The result is a gallery that adapts from a phone to a wide monitor with no extra rules, and individual tiles can still be swapped, added or removed in the markup without touching the stylesheet at all. That low maintenance quality is the practical reason grid has become the default choice for galleries, listings and search results.
Conclusion
Grid rewards a small set of habits: define columns with a mix of fixed sizes and fractions, let items flow automatically, use the auto fit and minmax pattern for card galleries, name areas for page shells, and reserve line placement for items that truly need to span or sit somewhere specific. With those patterns, responsive page layouts that once needed frameworks and float fixes become a few lines of CSS that stay readable as the site grows.
Tagged: css css snippets grid layout responsive web design



2 thoughts on “CSS Grid Quick Start: Simple Responsive Layouts”