Flexbox: header menu and a row of cards
Task
The starter page has a header with a logo and a menu, and three course cards. With no layout, the menu items are stacked in a column and the cards sit one below another. Add CSS to the <style> block so the logo is on the left, the menu is on the right in a single row, and the cards line up in a row with equal height and wrap on a narrow screen. Don't change the HTML markup.
Requirements
- All CSS is in <style> in the <head>; the <body> markup is unchanged and there are no style attributes
- The header is a flex container: logo on the left, menu on the right (justify-content: space-between), vertically centered (align-items: center)
- The .menu items are in one row with a gap and without list markers (list-style: none)
- The .cards block is a flex container with a gap and wrapping (flex-wrap: wrap)
- The cards grow equally from a base width, for example flex: 1 1 220px
- The "Learn more" link sits at the bottom of each card: the card is a flex column (flex-direction: column) and the link has margin-top: auto
- Flexbox only: no float, position, grid, tables or inline-block
Starter code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web layout courses</title>
<style>
body { font-family: sans-serif; margin: 0; }
header { padding: 12px 24px; background: #1e293b; }
header a { color: #fff; text-decoration: none; }
.logo { font-size: 20px; font-weight: bold; }
h1 { margin: 24px 24px 0; }
.cards { padding: 24px; }
.card { border: 1px solid #cbd5e1; border-radius: 8px; padding: 16px; }
/* Add the flexbox layout here */
</style>
</head>
<body>
<header>
<a class="logo" href="/">CodeStart</a>
<nav>
<ul class="menu">
<li><a href="/courses">Courses</a></li>
<li><a href="/prices">Pricing</a></li>
<li><a href="/contacts">Contact</a></li>
</ul>
</nav>
</header>
<main>
<h1>Web layout courses</h1>
<div class="cards">
<article class="card">
<h2>HTML from scratch</h2>
<p>Tags, attributes and page structure.</p>
<a href="/courses/html">Learn more</a>
</article>
<article class="card">
<h2>CSS and Flexbox</h2>
<p>Selectors, the box model, laying out elements in rows and columns, alignment and spacing.</p>
<a href="/courses/css">Learn more</a>
</article>
<article class="card">
<h2>Responsive design</h2>
<p>Media queries and flexible sizes.</p>
<a href="/courses/responsive">Learn more</a>
</article>
</div>
</main>
</body>
</html>
▶ Open in HTML Editor Hints
- Flexbox is turned on for the parent: display: flex on the header lays out its direct children — the logo and the nav — in a row.
- The menu items are the li elements inside ul.menu, so display: flex goes on the list, not on the nav. list-style: none, margin: 0 and padding: 0 remove the markers and the extra indent.
- gap sets the space between flex items without leaving extra margins on the outer ones.
- flex: 1 1 220px means the card may grow (1), may shrink (1), and its base width is 220px. When three 220px cards don't fit, flex-wrap: wrap moves them to a new line.
- Cards in one row already have equal height: align-items defaults to stretch in a flex container. To put the link at the bottom, make the card a flex column — margin-top: auto takes all the free space above the link.
- Check the result by narrowing the preview, and try the properties in the Flexbox playground.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web layout courses</title>
<style>
body { font-family: sans-serif; margin: 0; }
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 24px;
background: #1e293b;
}
header a { color: #fff; text-decoration: none; }
.logo { font-size: 20px; font-weight: bold; }
.menu {
display: flex;
gap: 24px;
list-style: none;
margin: 0;
padding: 0;
}
h1 { margin: 24px 24px 0; }
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
padding: 24px;
}
.card {
flex: 1 1 220px;
display: flex;
flex-direction: column;
border: 1px solid #cbd5e1;
border-radius: 8px;
padding: 16px;
}
.card a { margin-top: auto; }
</style>
</head>
<body>
<header>
<a class="logo" href="/">CodeStart</a>
<nav>
<ul class="menu">
<li><a href="/courses">Courses</a></li>
<li><a href="/prices">Pricing</a></li>
<li><a href="/contacts">Contact</a></li>
</ul>
</nav>
</header>
<main>
<h1>Web layout courses</h1>
<div class="cards">
<article class="card">
<h2>HTML from scratch</h2>
<p>Tags, attributes and page structure.</p>
<a href="/courses/html">Learn more</a>
</article>
<article class="card">
<h2>CSS and Flexbox</h2>
<p>Selectors, the box model, laying out elements in rows and columns, alignment and spacing.</p>
<a href="/courses/css">Learn more</a>
</article>
<article class="card">
<h2>Responsive design</h2>
<p>Media queries and flexible sizes.</p>
<a href="/courses/responsive">Learn more</a>
</article>
</div>
</main>
</body>
</html>
Related tools
CSS Flexbox Playground
Change flex-direction, justify-content, align-items, flex-wrap and gap and see how flexbox works right away. Copy the ready CSS code in one click.
HTML Editor Online
Write HTML with syntax highlighting and line numbers, see a live preview, format or minify it, and open or save .html files — all in your browser.
Build a card with CSS
Practice exercise: build a product card in HTML and link an external stylesheet to it.
Build a navigation menu
Practice exercise: build a semantic navigation menu using <nav> and a list of links.
Linking CSS
Lesson 11: three ways to add CSS — an external file via <link rel="stylesheet">, a <style> block in head and the style attribute. Example and task.
Responsive CSS Grid cards
A card grid that adapts its column count to screen width, with no media queries.
Center an element horizontally and vertically
The simplest modern way to center a block using flexbox.