Getting Started with Flexbox

Background

The Flexbox Layout (Flexible Box) module (a W3C Candidate Recommendation as of October 2017) aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word “flex”).

The main idea behind the flex layout is to give the container the ability to alter its items’ width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space or shrinks them to prevent overflow.

Note: Flexbox layout is most appropriate to the components of an application, and small-scale layouts, while the Grid layout is intended for larger scale layouts.


Basics and terminology

Flexbox is an entire module rather than a single property, it involves several different things in addition to its entire set of properties. Some of them should be placed on the container (parent element, also known as "flex container"), while others should be placed on the children (also known as "flex items").

A diagram explaining flexbox terminology. The size across the main axis of flexbox is called the main size, the other direction is the cross size. Those sizes have a main start, main end, cross start, and cross end.

Items will be laid out following either the main axis (from main-start to main-end) or the cross axis (from cross-start to cross-end).

main axis

The main axis of a flex container is the primary axis along which flex items are laid out. It is not necessarily horizontal; it depends on the flex-direction property (see below).

main-start | main-end

The flex items are placed within the container starting from main-start and going to main-end.

main size

A flex item’s width or height, whichever is in the main dimension, is the item’s main size. The flex item’s main size property is either the ‘width’ or ‘height’ property, whichever is in the main dimension.

cross axis

The axis perpendicular to the main axis is called the cross-axis. Its direction depends on the main axis direction.

cross-start | cross-end

Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side.

cross size

The width or height of a flex item, whichever is in the cross dimension, is the item’s cross-size. The cross-size property is whichever of "width" or "height" that is in the cross dimension.



Flex properties

Display

This defines a flex container; inline or block depending on the given value. It enables a flex context for all its direct children.

Flex-direction

Sets the direction of the flex-items.

the four possible values of flex-direction being shown: top to bottom, bottom to top, right to left, and left to right

  • row (default): left to right, right to left

  • row-reverse: right to left, left to right

  • column: same as row but top to bottom

  • column-reverse: same as row-reverse but bottom to top

Flex-wrap

Flex items will by default attempt to fit on a single line. With this property, you can alter that and permit the pieces to wrap as necessary.

two rows of boxes, the first wrapping down onto the second

  • nowrap (default): all flex items will be on one line

  • wrap: flex items will wrap onto multiple lines, from top to bottom.

  • wrap-reverse: flex items will wrap onto multiple lines from bottom to top.

Justify-Content

flex items within a flex container demonstrating the different spacing options

justify-content is a CSS property that helps to align and distribute flex items along the main axis of a flex container. It controls the horizontal alignment of the flex items when the flex-direction is set to either row or row-reverse.

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

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

  3. center: This centers the flex items along the main axis of the flex container.

  4. space-between: This distributes the flex items evenly along the main axis of the flex container. The first item is placed at the start of the main axis, the last item is placed at the end, and the remaining items are evenly distributed in between.

  5. space-around: This distributes the flex items evenly along the main axis of the flex container, with equal space around each item.

  6. space-evenly: This distributes the flex items evenly along the main axis of the flex container, with equal space between and around each item.

Align-items

demonstration of differnet alignment options, like all boxes stuck to the top of a flex parent, the bottom, stretched out, or along a baseline

align-items is a CSS property that helps to align flex items along the cross-axis of a flex container. It controls the vertical alignment of the flex items when the flex-direction is set to either column or column-reverse.

  1. stretch: This is the default value, and it stretches the flex items to fill the container along the cross-axis.

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

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

  4. center: This centers the flex items along the cross-axis of the flex container.

  5. baseline: This aligns the flex items such that their baselines are aligned.

Align-content

examples of the align-content property where a group of items cluster at the top or bottom, or stretch out to fill the space, or have spacing.

This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.

This property only takes effect on multi-line flexible containers, where flex-wrap is set to either wrap or wrap-reverse). A single-line flexible container (i.e. where flex-wrapis set to its default value, no-wrap) will not reflect align-content.

Gap

gap is a CSS property that sets the size of the gap between the rows and columns of a grid container. It is a shorthand property for the row-gap and column-gap properties, which set the size of the gap between the rows and columns of a grid container, respectively.

Order

Diagram showing flexbox order. A container with the items being 1 1 1 2 3, -1 1 2 5, and 2 2 99.

The order CSS property sets the order to lay out an item in a flex or grid container.

.item {
  order: 3; /* default is 0 */
}

flex-grow

flex-grow is a CSS property that defines the ability of a flex item to grow and fill any remaining space along the main axis of a flex container.

two rows of items, the first has all equally-sized items with equal flex-grow numbers, the second with the center item at twice the width because its value is 2 instead of 1.