HTML & CSS · Beginner · 15 min
Arrange items with Flexbox
Use a wrapping flex layout with consistent gaps.
Create a flex container
Setting display: flex arranges direct children along a main axis. The default direction is a row. Use gap to create space between items without adding a margin to every item. Flexbox is useful for a navigation bar or a short row of cards.
<style>
.topics { display: flex; gap: 16px; }
</style>
<div class="topics"><p>HTML</p><p>CSS</p></div>Expected appearance
display: flex gap: 16px
Allow items to wrap
Flex items stay on one line by default. Set flex-wrap: wrap when they should move onto another line. Preserve a meaningful source order: visually rearranging items can make keyboard and reading order confusing.
<style>
.topics { display: flex; flex-wrap: wrap; gap: 16px; }
.topics > p { flex: 1 1 160px; }
</style>
<div class="topics"><p>HTML</p><p>CSS</p><p>Accessibility</p></div>Expected appearance
flex-wrap: wrap flex-basis: 160px
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
Make .topics a flex container with flex-wrap: wrap and gap: 16px. Keep its three <p> children.
Set all three layout properties on .topics. Child sizing can use flex: 1 1 160px.
Show worked solution
<style>
.topics { display: flex; flex-wrap: wrap; gap: 16px; }
.topics > p { flex: 1 1 160px; }
</style>
<div class="topics"><p>HTML</p><p>CSS</p><p>Accessibility</p></div>Set all three layout properties on .topics. Child sizing can use flex: 1 1 160px.
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Switch between narrow and wide previews.
- Explain why wrapping is preferable to forcing these cards onto one line.