Skip to content
Tutorials Center
My learning

Topics

Lesson 75 of 139 ยท Grid

Placing Grid Items

Position CSS Grid items with line numbers, span and negative lines, understand the implicit grid, and fill gaps automatically with grid-auto-flow dense.

So far the browser has placed every grid item for you, one after another. That auto-placement is great for galleries, but layouts often need more control: a header that spans the full width, a featured card that takes two cells, a sidebar that runs down the side.

In this lesson you will place items by line numbers, stretch them with span, count lines backward with negative numbers, see what happens when you place something outside the grid you defined, and learn how grid-auto-flow: dense fills holes.

Line numbers

Every grid has numbered lines. Vertical lines (column lines) are numbered from 1 at the start edge; horizontal lines (row lines) from 1 at the top. A three-column grid has column lines 1, 2, 3 and 4.

To place an item, tell it which line to start on and which line to end on:

CSS
.header {
  grid-column-start: 1;
  grid-column-end: 4;   /* from line 1 to line 4: all three columns */
  grid-row-start: 1;
  grid-row-end: 2;
}

The shorthands are shorter and far more common. Write the start line, a slash, then the end line:

CSS
.header {
  grid-column: 1 / 4;
  grid-row: 1 / 2;
}

If you give only a start line, the item spans one track: grid-row: 2 means "row line 2 to row line 3".

Placing items by line number

The footer has only grid-column set. Its row is chosen by auto-placement, which puts it in the first free row: row 4. Try changing the sidebar to grid-row: 2 / 5 and see the footer react.

Spanning with span

Counting lines gets annoying when you only care about size. The span keyword says "cover this many tracks":

CSS
.featured {
  grid-column: span 2;  /* two columns wide, wherever it lands */
  grid-row: span 2;     /* two rows tall */
}

.banner {
  grid-column: 2 / span 3;  /* start at line 2, cover three columns */
}

span on its own leaves the position up to auto-placement, which makes it perfect for galleries where some items are bigger than others.

Featured items with span

Look closely and you may see a hole: an empty cell where an item did not fit, because the next item was too wide and had to move to the following row. Keep that in mind; you will fix it in a moment.

Negative line numbers

Lines can also be counted from the end: -1 is the last line, -2 the one before it, and so on. That gives you an easy way to say "all the way across" without knowing how many columns there are:

CSS
.full-width {
  grid-column: 1 / -1;  /* first line to last line */
}

1 / -1 is one of the most useful snippets in Grid. Use it for headers, footers and section titles inside card grids.

One catch: negative numbers count from the end of the explicit grid, the tracks you defined in grid-template-columns and grid-template-rows. They do not know about implicit rows the browser added. So grid-row: 1 / -1 only spans every row if you defined the rows yourself.

The implicit grid

What happens if you place an item outside the grid you defined, say at column 6 in a three-column grid? The browser does not complain. It adds implicit tracks to make room.

CSS
.grid {
  grid-template-columns: 100px 100px;  /* two explicit columns */
  grid-auto-columns: 50px;             /* size for any extra columns */
}
.far-away {
  grid-column: 4;  /* creates implicit columns 3 and 4 */
}

Implicit rows are much more common: every time auto-placement runs out of rows, it adds one. As you saw in Grid Basics, their size comes from grid-auto-rows (and grid-auto-columns for columns), which defaults to auto.

Implicit columns appear when needed

Columns 1 and 2 are explicit (90px). Columns 3 to 5 were created on demand and are 50px wide. Columns 3 and 4 are empty but still take space.

Filling holes with grid-auto-flow: dense

By default, auto-placement only moves forward. When a wide item does not fit at the end of a row, it moves to the next row and leaves a gap behind. It never goes back to fill it.

grid-auto-flow: dense changes that. The browser goes back and fills earlier holes with later items that fit:

CSS
.gallery {
  grid-auto-flow: dense;
  /* or: grid-auto-flow: row dense;  /  column dense; */
}

Add grid-auto-flow: dense; to the .gallery in the "Featured items" example above and the holes disappear.

Dense packing reorders items visually. Item 7 might appear before item 6 on screen, while keyboard focus and screen readers still follow the HTML order. That is fine for a photo wall where order does not matter. Avoid it for content that is read in sequence, such as articles, steps or search results.

Overlapping items

Nothing stops two items from claiming the same cells. When they do, they overlap. Later items in the source paint on top, and you can change that with z-index (grid items can use z-index without needing position). This is a clean way to put a caption on top of an image without absolute positioning:

CSS
.hero { display: grid; }
.hero > * { grid-area: 1 / 1; }   /* everything in the same cell */

grid-area: 1 / 1 is a shorthand for row start and column start. You will meet grid-area properly in the next lesson, and use the overlap trick in Grid Patterns.

Order and accessibility

Grid makes it easy to put the fifth element in the top-left corner. Use that power gently. Keyboard users move through links and buttons in source order, and screen readers read in source order too. If the visual order is wildly different, people get lost. Change the order in the HTML when the order matters, and use placement mainly to arrange regions of a page.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which declaration makes an item stretch across every column of the explicit grid?

    Line 1 is the first line and line -1 is the last line of the explicit grid, so 1 / -1 spans all explicit columns.
  2. 2. What does grid-column: 2 / span 3 mean?

    The item starts at column line 2 and spans three tracks, so it ends at line 5.
  3. 3. You place an item at grid-column: 6 in a grid that defines only three columns. What happens?

    Grid adds implicit tracks, sized by grid-auto-columns, until the requested line exists.
  4. 4. Why should you be careful with grid-auto-flow: dense?

    Dense packing moves later items into earlier holes visually, but focus order and reading order still follow the HTML.

Summary

  • Grid lines are numbered from 1; place items with grid-column: start / end and grid-row: start / end.
  • span N sets a size instead of an end line, and can be combined with a start line.
  • Negative numbers count from the end of the explicit grid; 1 / -1 spans the full width.
  • Placing items beyond the defined tracks creates implicit tracks sized by grid-auto-rows and grid-auto-columns.
  • grid-auto-flow: dense back-fills holes but changes visual order, so use it only where order does not matter.
  • Items can share cells and overlap; keep visual order close to source order for accessibility.

Next: Grid Template Areas, where you name parts of the grid and draw your layout in ASCII art.