Learn HTML and CSS: A layout with flexbox
A menu bar and cards in a row
Nadia sends a design by her nephew, who's a graphic designer: the logo and the name on the left, the menu on the right, and the programme cards side by side. "Can you build that?" With what you know now, you can.
A margin that swallows everything
In a flex container, a margin with the value auto takes all the free room on its side. With margin-left: auto on the menu, the menu moves all the way to the right, while the logo and the name stay on the left.
That's how you make the classic menu bar: display: flex and align-items: center on the header, gap for some room, and margin-left: auto on the nav.
With flex-wrap: wrap the menu may go to a second line on a phone; there too the margin pushes it to the right.
header { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;}nav { margin-left: auto;}Try it yourself
Build the menu bar of the design: the logo and the name on the left, in the middle of the height, 12px of room, and the menu on the right. On a phone the menu may go to a second line.
/* Your CSS goes here. */<!DOCTYPE html><html lang="en"><head> <title>Wheels & Meals</title> <link rel="stylesheet" href="basis.css"> <link rel="stylesheet" href="style.css"></head><body> <header> <img src="truck.svg" alt="" width="40" height="40"> <h1>Wheels & Meals</h1> <nav> <a href="index.html">Home</a> <a href="trucks.html">Trucks</a> <a href="map.html">Map</a> <a href="tickets.html">Tickets</a> </nav> </header> <main> <p class="slogan">Food truck festival on Fore Street in Tiverton, 14 and 15 June.</p> <section class="square"> <div class="truck chips">Chips</div> <div class="truck taco">Tacos</div> <div class="truck ice-cream">Ices</div> <div class="truck pizza">Pizza</div> </section> <section class="cards"> <article class="card"> <h2>Saturday</h2> <p>From 11 am to 11 pm, with live music from 8 pm.</p> </article> <article class="card"> <h2>Sunday</h2> <p>From 11 am to 8 pm, with a kids’ corner.</p> </article> <article class="card"> <h2>Entry</h2> <p>Free. You pay per bite.</p> </article> </section> </main> <footer> <p>Fore Street, Tiverton EX16 6LT</p> </footer></body></html>body { margin: 0; font-family: system-ui, sans-serif; background: #fff8ec; color: #2b2118;}header { padding: 12px 16px; background: #264653; color: #ffffff;}header h1 { margin: 0; font-size: 1.5rem;}nav a { padding: 6px 10px; border-radius: 999px; color: #ffffff; text-decoration: none;}main { padding: 0 16px 24px;}.slogan { margin: 16px 0 0;}.square { margin: 16px 0; padding: 12px; border-radius: 18px; background-color: #efe6d4; background-image: radial-gradient(rgb(110 90 60 / 0.16) 1px, transparent 1.5px); background-size: 14px 14px; box-shadow: inset 0 0 0 1px rgb(110 90 60 / 0.14);}.square.tall { height: 200px;}.square.narrow { max-width: 250px;}/* A striped awning and two wheels: only for the look. */.truck { position: relative; margin-bottom: 6px; padding: 16px 14px 12px; border-radius: 10px 10px 8px 8px; background-color: #e76f51; background-image: repeating-linear-gradient(90deg, rgb(255 255 255 / 0.85) 0 7px, transparent 7px 14px); background-size: 100% 7px; background-repeat: no-repeat; box-shadow: 0 1px 1px rgb(43 33 24 / 0.12), 0 6px 12px -6px rgb(43 33 24 / 0.35); color: #ffffff; font-weight: 700;}.truck::before,.truck::after { content: ""; position: absolute; bottom: -5px; width: 10px; height: 10px; border-radius: 50%; background: #d9cfbb; box-shadow: inset 0 0 0 3px #2b2118;}.truck::before { left: 9px;}.truck::after { right: 9px;}.chips { background-color: #e9c46a; color: #3a2a00;}.taco { background-color: #e76f51;}.pizza { background-color: #2a9d8f;}.waffle { background-color: #9c6644;}.ice-cream { padding-top: 30px; padding-bottom: 26px; background-color: #f4a6c6; color: #4a1030;}.curry { background-color: #f4a261; color: #3a1f00;}.burger { background-color: #6d597a;}.soup { background-color: #588157;}.card { padding: 4px 16px; border-radius: 12px; background: #ffffff; box-shadow: 0 1px 2px rgb(43 33 24 / 0.08);}.card h2 { font-size: 1.15rem;}footer { padding: 12px 16px; background: #264653; color: #ffffff;}Ready for more?
The full lesson has 4 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson