css · level 3 · unit 4 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A shares its parent's rows, so cards line up.
Two product cards sit side by side. One title wraps onto two lines, so its price and button sit lower than the other card's. It looks broken, and each card's own grid cannot know about its neighbour.
A subgrid fixes it. Make each card span three rows of the parent grid, then give it display: grid and grid-template-rows: subgrid. Now the title, price and button use the parent's rows, and each row grows to fit its tallest part.
The parent must be a grid, or there are no rows to borrow.
<div class="shelf">
<div class="card"><h3>Lamp</h3><p>£20</p><button>Add</button></div>
<div class="card"><h3>Oak reading chair with cushions</h3><p>£180</p><button>Add</button></div>
</div> .shelf {
display: grid;
grid-template-columns: 140px 140px;
gap: 12px;
}
.card {
grid-row: span 3;
display: grid;
grid-template-rows: subgrid;
padding: 8px;
background-color: #e0f2f1;
} Press show my page to see it here.
guess first
The parent is not a grid. Does subgrid still work?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<div class="list">
<div class="card"><h3>Mug</h3><p>£8</p></div>
</div> .list {
display: block;
}
.card {
grid-row: span 2;
display: grid;
grid-template-rows: subgrid;
}
your turn
Your turn. Line up the menu cards.
Fill in the blanks so each dish spans three rows of the menu grid and uses those rows as its own.
the page you’re styling
<div class="menu">
<div class="dish"><h3>Soup</h3><p>Tomato and basil</p><b>£6</b></div>
<div class="dish"><h3>Pie</h3><p>Steak, ale and a very flaky crust</p><b>£14</b></div>
</div>your css
Press show my page to see it here.
- each dish spans three rowsnot checked yet
- each dish borrows the menu's rowsnot checked yet
fix it
The prices still do not line up. Can you fix it?
the page you’re styling
<div class="shop">
<div class="product"><h3>Kite</h3><p>Flies in light wind</p><b>£15</b></div>
<div class="product"><h3>Stunt kite</h3><p>Two lines</p><b>£40</b></div>
</div>your css
Press show my page to see it here.
- the products borrow the shop's rowsnot checked yet
- The prices sit at different heights, so the rows are not shared. Which direction did the code borrow?
- Columns run across and rows run down. The title, text and price are stacked in rows.
- Change
grid-template-columns: subgridtogrid-template-rows: subgrid, then press run.
nailed it
you lined up every card in the row. :D
subgridborrows the parent grid's rows or columns.- Span the rows first, then set
grid-template-rows: subgrid. - Each shared row grows to fit its tallest part.
new words in your collection
- subgrid
This lesson isn’t saved. A free account remembers every lesson you finish.