Flexbox shines for one-dimensional rows: logo on the left, links on the right, wrapping on small screens. You will use it constantly in headers.
Core properties
display: flexon the container.justify-content: space-between— push groups apart.align-items: center— vertical alignment.gap— space between items without margin hacks.
HTML structure
Keep nav links in a <ul> inside <nav> for semantics; reset list styles in CSS.
Practice
- Add a fourth nav item—gap keeps spacing even.
- Set
flex-wrap: wrapand shrink the window. - Try
margin-left: autoon theulinstead ofspace-between.
Important interview questions and answers
- Q: Flexbox vs Grid in one line?
A: Flex lays out along one axis; Grid handles two-dimensional rows and columns together. - Q: What does `justify-content` control?
A: Alignment along the main axis of the flex container. - Q: Why use `gap` instead of margins on every item?
A: Gap is consistent, avoids first/last margin bugs, and works with wrapping.
Challenge
Collapse the nav
- At
max-width: 30rem, stack brand and links vertically withflex-direction: column.
Done when: narrow preview shows stacked nav without overflow.