Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Flexbox


CSS Flexbox

The CSS Flexible Box Layout (or Flexbox) makes it easy to design flexible, responsive layout structures that adapt to different screen sizes.


What is Flexbox?

Flexbox is a layout model that arranges elements in rows or columns and automatically adjusts their size to fit the available space.

  • Items can stretch or shrink to fill space.
  • You can align and distribute space between items easily.
  • Works great for navigation bars, cards, and responsive grids.

Example

Basic Flexbox container:

.container {
  display: flex;
  background-color: lightgray;
}

.item {
  background-color: coral;
  margin: 10px;
  padding: 20px;
  color: white;
}
Try it Yourself »

Flex Container and Flex Items

A Flexbox layout consists of a container and one or more flex items.

  • Set display: flex; on the parent element (the container).
  • All direct children become flex items.

Flex Direction

The flex-direction property defines the direction of the items inside the container.

  • row (default) - items are placed left to right.
  • row-reverse - right to left.
  • column - top to bottom.
  • column-reverse - bottom to top.

Example

Changing direction:

.container {
  display: flex;
  flex-direction: row-reverse;
}
Try it Yourself »

Justify Content

The justify-content property aligns items along the main axis (horizontal by default).

  • flex-start - items at the start (default).
  • center - items centered.
  • flex-end - items at the end.
  • space-between - even spacing between items.
  • space-around - equal space around each item.

Example

Centering items horizontally:

.container {
  display: flex;
  justify-content: center;
}
Try it Yourself »

Align Items

The align-items property aligns items vertically (along the cross axis).

  • stretch (default)
  • flex-start
  • center
  • flex-end
  • baseline

Example

Centering items vertically:

.container {
  display: flex;
  align-items: center;
  height: 200px;
  background-color: lightgray;
}
Try it Yourself »

Align Content

If items wrap into multiple lines, align-content controls vertical spacing between lines.

  • stretch (default)
  • center
  • flex-start
  • flex-end
  • space-between
  • space-around

Flex Wrap

By default, items try to fit on one line. Use flex-wrap to allow wrapping.

Example

Wrapping items:

.container {
  display: flex;
  flex-wrap: wrap;
}
Try it Yourself »

Align Self

Individual items can override vertical alignment using align-self.

Example

Different alignments per item:

.item1 { align-self: flex-start; }
.item2 { align-self: center; }
.item3 { align-self: flex-end; }
Try it Yourself »

flex-grow, flex-shrink, and flex-basis

These properties control how flex items resize inside a flex container:

  • flex-grow - how much an item can grow relative to others.
  • flex-shrink - how much it can shrink.
  • flex-basis - the default size before growing/shrinking.

Example

Flexible item growth:

.item1 { flex-grow: 1; }
.item2 { flex-grow: 2; }
.item3 { flex-grow: 1; }
Try it Yourself »

flex Shorthand

You can combine flex-grow, flex-shrink, and flex-basis into one line:

Example

Shorthand property:

.item { flex: 1 1 200px; }
Try it Yourself »

Flexbox Centering Example

Flexbox makes centering items (both horizontally and vertically) very easy:

Example

Perfectly centered box:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  background: lightgray;
}
Try it Yourself »

Best Practices

  • Use Flexbox for one-dimensional layouts (rows or columns).
  • Use justify-content and align-items to control alignment.
  • Use flex-wrap for responsive rows.
  • Use flex: 1; to make items share equal space.

Next, you'll learn about CSS Grid - a powerful system for creating two-dimensional layouts with rows and columns.

Next » CSS Grid


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.