Grid Basics - HTML Tutorial
CSS Grid is a layout system for arranging items in rows and columns at the same time. Where Flexbox lines things up in one direction, Grid lets you design in two, which makes it ideal for card grids, galleries and whole-page layouts.
Creating a Grid
Like Flexbox, Grid works on a container and its direct children. Set display: grid on the container, then describe the columns with grid-template-columns:
<style>
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 1rem;
}
.grid div {
background-color: #eef6ff;
border: 1px solid #9cc2f0;
padding: 1rem;
}
</style>
<div class="grid">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
The six items fill the grid in order, three per row. New rows are created automatically as needed, and gap adds space between both rows and columns.
The fr Unit
The fr unit means "a fraction of the available space". 1fr 1fr 1fr creates three equal columns. Some other examples:
2fr 1fr: the first column is twice as wide as the second.15rem 1fr: a fixed-width first column, with the second column taking up the rest.repeat(4, 1fr): four equal columns.repeat()saves you typing the same value over and over.
A Responsive Card Grid
One of Grid's most useful patterns creates as many columns as will fit, with no media queries at all:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
gap: 1rem;
}
This reads as: "Fit as many columns as you can, where each column is at least 15rem wide and shares any extra space equally." On a phone you get one column, on a tablet two, and on a wide screen three or more. Try it with the menu cards from Flexbox Basics and resize your window.
Spanning Columns and Rows
An item can stretch across more than one column or row using grid-column and grid-row:
.featured {
grid-column: span 2; /* two columns wide */
}
.tall {
grid-row: span 2; /* two rows tall */
}
Laying Out a Whole Page
Grid can also arrange the main parts of a page. With grid-template-areas, you give each area a name and then "draw" the layout using those names:
<style>
.page {
display: grid;
grid-template-columns: 1fr 18rem;
grid-template-areas:
"header header"
"content sidebar"
"footer footer";
gap: 1rem;
}
.page > header { grid-area: header; }
.page > main { grid-area: content; }
.page > aside { grid-area: sidebar; }
.page > footer { grid-area: footer; }
</style>
<div class="page">
<header>Corner Cafe</header>
<main>Main content goes here.</main>
<aside>Opening hours and news.</aside>
<footer>Contact details</footer>
</div>
Each quoted line in grid-template-areas is a row, and each word is a column. The header and footer span both columns, while the main content and sidebar sit side by side. The > in the selectors means "a direct child of", so only the top-level parts of the page are placed.
The two-column layout is too cramped on a phone, though. In Responsive Design Basics, you'll learn how to switch to a single column on small screens.
Flexbox or Grid?
Both are excellent, and you'll often use them together. As a rule of thumb:
- Use Flexbox for a single row or column of items, where the size of each item can depend on its content, such as a navigation bar, a row of buttons or centering a single item.
- Use Grid when you want items to line up in both rows and columns, such as a card grid, a photo gallery or the overall layout of a page.