Protected Learning Content

AICPE Gurukul content is created for learning purposes. Printing, copying and unauthorized reuse are restricted.

AICPE Learning Hub HTML with CSS
Chapter 19
CSS Flexbox
Chapter 19 | Modern Layout

CSS Flexbox

Flexbox is a powerful CSS layout system used to arrange items in rows or columns. It is perfect for menus, cards, buttons, pricing boxes, course grids and responsive layouts.

display:flex

Turns a parent element into a flex container.

justify-content

Aligns items horizontally along the main axis.

align-items

Aligns items vertically along the cross axis.

flex-wrap

Allows items to move to the next line.

Learning Objectives

After This Chapter, You Will Understand

This chapter helps you create clean, flexible and responsive layouts.

Flex Container

Use display:flex on a parent element.

Main Axis

Control row or column direction.

Alignment

Use justify-content and align-items.

Responsive Cards

Create wrapping card layouts.

Flexbox Meaning

What is CSS Flexbox?

Flexbox is a one-dimensional layout system for arranging items in a row or column.

1 Simple Meaning

Flexbox allows a parent container to control the position, direction, spacing and alignment of its child elements. It is especially useful when items need to adjust according to screen size.

Definition: Flexbox is a CSS layout model used to arrange elements efficiently in one direction: row or column.
Easy Memory: Parent gets display:flex. Children become flex items.

2 Basic Syntax

.container {
    display: flex;
    gap: 20px;
}

The element with display:flex becomes the flex container. Its direct children become flex items.

Flex Properties

Important Flexbox Properties

These are the most commonly used Flexbox properties for layouts.

Property Use Common Values
display Creates flex container flex
flex-direction Controls item direction row, column, row-reverse, column-reverse
justify-content Aligns items on main axis flex-start, center, space-between, space-around
align-items Aligns items on cross axis stretch, center, flex-start, flex-end
flex-wrap Allows items to wrap to next line nowrap, wrap
gap Adds space between flex items 10px, 20px, 2rem

3 flex-direction

.container {
  display: flex;
  flex-direction: row;
}

4 gap

.container {
  display: flex;
  gap: 20px;
}
Visual Examples

Flexbox in Action

These demos show how Flexbox changes item direction and alignment.

Row Direction

1
2
3
flex-direction: row;

Column Direction

1
2
3
flex-direction: column;

Center Alignment

A
B
justify-content: center;
align-items: center;

Wrap Items

Course 1
Course 2
Course 3
Course 4
Course 5
flex-wrap: wrap;

Space Between

Left
Right
justify-content: space-between;

Flex End

End
justify-content: flex-end;
align-items: flex-end;
Responsive Layout

Course Card Layout with Flexbox

Flexbox is excellent for responsive cards because items can wrap naturally.

HTML

Structure of webpages.

CSS

Design and styling.

JavaScript

Interactive behaviour.

Project

Build real webpage.

.course-grid {
    display: flex;
    gap: 18px;
    flex-wrap: wrap;
}

.course-card {
    flex: 1 1 180px;
}
Professional Tip: Use Flexbox for one-dimensional layouts like menus, rows, cards and alignment. Use CSS Grid for two-dimensional page layouts.
Try It Yourself

Practice Flexbox Layout

Edit Flexbox properties and click Run Code to see the layout change.

HTML + CSS Code Edit and run
Output Preview Browser result
Practice Task: Change navbar justify-content to center, course-card flex-basis to 250px, and add one more course card.
Common Mistakes

Mistakes Students Should Avoid

Flexbox becomes easy when parent and child roles are clear.

Wrong Habits

  • Applying flex properties to wrong element
  • Forgetting display:flex on parent
  • Using margin everywhere instead of gap
  • Not using flex-wrap for card layouts
  • Confusing justify-content and align-items

Correct Habits

  • Put display:flex on parent container
  • Use gap for spacing between items
  • Use flex-wrap for responsive cards
  • Use justify-content for main axis alignment
  • Use align-items for cross axis alignment
Remember: Flexbox works between a parent container and its direct child items.
Quick Quiz

Test Your Understanding

Select the correct answers and check your score.

1. Which property turns a container into a flex container?

display:flex turns a parent element into a flex container.

2. Which property controls row or column direction?

flex-direction controls whether items flow in row or column direction.

3. Which property adds space between flex items?

gap adds space between flex items.

4. Which property allows flex items to move to next line?

flex-wrap allows items to wrap to the next line.

5. justify-content aligns items on which axis?

justify-content aligns flex items along the main axis.
Quick Revision

Remember These Points

Revise these before moving to Chapter 20.

Flexbox

A one-dimensional CSS layout system for rows or columns.

display:flex

Turns a parent element into a flex container.

flex-direction

Controls row or column direction.

justify-content

Aligns items along the main axis.

align-items

Aligns items along the cross axis.

flex-wrap

Allows items to move to the next line.