On this page11 sections
Lecture 051: Grid: The Container
| Category | 8: Flexbox & Grid |
| Lecture | 051 of 057 |
| Estimated video length | 16 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Flexbox handles one line at a time. Grid handles rows and columns together: photo galleries, card layouts, whole pages with a header, sidebar and footer. You draw the grid on the parent, and the children fill it in.
Learning Objectives
- Create columns and rows with
grid-template-columnsandgrid-template-rows - Use the
frunit andrepeat() - Make items span several columns or rows
- Build a responsive card grid with
auto-fitandminmax() - Lay out a page with
grid-template-areas
1. Your First Grid
Example
<style>
.grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.grid div { background: #4c6ef5; color: white; padding: 20px; text-align: center; border-radius: 6px; }
</style>
<div class="grid">
<div>1</div><div>2</div><div>3</div>
<div>4</div><div>5</div><div>6</div>
</div>1fr 1fr 1fr = three equal columns. Items fill the cells in order, starting a new row
when one is full.
2. The fr Unit and repeat()
fr means "one share of the free space".
| You write | Columns |
|---|---|
1fr 1fr | Two equal halves |
2fr 1fr | Two thirds + one third |
200px 1fr | A fixed 200px column + the rest |
repeat(4, 1fr) | Four equal columns (short for 1fr 1fr 1fr 1fr) |
Example: a sidebar and content:
<style>
.layout { display: grid; grid-template-columns: 160px 1fr; gap: 12px; }
.layout > * { padding: 16px; border-radius: 6px; }
aside { background: #fff3bf; }
main { background: #e7f5ff; }
</style>
<div class="layout">
<aside>Sidebar (160px)</aside>
<main>Main content takes the rest</main>
</div>3. Spanning Cells
Example
<style>
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.grid div { background: #12b886; color: white; padding: 18px; border-radius: 6px; }
.wide { grid-column: span 2; background: #f76707 !important; }
.tall { grid-row: span 2; background: #ae3ec9 !important; }
</style>
<div class="grid">
<div class="wide">spans 2 columns</div>
<div class="tall">spans 2 rows</div>
<div>3</div><div>4</div><div>5</div>
</div>4. A Responsive Grid With No Media Queries
repeat(auto-fit, minmax(160px, 1fr)) means: "fit as many columns as you can, each at
least 160px wide, and share any leftover space". Drag the preview's corner to see the
columns change.
Example
<style>
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.card { border: 1px solid #dee2e6; border-radius: 10px; padding: 14px; }
</style>
<div class="cards">
<div class="card"><strong>HTML</strong><p>Structure</p></div>
<div class="card"><strong>CSS</strong><p>Style</p></div>
<div class="card"><strong>JavaScript</strong><p>Behaviour</p></div>
<div class="card"><strong>Flutter</strong><p>Apps</p></div>
</div>This one line is behind most card layouts on the web.
5. Named Areas: a Whole Page
Name the areas, then draw the layout as text.
Example
<style>
.page {
display: grid;
grid-template-columns: 140px 1fr;
grid-template-areas:
"header header"
"side main"
"footer footer";
gap: 8px;
}
.page > * { padding: 14px; border-radius: 6px; }
header { grid-area: header; background: #364fc7; color: white; }
aside { grid-area: side; background: #fff3bf; }
main { grid-area: main; background: #e7f5ff; min-height: 120px; }
footer { grid-area: footer; background: #343a40; color: white; }
</style>
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>6. Grid or Flexbox?
| Use Flexbox when… | Use Grid when… |
|---|---|
| Items go in one line (a nav bar, a button row) | You need rows and columns together |
| Item sizes come from their content | The layout decides the sizes |
They work well together: a Grid page layout, with Flexbox inside each card.
Common Beginner Mistakes
1. display: grid on the wrong element. Like Flexbox, it goes on the parent.
2. Percent columns plus gap. 33% 33% 33% plus a gap overflows. 1fr columns subtract the gap for you.
3. Area names that don't form rectangles. Each named area must be one rectangle in
grid-template-areas.
4. Forgetting grid-area on the children. The template alone doesn't place anything.
Practice Questions
- Write the CSS for a grid with four equal columns and a 16px gap.
- What does
1frmean? - How do you make one item stretch across three columns?
- What does
repeat(auto-fit, minmax(200px, 1fr))do? - Give an example of a layout better suited to Grid than Flexbox.
- Write
grid-template-columnsfor a 250px sidebar followed by flexible content.
Practice Answers
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }- One share of the free space left in the grid.
grid-column: span 3;- It fits as many columns as possible, each at least 200px wide, and stretches them to fill the row.
- A photo gallery, a card grid, or a page with header, sidebar, content and footer.
grid-template-columns: 250px 1fr;
Key Takeaways
display: grid+grid-template-columnsdraws the grid; children fill it in order.frshares free space;repeat()saves typing.spanstretches items;grid-template-areaslays out whole pages.repeat(auto-fit, minmax(…, 1fr))gives responsive cards in one line.