Back to Tutorials
CSS
25 min read
Sahasransu Satpathy
12/1/2025

CSS Flexbox Complete Guide

Learn CSS Flexbox from basics to advanced layouts with practical examples

Introduction

CSS Flexbox is a modern layout module that makes it easy to design flexible and responsive layouts. This guide covers all essential Flexbox concepts with examples.


Step 1: Understanding Flex Container

The parent element becomes a flex container by setting `display: flex`:

```css .container { display: flex; border: 2px solid #ccc; padding: 10px; } ```


Step 2: Flex Direction

Control the direction of flex items using `flex-direction`:

```css /* Row (default) */ .container { flex-direction: row; }

/* Column */ .container { flex-direction: column; } ```


Step 3: Justify Content

Align items horizontally along the main axis:

```css .container { justify-content: flex-start; /* flex-start, center, flex-end, space-between, space-around */ } ```


Step 4: Align Items

Align items vertically along the cross axis:

```css .container { align-items: stretch; /* flex-start, center, flex-end, stretch, baseline */ } ```


Step 5: Flex Wrap

Allow flex items to wrap onto multiple lines:

```css .container { flex-wrap: wrap; /* nowrap, wrap, wrap-reverse */ } ```


Step 6: Flex Item Properties

Control individual flex items:

```css .item { flex-grow: 1; /* grow to fill space / flex-shrink: 1; / shrink if needed / flex-basis: 100px; / initial size */ } ```


Step 7: Practical Example

Create a responsive card layout:

```html

<div class="container"> <div class="item">Card 1</div> <div class="item">Card 2</div> <div class="item">Card 3</div> </div> \`\`\`

```css .container { display: flex; flex-wrap: wrap; gap: 10px; }

.item { flex: 1 1 200px; background-color: #4CAF50; color: white; padding: 20px; text-align: center; } ```


Step 8: Tips & Best Practices

  • Always use `flex` shorthand for simpler code.
  • Use `gap` instead of margins for spacing.
  • Combine Flexbox with media queries for responsive design.

Conclusion

Mastering Flexbox enables you to create clean, responsive layouts with less code. Practice by building real-world UI components like navigation bars, card grids, and footers.


SEO Suggestions:

  • Main keywords: CSS Flexbox tutorial, Flexbox guide, responsive layouts, CSS layout techniques
  • Meta description: Learn CSS Flexbox from basics to advanced layouts with practical examples. Step-by-step tutorial for 2025.
  • Catchy title suggestions: "CSS Flexbox Complete Guide 2025", "Master CSS Flexbox: Step-by-Step Tutorial"

Previous Tutorial

Browse All Tutorials