• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 2.1: Introduction to Flexbox

Lesson 2.1: Introduction to Flexbox

2 min read

Introduction

Flexbox (Flexible Box Module) is a one-dimensional CSS layout module designed to distribute space among items in a container, even when their size is unknown or dynamic. It's perfect for arranging items in a single row or column, making it much easier to align, space, and order elements than traditional float-based layouts.

Key Concepts

Flex Container and Flex Items

Flexbox works by setting a parent element as a flex container and its direct children as flex items.

  • To create a flex container, simply apply display: flex; or display: inline-flex; to the parent element.

Main Axis and Cross Axis

Flexbox operates along two axes:

  • Main Axis: The primary axis along which flex items are laid out. Its direction is determined by flex-direction (default is row).

  • Cross Axis: The axis perpendicular to the main axis.

Essential Flex Container Properties

  • flex-direction: Sets the direction of the main axis (row, row-reverse, column, column-reverse).

  • justify-content: Aligns items along the main axis (flex-start, flex-end, center, space-between, space-around, space-evenly).

  • align-items: Aligns items along the cross axis (flex-start, flex-end, center, baseline, stretch).

  • flex-wrap: Controls whether flex items are forced onto one line or can wrap onto multiple lines (nowrap, wrap, wrap-reverse).

Quick Example

Let's create a simple horizontal navigation menu using Flexbox:

html
<nav class="navbar"> <a href="#">Home</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Contact</a> </nav> css .navbar { display: flex; justify-content: space-around; /* Distribute items evenly */ align-items: center; /* Vertically center items */ background-color: #333; padding: 10px; } .navbar a { color: white; text-decoration: none; padding: 8px 15px; border-radius: 5px; } .navbar a:hover { background-color: #555; } ## Summary Flexbox is a powerful tool for one-dimensional layouts, simplifying the alignment and distribution of items within a container. By mastering properties like `display: flex`, `flex-direction`, `justify-content`, and `align-items`, you can create robust and responsive components like navigation bars, card layouts, and more.
End of lesson
👏Well done!
Previous Lesson
Quiz: Module 1: CSS Fundamentals
Next Lesson
Lesson 2.2: Introduction to CSS Grid

Course Content

0% Complete0/11 Lessons

Lesson 1.1: Introduction to CSS

Lesson 1.2: CSS Selectors and Properties

Lesson 1.3: The CSS Box Model

Quiz

Lesson 2.1: Introduction to Flexbox

Lesson 2.2: Introduction to CSS Grid

Lesson 2.3: Positioning Elements

Quiz

Course Content

0% Complete0/11 Lessons

Lesson 1.1: Introduction to CSS

Lesson 1.2: CSS Selectors and Properties

Lesson 1.3: The CSS Box Model

Quiz

Lesson 2.1: Introduction to Flexbox

Lesson 2.2: Introduction to CSS Grid

Lesson 2.3: Positioning Elements

Quiz