Skip to content

Instantly share code, notes, and snippets.

@jbracht
Created September 18, 2019 00:03
Show Gist options
  • Save jbracht/8fb156f50d022c25507ac2956f2c792d to your computer and use it in GitHub Desktop.
Save jbracht/8fb156f50d022c25507ac2956f2c792d to your computer and use it in GitHub Desktop.

Flexbox

"Flexible Box Model" - aims to provide a more efficient way to style pages

  • Items contained inside a flex container with "flex" to best fit the available space.
  • Flexbox layout is direction-agnostic making it more flexible than other layout models (block layout being vertically-biased, and inline layout being horizontally-biased).
  • Flexy layouts are responsive – they will grow/shrink as specified to fit different screen sizes

Terminology

The flex layout is based on "flex-flow directions". If the flex-direction is set to row (default), the flex axes will look as follows:

flex-flow

Why not just call the axes x and y like everything else? Well, this would assume that the x is horizontal, and the y is vertical. However, with flexbox, this is not always the case, since it flips depending on the flex direction! So instead, we have these:

  • main axis: By default, this will be horizontal. Technically speaking, it is the primary axis along which flex items are laid out. If the flex-direction is set to column, the main axis will now be vertical!
  • cross axis: By default, this will be vertical. It will always be perpendicular to the main axis.

(Some) Container Properties

These properties will apply to the container, and affect the arrangement of the items it contains.

  • display: This will kick the whole thing off! (display: flex;)
  • flex-direction: Establish the main axis. This will tell flex items inside the flex container how to align themselves.
  • flex-wrap: Will the items in the container wrap at all?
  • justify-content: Defines alignment of items in the container along the main axis.
  • align-items: Defines alignment of items in the container along the cross axis. (This is like the justify-content for the cross axis)

(Some) Item Properties

These properties will apply to the specific items they are placed on.

  • flex-grow: An item can grow if necessary. (An item with flex-grow: 2 will try to take up 2x as much space as other items with flex-grow: 1)
  • flex-shrink: An item can shrink if necessary.
  • align-self: Override what is specified with align-items for this specific item.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment