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 INTRO TO HTML & CSS BASH RUST

HTML and CSS Flexbox Layout


HTML & CSS: Flexbox Layout

Flexbox (Flexible Box Layout) excels at arranging components in one dimension, either rows or columns, while distributing space and aligning items responsively.


Why flexbox?

  • Simple alignment: center items vertically and horizontally without hacks.
  • Adaptive spacing: flex items expand, shrink, or wrap within available space.
  • Component-friendly: build navbars, cards, media objects, toolbars, and forms.

If you want to read more about CSS Flexbox or get an in-depth understanding, go to CSS Flexbox in the CSS tutorial.


Creating a flex container

Enable Flexbox by setting display: flex; (row) or display: inline-flex;. Use flex-direction to switch to columns.

Note: A flex container is the parent element.

Its children become flex items that follow the rules below.

Basic horizontal layout

.toolbar {
  display: flex;
  gap: 16px;
  align-items: center;
}
.toolbar .spacer {
  flex: 1 1 auto;
}
Try it Yourself »


Axis and alignment

  • Main axis follows flex-direction. Control distribution with justify-content.
  • Cross axis is perpendicular. Align items with align-items or per-item align-self.
  • Use gap for consistent spacing without extra margins.

Note: If flex-direction is row, the main axis is horizontal and the cross axis is vertical.

Switching to column flips them.

Card strip

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
  <section class="cards">
    <article>
      <h2>Prototype</h2>
      <p>Validate ideas quickly with interactive mockups.</p>
    </article>
    <article>
      <h2>Build</h2>
      <p>Create accessible, responsive components in code.</p>
    </article>
    <article>
      <h2>Ship</h2>
      <p>Automate deployment pipelines and monitoring.</p>
    </article>
  </section>
</body>
</html>
.cards {
  display: flex;
  gap: 24px;
  justify-content: center;
  align-items: stretch;
  flex-wrap: wrap;
}
.cards article {
  flex: 1 1 220px;
  padding: 24px;
  border-radius: 16px;
  background: #f1f5f9;
}
Try it Yourself »

Controlling flex items

  • flex: grow shrink basis; defines how items consume space (default 0 1 auto).
  • flex-basis sets ideal size before free space is distributed.
  • Combine flex shortcuts like flex: 1; (equal widths) or flex: 0 0 300px; (fixed width).

Note: The three numbers in flex mean grow, shrink, and base size.

For example, flex: 1 lets items grow equally.


Ordering and wrapping

  • flex-wrap: wrap; breaks items into new rows or columns.
  • order reorders items visually without changing the DOM.
  • Use align-content when multiple flex lines exist.

Note: Wrapping stops items from squishing too small; they move to the next line instead.


Common patterns

  • Toolbar with growing spacer.
  • Split layout: two columns with flex: 2 and flex: 1.
  • Responsive media object: image + text that stacks on small screens.


×

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.