Skip to content
tezvyn:

CSS Grid: Placing Items on the Grid

Source: developer.mozilla.orgMediumHow cards are made

CSS Grid: Placing Items on the Grid

Think of CSS Grid as a coordinate system for your layout. You can explicitly place items using line numbers or named areas, or let the browser auto-place them. This is key for page scaffolds and galleries. Footgun: Grid lines are numbered from 1, not 0.

Why it exists

Before CSS Grid, creating two-dimensional layouts was a hack. Developers used floats, tables, or complex Flexbox nesting to arrange elements in both rows and columns. CSS Grid was introduced to provide a native, robust system for true 2D layout, solving the core problem of arranging content along two axes simultaneously.

The mental model

Think of a grid container as a piece of graph paper. By declaring display: grid, you create a set of intersecting horizontal and vertical lines that define columns and rows. Your direct child elements become grid items, which you can then place onto this graph paper with precision, like plotting points on a coordinate system.

How it works

You control item placement in two main ways. First, explicit placement. You tell an item exactly where to go using properties like grid-column-start and grid-row-start. For example, grid-column: 1 / 3; tells an item to start at the first column line and end at the third, spanning two columns. You can also name your grid lines and areas for more semantic code. Second, auto-placement. If you don't give an item a specific position, the browser's grid algorithm places it for you. It finds the next available empty cell, typically moving across columns before dropping to the next row.

When to use it

Use explicit placement for foundational layout elements like a page's header, sidebar, main content area, and footer. This ensures your key components are exactly where you expect them to be. Use auto-placement for homogenous collections of items, like a photo gallery or a list of product cards, where the browser can efficiently handle the flow.

When not to use it

Avoid relying solely on auto-placement when the visual order of items is critical for accessibility or user understanding. A change in the number of grid columns (e.g., on a smaller screen) can cause items to reflow in a non-intuitive order. For layouts that only need to control one dimension (either a row or a column), Flexbox is often a simpler choice.

One canonical example

To create a classic sidebar layout, you can define a two-column grid and explicitly place your items. Given a container with header, main, aside, and footer elements, the CSS would be: .container { display: grid; grid-template-columns: 3fr 1fr; } header { grid-column: 1 / 3; } main { grid-column: 1 / 2; } aside { grid-column: 2 / 3; } footer { grid-column: 1 / 3; }. The header and footer span both columns, while the main content and sidebar occupy their own.

Interview question

For which layout scenario is explicit item placement in CSS Grid most suitable?

  • a.Arranging a photo gallery where images should fill available cells automatically.
  • b.Displaying a collection of user-generated content where the order isn't strictly defined.
  • c.Structuring the main regions of a webpage, such as the header, navigation, and main content.Correct
  • d.Creating a horizontal list of items that need to be evenly spaced within a single row.
Why?

The card states that explicit placement is ideal for foundational layout elements like a page's header, sidebar, and main content. Options A and B describe scenarios better suited for CSS Grid's auto-placement feature, while option D is typically handled more simply with Flexbox for single-dimension layouts.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles