Sub topic 1
Creating a grid container
Prerequisites
This chapter assumes you know basic HTML structure, CSS selectors and the box model, and how the display property and normal document flow work. You should also be comfortable with length units such as px, %, and rem.
What a grid container is
CSS Grid lays out content in rows and columns at the same time. You turn on Grid by setting display: grid on a parent element. That element becomes the grid container. Its direct children become grid items.
.container {
display: grid;
}Only direct children become grid items. Grandchildren stay in normal flow inside their parent item, unless you make that item a grid too (covered in a later chapter).
A first example
Here is some HTML with a container and four items.
<div class="container">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
<div class="item">Four</div>
</div>With only display: grid and no track definitions, the browser creates one column that fills the container's width. Each item gets its own row, so the result looks like stacked block elements. The grid exists, but it has no visible structure yet.
To get a real layout, you define tracks. A track is a single row or column in the grid. The next chapter covers this in detail. This example previews it:
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto auto;
gap: 16px;
}This gives three columns. The first is 200px wide. The other two share the remaining space equally. There are two rows, separated from each other by 16px gaps. With four items, the first three fill row one and the fourth starts row two.
What changes when you set display: grid
Setting display: grid does several things.
- The container itself behaves like a block-level box. It takes the full available width, like a
div. - Its children are blockified. For example, a
spanchild is treated as a block-level item. float,clear, andvertical-alignhave no effect on grid items.- Margins of grid items do not collapse with each other or with the container.
If you want the container to size to its content and sit inline with surrounding text, use display: inline-grid. The children behave the same way. Only the outer behavior of the container changes.
How the container is structured
Common errors
- Applying
display: gridto the wrong element. The property goes on the parent, not on the items. - Expecting columns without defining them. Without
grid-template-columns, you get a single column. - Expecting grandchildren to line up with the grid. Only direct children participate.
- Using
floaton items. It is ignored inside a grid.
Exercise
You set display: grid on a ul that contains five li elements, and each li contains a link. Which elements are grid items, and how many columns does the grid have if you add no other properties?
Show answer
The five li elements are the grid items, because they are direct children of the container. The links are not grid items. The grid has one column, because no grid-template-columns is defined.