A Guide To CSS Flexbox

CSS flexbox is a one-dimensional layout pattern that makes it easy to design flexible and effective layouts. It uses a flex container. 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). The flex container adjusts the space between the content.

Since flexbox is a whole module and not a single property, it involves a lot of things including its whole set of properties. Some of them are meant to be set on the container (parent element, known as “flex container”) whereas the others are meant to be set on the children (said “flex items”).

main axis — It is the primary axis of the flex container along which flex items are laid out. It depends on the flex-direction property.

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 — It is 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.

Parent Container Properties

These are some of the properties that can be applied to a flex parent container:

  • display
  • flex-direction
  • flex-wrap
  • flex-flow
  • justified-content
  • align-content
  • align-items

display

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

output

flex-direction

This establishes the main-axis, thus defining the direction flex items are placed in the flex container. Flexbox is (aside from optional wrapping) a single-direction layout concept. Think of flex items as primarily laying out either in horizontal rows or vertical columns.

It can accept multiple values apart from row,

  • row (default): left to right
  • row-reverse: right to left
  • column: top to bottom
  • column-reverse: bottom to top

flex-wrap

By default, flex items will all try to fit onto one line. We can use this property to control the wrapping the items in the container. It is helpful when width of the browser changes it adjusts the width to make visible all the items.

  • 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.

flex-flow

This is a shorthand property for flex-direction and flex-wrap. By default it is set to row and nowrap for flex-direction and flex-wrap respectively.

justified-content

This defines the alignment along the main axis. It helps distribute extra space leftover during the alignment of items. It also exerts some control over the alignment of items when they overflow the line.

There are six possible values,

  • flex-start
  • flex-end
  • center
  • space-between
  • space-around
  • space-evenly
  • flex-start (default): items are packed toward the start of the flex-direction.
  • flex-end: items are packed toward the end of the flex-direction.
  • center: items are centered along the line.
  • space-between: items are evenly distributed in the line; first item is on the left end, last item on the right end.
  • 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 is equal.

align-items

This property defines the behavior for how flex items are arranged along the cross-axis on the current line.

There are various values to this property,

  • stretch
  • flex-start
  • flex-end
  • center
  • baseline
  • stretch (default) : It will stretch to fill the container from end to end.
  • flex-start: Items are placed at the start of the cross axis.
  • flex-end: Items are placed at the end of the cross axis.
  • center: Items are centered in the cross-axis.
  • baseline: items are aligned such as their baselines align.

align-content

This property aligns the lines of a flex-container within when there is extra space in the cross-axis. It is similar to justify-content, the only difference is that in justify-content items are aligned along the main-axis here items are aligned along cross-axis. It only works when there are multiple rows in the flex-container.

We can assign different values to the align-content property,

  • stretch
  • flex-start
  • flex-end
  • center
  • space-between
  • space-around
  • stretch: lines stretch to take up the remaining space
  • flex-start: flex-items are packed at the start of the container.
  • flex-end: flex-items packed to the end of the container.
  • center: flex-items centered in the container.
  • space-between: flex-items are evenly distributed, the first line is at the start of the container while the last one is at the end.
  • space-around: flex-items are evenly distributed with equal space around each line.
  • space-evenly: flex- items are evenly distributed with equal space around them.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store