Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

CSS Flexbox: Modern Layout Control

Flexbox is a modern and efficient method for creating responsive layouts in CSS. Unlike classic techniques, Flexbox provides control over both the main and cross axes, precise alignment, and automatic space distribution. In this tutorial, we'll explore step by step how Flexbox works and how we can use it in real projects.

1. display

Activates Flexbox mode on a container.

🔧 Live Example

2. flex-direction

Controls the direction in which elements are arranged inside the container.

🔧 Live Example

3. justify-content

Controls the distribution of elements along the main axis (horizontal if flex-direction: row, vertical if flex-direction: column).

🔧 Live Example

4. align-items

Controls the alignment of items along the cross axis (vertical if flex-direction: row, horizontal if flex-direction: column).

🔧 Live Example

5. flex-wrap

Controls whether the items in the container wrap onto multiple lines when there's no more space on the main axis.

🔧 Live Example

6. align-content

Controls the alignment of multiple lines along the cross axis, but only works when there are multiple rows (so flex-wrap must be active).

🔧 Live Example

7. order

Controls the display order of elements in a Flexbox container without changing their order in the HTML.

🔧 Live Example

8. flex-grow

Controls how much an element can grow to occupy the available space in the container.

🔧 Live Example

9. flex-shrink

Controls how much an element shrinks when space is limited.

🔧 Live Example

10. flex-basis

Defines the base size of an element before the available space is distributed. It's similar to width, but works within the Flexbox context.

🔧 Live Example

11. flex (shorthand)

This is a shorthand for flex-grow, flex-shrink, and flex-basis.

🔧 Live Example

12. align-self

Allows an element to control its own alignment along the cross axis, overriding the container's align-items.

🔧 Live Example

Flexbox Cheat Sheet

Property Values Description
display flex, inline-flex Activates Flexbox mode
flex-direction row, row-reverse, column, column-reverse Direction of items in the container
justify-content flex-start, flex-end, center, space-between, space-around, space-evenly Alignment on the main axis
align-items stretch, flex-start, flex-end, center, baseline Alignment on the cross axis
flex-wrap nowrap, wrap, wrap-reverse Wrapping items onto multiple lines
align-content flex-start, flex-end, center, space-between, space-around, stretch Alignment of multiple lines
order 0, 1, 2, ... Display order
flex-grow 0, 1, 2, ... Growth based on available space
flex-shrink 0, 1, 2, ... Shrinkage when space is limited
flex-basis auto, 0, px, % Base size
flex shorthand: grow shrink basis Shorthand for the three above
align-self auto, flex-start, flex-end, center, baseline, stretch Individual alignment on the cross axis

Example: Responsive Card Layout

A card layout that automatically adapts to the screen size using flex-wrap and flex-basis.

🔧 Live Example

Example: Navbar with Flexbox

A simple horizontally aligned navbar with a logo on the left and links on the right, using display: flex and justify-content: space-between.

🔧 Live Example

Complete Layout for a Mini Site

A solid example of web structure — with header, navigation, sidebar, main content, and footer — built with clean HTML and CSS. Ideal for simple projects, dashboards, or personal websites. Everything is modular and easy to extend with PHP, JS, or modern frameworks.

Complete layout with logo and menu

Flexbox gave you control over horizontal and vertical alignment, but if you want to orchestrate complex layouts like a web architect, it's time to move on to CSS Grid — where every row and column is under your command. Let's get started.


🧠 Quiz – CSS Flexbox: Modern Layout Control

Top