Skip to content
Parwaz DigitalMarketing · Web · Courses

Web Development Course · Module 1, lesson 4

Layout with CSS Flexbox

Use Flexbox to line up items in a row or column, control spacing with gap, and align content, for navigation bars, cards and buttons.

What you will learn

  • Turn a container into a flex container and set its direction
  • Align and distribute items with justify-content and align-items
  • Let items wrap and grow using flex-wrap and flex properties

Flexbox lays out items along one direction at a time. Setting display: flex on a container places its direct children in a row by default. Change this with flex-direction: column to stack them. The gap property adds even space between items without needing margins on each one. Flexbox suits components such as navigation bars, button groups and rows of cards.

Alignment works on two axes. The main axis follows the flex direction, and justify-content controls it, with values such as flex-start, center and space-between. The cross axis runs the other way, and align-items controls it. A common pattern is a header with display: flex, justify-content: space-between and align-items: center, which puts a logo on the left and a menu on the right.

By default, flex items squeeze onto one line. Adding flex-wrap: wrap lets them move onto new lines when space runs out. The flex property controls how items grow and shrink: flex: 1 makes items share the available space equally. Combining flex: 1 with a minimum width, such as flex-basis: 200px, gives a simple card row that wraps neatly on narrow screens.

Practice task

Build a header for your bakery page with the shop name on the left and three nav links on the right, then a row of three product cards that wraps when you narrow the window.

Web Development Course

Learn this properly,with a final project.

These pages summarise each lesson. The full course teaches them hands-on. Message us for the current fee, schedule, and format.

Available for new projects — Replies within one business day