Lesson 7 ยท about 15 minutes
๐ Flexbox: Lining Up Your Boxes
Right now your boxes stack on top of each other like pancakes. Today you learn the magic spell that lines them up side by side โ and your page suddenly looks like a REAL website.
๐ฌ Watch first (90 seconds)
The pancake problem
In lesson 6 you learned that every element is a box. But have you noticed something? The browser stacks your boxes top to bottom, like a pile of pancakes. ๐ฅ One card, then the next card under it, then the next one under that.
Real landing pages don't look like that. They have cards sitting next to each other in a neat row. How? With one line of CSS paint called flexbox.
.row {
display: flex;
}
โฆand all its children (the train cars) snap into a row, side by side. The rule goes on the track, never on the cars!
Once the track exists, you get three bonus controls โ all written on the parent too:
gap: 16px;โ space between the train cars, so they don't squish together.justify-content: center;โ where the whole train sits on the track.centerparks it in the middle,space-betweenspreads the cars from end to end,flex-endpushes them to the right.align-items: center;โ lines the cars up top-to-bottom, so a short car and a tall car sit level with each other.
display: flex on a box, it is NOT about how that box looks. It's about where its KIDS go. Parent = track, children = cars. Remember that and half of all CSS layouts stop being mysterious.
๐ ๏ธ Build it (the fun part)
- Open
practice/myshop.htmlin VS Code. - Make THREE feature cards โ divs with
class="card", just like lesson 6. Each one gets a tiny headline and a sentence about one cool thing your product does:<div class="card"> <h3>Super Fast</h3> <p>Ties your shoes in 2 seconds flat.</p> </div> <div class="card"> <h3>Waterproof</h3> <p>Works in rain, puddles, even the bath.</p> </div> <div class="card"> <h3>Glow Mode</h3> <p>Lights up when it gets dark outside.</p> </div> - Now build the track: wrap ALL three cards in one parent div:
<div class="row"> ... your three cards go here ... </div> - In your
<style>section, add the flexbox spell to the parent:.row { display: flex; gap: 16px; justify-content: center; } - Save (โS) and refresh the browser (โR). BOOM โ three cards in a row, centered. Your page just went from "school project" to "real landing page". ๐
๐น๏ธ Try it right here โ the lesson checks your code!
Type in the dark box, watch the preview update live, then press Check my code โ to see if you nailed the task.
๐จ Now break it!
Back in myshop.html โ mess with your .row and watch the train move. Predict what will happen BEFORE you refresh!
- Change
justify-content: centertospace-between. Save, refresh. Where did the cars go? Now tryflex-end. - Delete the
gap: 16px;line. Save, refresh. Yuck, right? Put it back. Trygap: 50px;too. - Add a FOURTH card inside the row. Did you need to change any CSS for it to join the train?
- Grab the edge of your browser window and drag it narrower. Watch the cards squish! Flexbox squeezes the cars to fit the track.
display: flex. It must be on the parent (.row), not on the cards. Putting it on the cards is the number one flexbox mistake on Earth โ even grown-up coders do it.
๐ฎ Quiz time
No peeking at the notes above โ trying to remember is what makes it stick!