CSS Flexbox: The Ultimate Guide to Layout Design

CSS Flexbox: The Ultimate Guide to Layout Design


Introduction

CSS Flexbox, or the Flexible Box Layout module, revolutionizes web design by making it easier to build responsive and dynamic layouts. Whether you're aligning items, distributing space, or handling layout responsiveness, Flexbox provides unparalleled control.

With its flexible approach to organizing elements along axes, Flexbox eliminates many traditional layout challenges, such as floating and manual positioning. This guide dives deep into all core aspects of Flexbox, including axes, alignment, wrapping, and item properties, helping you master this essential CSS feature for modern web design.

What is Flexbox?

Flexbox, short for the Flexible Box Layout, is a CSS layout module designed to arrange and align items within a container efficiently, even when their sizes are dynamic. It simplifies creating layouts by allowing developers to align and distribute space among elements, regardless of their size or the container's size.

Key Concepts of Flexbox

Flexbox works on two primary axes:

  1. Main Axis: The axis along which the flex items are arranged. Controlled by flex-direction.

  2. Cross Axis: The axis perpendicular to the main axis. Alignment along this axis is controlled by properties like align-items and align-content.

Flex Container and Flex Items

  • Flex Container: The parent element that holds flex items. Defined by display: flex or display: inline-flex.

  • Flex Items: The child elements inside a flex container.

Flexbox Cheat Sheet

1. Flex Container Properties

These properties are applied to the container.

PropertyDescriptionValues
displayDefines the container as a flex container.flex, inline-flex
flex-directionSpecifies the direction of the main axis and the order of flex items.row (default), row-reverse, column, column-reverse
justify-contentAligns items along the main axis.flex-start (default), flex-end, center, space-between, space-around, space-evenly
align-itemsAligns items along the cross axis (perpendicular to the main axis).stretch (default), flex-start, flex-end, center, baseline
align-contentAligns multiple lines along the cross axis when there are multiple rows (works only with flex-wrap).stretch (default), flex-start, flex-end, center, space-between, space-around
flex-wrapSpecifies whether items should wrap to the next line when there’s insufficient space.nowrap (default), wrap, wrap-reverse
gapDefines spacing between flex items.<length> (e.g., 10px, 1rem)

2. Flex Item Properties

These properties are applied to the child elements.

PropertyDescriptionValues
orderChanges the order of a flex item within the container.<integer> (default: 0)
flex-growSpecifies how much a flex item will grow relative to others.<number> (default: 0)
flex-shrinkSpecifies how much a flex item will shrink relative to others when space is insufficient.<number> (default: 1)
flex-basisSpecifies the initial size of a flex item before space is distributed.size (e.g., 100px), auto
flexA shorthand for flex-grow, flex-shrink, and flex-basis.none, auto, <flex-grow> <flex-shrink> <flex-basis>
align-selfAligns a specific flex item along the cross axis (overrides align-items).auto (default), flex-start, flex-end, center, baseline, stretch
<div class="container">
  <div class="item" style="order: 2;">1</div>
  <div class="item" style="order: 1;">2</div>
  <div class="item" style="order: 3;">3</div>
</div>

<style>
  .container {
    display: flex;
    justify-content: space-between;
  }
  .item {
    flex: 1;
    background: coral;
    margin: 10px;
    text-align: center;
  }
</style>

order reorders items without altering their HTML structure.

Main Axis and Cross Axis in Flexbox

1. Main Axis
The main axis is the primary axis along which flex items are laid out. The direction of this axis is determined by the flex-direction property.

  • Direction: It can be horizontal or vertical, depending on the value of flex-direction.

    • row (default): Horizontal (left to right).

    • row-reverse: Horizontal (right to left).

    • column: Vertical (top to bottom).

    • column-reverse: Vertical (bottom to top).

Terms:

  • Main-start: The starting point of the main axis, where the flex items begin to appear.

  • Main-end: The endpoint of the main axis, where the flex items finish.

  • Main size: The size of the flex item along the main axis. For horizontal layouts, this is the width, and for vertical layouts, this is the height.


2. Cross Axis

The cross axis is perpendicular to the main axis and is used for secondary alignment of items.

  • Direction: It is automatically set perpendicular to the main axis.

    • If flex-direction is row or row-reverse, the cross axis is vertical.

    • If flex-direction is column or column-reverse, the cross axis is horizontal.

Terms:

  • Cross-start: The starting point of the cross axis, where flex lines begin to fill.

  • Cross-end: The endpoint of the cross axis, where flex lines stop.

  • Cross size: The size of the flex item along the cross axis. For horizontal layouts, this is the height, and for vertical layouts, this is the width.

Example

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

<style>
  .container {
    display: flex;
    flex-direction: row; /* Main axis is horizontal */
    justify-content: flex-start; /* Align items along the main axis */
    align-items: center; /* Align items along the cross axis */
    height: 200px; /* Defines the cross size */
    border: 2px solid lightgray;
  }
  .item {
    width: 50px; /* Main size */
    height: 50px; /* Cross size */
    background: coral;
    margin: 10px;
  }
</style>
  • Main Axis: Horizontal (left to right) because flex-direction: row.

  • Cross Axis: Vertical (top to bottom).

  • Items are aligned along the main axis using justify-content and along the cross axis using align-items.

Row vs Column

Propertyflex-direction: rowflex-direction: column
Main AxisHorizontal (left to right)Vertical (top to bottom)
Cross AxisVertical (top to bottom)Horizontal (left to right)
Main-startLeftTop
Main-endRightBottom
Cross-startTopLeft
Cross-endBottomRight
Main SizeWidth of the flex itemsHeight of the flex items
Cross SizeHeight of the flex itemsWidth of the flex items

Flexbox Cheat Sheet

1. Flex Container Properties

These properties are applied to the container.

PropertyDescriptionValues
displayDefines the container as a flex container.flex, inline-flex
flex-directionSpecifies the direction of the main axis and the order of flex items.row (default), row-reverse, column, column-reverse
justify-contentAligns items along the main axis.flex-start (default), flex-end, center, space-between, space-around, space-evenly
align-itemsAligns items along the cross axis (perpendicular to the main axis).stretch (default), flex-start, flex-end, center, baseline
align-contentAligns multiple lines along the cross axis when there are multiple rows (works only with flex-wrap).stretch (default), flex-start, flex-end, center, space-between, space-around
flex-wrapSpecifies whether items should wrap to the next line when there’s insufficient space.nowrap (default), wrap, wrap-reverse
gapDefines spacing between flex items.<length> (e.g., 10px, 1rem)

2. Flex Item Properties

These properties are applied to the child elements.

PropertyDescriptionValues
orderChanges the order of a flex item within the container.<integer> (default: 0)
flex-growSpecifies how much a flex item will grow relative to others.<number> (default: 0)
flex-shrinkSpecifies how much a flex item will shrink relative to others when space is insufficient.<number> (default: 1)
flex-basisSpecifies the initial size of a flex item before space is distributed.<length> (e.g., 100px), auto
flexA shorthand for flex-grow, flex-shrink, and flex-basis.none, auto, <flex-grow> <flex-shrink> <flex-basis>
align-selfAligns a specific flex item along the cross axis (overrides align-items).auto (default), flex-start, flex-end, center, baseline, stretch