CSS Flexbox

Dr. Greg Bernstein

Updated September 8th, 2019

CSS Layout Flexbox

Readings

  1. MDN Flexbox

  2. A Complete Guide to Flexbox

What is it?

From MDN:

Flexbox is a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spaces.

What is it? II

From CSS Tricks:

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.

Why Flexbox

From MDN: “The following simple layout requirements are either difficult or impossible to achieve with such tools, in any kind of convenient, flexible way:”

  • Vertically centering a block of content inside its parent.
  • Making all the children of a container take up an equal amount of the available width/height, regardless of how much width/height is available.
  • Making all columns in a multiple column layout adopt the same height even if they contain a different amount of content.

Simple Flexbox Example I

Example from MDN, three articles within a section, default (no flex):

Simple Flexbox Example II

Applying display: flex to the section containing the articles:

Basic Model/Terminology

From MDN:

  • The main axis is the axis running in the direction the flex items are being laid out in. The start and end of this axis are called the main start and main end.
  • The cross axis is the axis running perpendicular to the direction the flex items are being laid out in. The start and end of this axis are called the cross start and cross end.
  • The parent element that has display: flex set on it is called the flex container.
  • The items being laid out as flexible boxes inside the flex container are called flex items.

Basic Model II

From MDN:

Live Practice Files

Play with these are you read these notes:

  • From MDN, container is the section element
  • From me, container is the p element holding the links to other pages

Flex Container Properties

display and flex-direction

  • Use display: flex; on the container element to enable flex layout.
  • Use flex-direction: property, where property = row, row-reverse, column or column-reverse
  • The default flex-direction is row

flex-wrap and flex-flow

From CSS-Tricks:

  • By default, flex items will all try to fit onto one line. You can change that and allow the items to wrap as needed with the flex-wrap property.
  • flex-wrap values: nowrap, wrap, wrap-reverse
  • flex-flow is a shorthand flex-direction and flex-wrap properties, which together define the flex container’s main and cross axes.
  • Default is row nowrap.

justify-content

From CSS-Tricks:

This defines the alignment along the main axis. It helps distribute extra free space left over when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size.

justify-content values I

From CSS-Tricks:

  • flex-start (default): items are packed toward the start line
  • flex-end: items are packed toward to end line
  • center: items are centered along the line
  • space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line

justify-content values II

From CSS-Tricks:

  • space-around: items are evenly distributed in the line with equal space around them.
  • space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is equal.

justify-content example I

space-around

justify-content example II

space-between

justify-content example III

center

align-items

From CSS-Tricks:

This defines the default behavior for how flex items are laid out along the cross axis on the current line.

align-items

From CSS-Tricks:

  • start: cross-start margin edge of the items is placed on the cross-start line
  • end: cross-end margin edge of the items is placed on the cross-end line
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align
  • stretch (default): stretch to fill the container (still respect min-width/max-width)

align-items: start

align-items: end

align-items: center

align-content

From CSS-Tricks align-content:

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.

Note: this property has no effect when there is only one line of flex items.

Flex Item Properties

order and flex-grow

From CSS-Tricks:

  • order: “By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.”

  • flex-grow: “This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.”

flex-shrink and flex-basis

From CSS-Tricks:

  • flex-shrink: This defines the ability for a flex item to shrink if necessary.
  • flex-basis: This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword.

flex

From CSS-Tricks:

This is the shorthand for flex-grow, flex-shrink and flex-basis combined. The second and third parameters (flex-shrink and flex-basis) are optional. Default is 0 1 auto.

It is recommended that you use this shorthand property rather than set the individual properties. The short hand sets the other values intelligently.

align-self

From CSS-Tricks:

This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.

Note: The default alignment is the alignment set on the container.