CSS Flexbox

In this lecture, we'll explore the CSS Flexible Box Layout, commonly known as Flexbox. This is a one-dimensional layout model that offers space distribution between items in an interface and powerful alignment capabilities.

Desired Outcomes

By the end of this lecture, you should be able to:

  • Understand the basic principles of the Flexbox layout.
  • Create a flexible layout using Flexbox properties.

Flexbox is a layout model that allows elements within a container to be automatically arranged depending upon screen size or viewing environment. It simplifies layouts in CSS as flex containers manage the layout of their children.

The two axes of flexbox

Flexbox operates along two primary axes — the main axis and the cross axis. The direction of the main axis is set by the flex-direction property, while the cross axis runs at a right angle to it. All interactions with flexbox rely on these axes, so it's essential to comprehend their workings from the start.

The main axis

The main axis's direction is determined by the flex-direction property, which can take on four possible values:

  • row
  • row-reverse
  • column
  • column-reverse

If you choose row or row-reverse, the main axis stretches along the row in the inline direction. Conversely, selecting column or column-reverse makes the main axis extend from the top to the bottom of the page, following the block direction.

Flexbox Main Axis - Direction Row
Flexbox Main Axis - Direction Row

Choose column or column-reverse and your main axis will run from the top of the page to the bottom — in the block direction.

Flexbox Main Axis - Direction Column
Flexbox Main Axis - Direction Column

The cross axis

The cross axis runs at a right angle to the main axis. Therefore, if your flex-direction (main axis) is either row or row-reverse, the cross axis goes down the columns.

Flexbox Cross Axis - Direction Row
Flexbox Cross Axis - Direction Row

If the main axis is column or column-reverse, then the cross axis traverses along the rows.

Flexbox Cross Axis - Direction Column
Flexbox Cross Axis - Direction Column

Start and end lines

A critical point to note about flexbox is that it doesn't pre-suppose the writing mode of the document. Unlike previous CSS methods that predominantly favored horizontal and left-to-right writing modes, contemporary layout methods cover a spectrum of writing modes, eliminating assumptions that a text line begins at the top left of a document and continues towards the right, with new lines appearing below one another.

It's essential to understand why, when discussing the direction of our flex items flow, we don't refer to left and right or top and bottom. If the flex-direction is row and I'm working in English, the main axis's start edge will be on the left, and the end edge on the right.

If I'm working in Arabic, the main axis's start edge would be on the right and the end edge on the left.

Flexbox Main Axis - Direction Row Reverse
Flexbox Main Axis - Direction Row Reverse

In both scenarios, the start edge of the cross axis is at the top of the flex container and the end edge at the bottom, given both languages employ a horizontal writing mode.

After some time, thinking about the start and end rather than left and right becomes second nature and will prove beneficial when working with other layout methods such as CSS Grid Layout, which follow the same conventions.

Flex Container

An area of a document laid out using flexbox is called a flex container. To create a flex container, we set the value of the area's container's display property to flex or inline-flex.

  • display: Defines a flex container.
  • flex-direction: Defines the direction items are placed in the container.
  • flex-wrap: Specifies whether items should wrap or not.
  • flex-flow: Shorthand for flex-direction and flex-wrap.
  • justify-content: Defines the alignment along the main axis.
  • align-items: Defines the default behavior for how items are laid out along the cross axis.
  • align-content: Similar to align-items, but for multiple lines of items.
.container {
  display: flex;
}

Flex Direction

Once you have a flex container, you can set the direction of the flex items. By default, they are set to row, meaning they align horizontally. But you can also set them to column for vertical alignment. This is done using the flex-direction property:

.container {
  display: flex;
  flex-direction: row; /* default value; can be omitted */
}
Flexbox Direction Row
Flexbox Direction Row
.container {
  display: flex;
  flex-direction: column; 
}
Flexbox Direction Column
Flexbox Direction Column

You can also reverse the order of the items using row-reverse or column-reverse:

.container {
  display: flex;
  flex-direction: row-reverse;
}
Flexbox Direction Row Reverse
Flexbox Direction Row Reverse

Flex Wrap

By default, flex items will try to fit onto one line. To change this, use the flex-wrap property:

.container {
  display: flex;
  flex-wrap: wrap; /* allows items to wrap onto multiple lines */
}
Flexbox Wrap
Flexbox Wrap

You can also use wrap-reverse to wrap items onto multiple lines in reverse order.

.container {
  display: flex;
  flex-wrap: wrap-reverse; /* allows items to wrap onto multiple lines */
}
Flexbox Wrap Reverse
Flexbox Wrap Reverse

Flex Flow

The flex-flow property is a shorthand property for setting flex-direction and flex-wrap. The default is row nowrap:

.container {
  display: flex;
  flex-flow: row wrap;
}

Justify Content

The justify-content property is used to align the flex items along the horizontal line in the container:

center: This aligns items to the center of the flex container.

.container {
  display: flex;
  justify-content: center; /* centers items horizontally */
}
Flexbox Justify Content Center
Flexbox Justify Content Center

flex-start: This aligns items to the start of the flex container.

.container {
  display: flex;
  justify-content: flex-start;
}
Flexbox Justify Content Flex Start
Flexbox Justify Content Flex Start

flex-end: This aligns items to the end of the flex container.

.container {
  display: flex;
  justify-content: flex-end;
}
Flexbox Justify Content Flex End
Flexbox Justify Content Flex End

space-between: This aligns items so that the first item is at the start, the last item is at the end, and all remaining space is distributed evenly between the items.

.container {
  display: flex;
  justify-content: space-between;
}
Flexbox Justify Content Space Between
Flexbox Justify Content Space Between

space-around: This aligns items so that there are equal spaces around them. The space between the first/last item and the container edge is half the space between items.

.container {
  display: flex;
  justify-content: space-around;
}
Flexbox Justify Content Space Around
Flexbox Justify Content Space Around

space-evenly: This aligns items so that there is an equal amount of space between each item, and between the items and the edges of the container.

.container {
  display: flex;
  justify-content: space-evenly;
}
Flexbox Justify Content Space Evenly
Flexbox Justify Content Space Evenly

With the above properties, you can easily control the alignment and distribution of your flex items.

Align Items

The align-items property is used to align the flex items along the vertical line in the container:

Other values for align-items include flex-start, flex-end, baseline, and stretch.

.container {
  display: flex;
  align-items: center; /* centers items vertically */
}

flex-start: This aligns items to the start of the flex container.

.container {
  display: flex;
  align-items: flex-start;
}
Flexbox Align Items Flex Start
Flexbox Align Items Flex Start
.container {
  display: flex;
  align-items: flex-end;
}
Flexbox Align Items Flex End
Flexbox Align Items Flex End

center: This aligns items to the center of the flex container.

.container {
  display: flex;
  align-items: center;
}
Flexbox Align Items Center
Flexbox Align Items Center

baseline: This aligns items along their baseline. The baseline is where the text sits.

.container {
  display: flex;
  align-items: baseline;
}
Flexbox Align Items Baseline
Flexbox Align Items Baseline

stretch: This stretches the items to fill the container. If you don't specify a height, or set it to auto, the items will stretch to fill the container vertically.

.container {
  display: flex;
  align-items: stretch;
}
Flexbox Align Items Stretch
Flexbox Align Items Stretch

Flex Item

Flex Items are direct children of a Flex Container (elements with display: flex or display: inline-flex).

  • order: Controls the order in which items appear within the flex container.
  • flex-grow: Determines how much a flex item will grow relative to the rest of the flex items.
  • flex-shrink: Determines how much a flex item will shrink relative to the rest of the flex items.
  • flex-basis: Specifies the initial main size of a flex item.
  • flex: Shorthand for flex-grow, flex-shrink, and flex-basis.
  • align-self: Allows for overriding a flex container's align-items value for individual flex items.

Here's an example of a basic flex container and flex items:

.container {
  display: flex;
  justify-content: space-around;
}

.item {
  flex-grow: 1;
}

Flex Grow, Shrink, and Basis

You can use the flex shorthand to set these three properties at once. The flex property takes the grow, shrink, and basis values, in that order:

.item {
  flex: 1 0 auto;
}