Flexbox: Advanced Web Design

Posted on:  


Basics and terminology

Download Flexbox Poster Here!

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”).

If “regular” layout is based on both block and inline flow directions, the flex layout is based on “flex-flow directions”. Please have a look at this figure from the specification, explaining the main idea behind the flex layout.

![](https://image-control-storage.s3.amazonaws.com/2023/04/06111959/image-45-1024x463.png)

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

Flexbox properties

![Flexbox properties](https://image-control-storage.s3.amazonaws.com/2023/04/06111931/image-44-1024x439.png)

Properties for the Parent

(flex container)

display

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

.container {
  display: flex; <em>/* or inline-flex */</em>
}

Note that CSS columns have no effect on a flex container.

flex-direction

![](https://image-control-storage.s3.amazonaws.com/2023/04/06111905/image-43-1024x484.png)

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.

.container {
  flex-direction: row | row-reverse | column | column-reverse;
}

flex-wrap

![](https://image-control-storage.s3.amazonaws.com/2023/04/06111827/image-42-1024x473.png)

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 this property.

.container {
  flex-wrap: nowrap | wrap | wrap-reverse;
}

There are some visual demos of flex-wrap here.

flex-flow

This is a shorthand for the flex-direction and flex-wrap properties, which together define the flex container’s main and cross axes. The default value is row nowrap.

.container {
  flex-flow: column wrap;
}

justify-content

![Flex-flow](https://image-control-storage.s3.amazonaws.com/2023/04/06111701/image-41-677x1024.png)

This defines the alignment along the main axis. It helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.

.container {
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly | start | end | left | right ... + safe | unsafe;
}

Note that that browser support for these values is nuanced. For example, space-between never got support from some versions of Edge, and start/end/left/right aren’t in Chrome yet. MDN has detailed charts. The safest values are flex-start, flex-end, and center.

There are also two additional keywords you can pair with these values: safe and unsafe. Using safe ensures that however you do this type of positioning, you can’t push an element such that it renders off-screen (e.g. off the top) in such a way the content can’t be scrolled too (called “data loss”).

align-items

![](https://image-control-storage.s3.amazonaws.com/2023/04/06112037/image-46-852x1024.png)

This defines the default behavior for how flex items are laid out along the cross axis on the current line. Think of it as the justify-content version for the cross-axis (perpendicular to the main-axis).

.container {
  align-items: stretch | flex-start | flex-end | center | baseline | first baseline | last baseline | start | end | self-start | self-end + ... safe | unsafe;
}

The safe and unsafe modifier keywords can be used in conjunction with all the rest of these keywords (although note browser support), and deal with helping you prevent aligning elements such that the content becomes inaccessible.

align-content

![](https://image-control-storage.s3.amazonaws.com/2023/04/06112103/image-47-830x1024.png)

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 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-wrap` is set to its default value, `no-wrap`) will not reflect `align-content`.
.container {
  align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | start | end | baseline | first baseline | last baseline + ... safe | unsafe;
}

The safe and unsafe modifier keywords can be used in conjunction with all the rest of these keywords (although note browser support), and deal with helping you prevent aligning elements such that the content becomes inaccessible.

gap, row-gap, column-gap

![](https://image-control-storage.s3.amazonaws.com/2023/04/06112139/image-48-922x1024.png)

The gap property explicitly controls the space between flex items. It applies that spacing only between items not on the outer edges.

.container {
  display: flex;
  ...
  gap: 10px;
  gap: 10px 20px; <em>/* row-gap column gap */</em>
  row-gap: 10px;
  column-gap: 20px;
}

The behavior could be thought of as a minimum gutter, as if the gutter is bigger somehow (because of something like justify-content: space-between;) then the gap will only take effect if that space would end up smaller.

It is not exclusively for flexbox, gap works in grid and multi-column layout as well.

Prefixing Flexbox

Flexbox requires some vendor prefixing to support the most browsers possible. It doesn’t just include prepending properties with the vendor prefix, but there are actually entirely different property and value names. This is because the Flexbox spec has changed over time, creating an “old”, “tweener”, and “new” versions.

Perhaps the best way to handle this is to write in the new (and final) syntax and run your CSS through Autoprefixer, which handles the fallbacks very well.

Alternatively, here’s a Sass @mixin to help with some of the prefixing, which also gives you an idea of what kind of things need to be done:

@mixin flexbox() {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
}

@mixin flex($values) {
  -webkit-box-flex: $values;
  -moz-box-flex:  $values;
  -webkit-flex:  $values;
  -ms-flex:  $values;
  flex:  $values;
}

@mixin order($val) {
  -webkit-box-ordinal-group: $val;  
  -moz-box-ordinal-group: $val;     
  -ms-flex-order: $val;     
  -webkit-order: $val;  
  order: $val;
}

.wrapper {
  @include flexbox();
}

.item {
  @include flex(1 200px);
  @include order(2);
}

Examples

Let’s start with a very very simple example, solving an almost daily problem: perfect centering. It couldn’t be any simpler if you use flexbox.

.parent {
  display: flex;
  height: 300px; <em>/* Or whatever */</em>
}

.child {
  width: 100px;  <em>/* Or whatever */</em>
  height: 100px; <em>/* Or whatever */</em>
  margin: auto;  <em>/* Magic! */</em>
}

This relies on the fact a margin set to auto in a flex container absorb extra space. So setting a margin of auto will make the item perfectly centered in both axes.

Now let’s use some more properties. Consider a list of 6 items, all with fixed dimensions, but can be auto-sized. We want them to be evenly distributed on the horizontal axis so that when we resize the browser, everything scales nicely, and without media queries.

.flex-container {
  <em>/* We first create a flex layout context */</em>
  display: flex;

  <em>/* Then we define the flow direction 
     and if we allow the items to wrap 
   * Remember this is the same as:
   * flex-direction: row;
   * flex-wrap: wrap;
   */</em>
  flex-flow: row wrap;

  <em>/* Then we define how is distributed the remaining space */</em>
  justify-content: space-around;
}

Done. Everything else is just some styling concern. Below is a pen featuring this example. Be sure to go to CodePen and try resizing your windows to see what happens.

See the Pen Demo Flexbox 1 by CSS-Tricks (@css-tricks) on CodePen.

Let’s try something else. Imagine we have a right-aligned navigation element on the very top of our website, but we want it to be centered on medium-sized screens and single-columned on small devices. Easy enough.

<em>/* Large */</em>
.navigation {
  display: flex;
  flex-flow: row wrap;
  <em>/* This aligns items to the end line on main-axis */</em>
  justify-content: flex-end;
}

<em>/* Medium screens */</em>
@media all and (max-width: 800px) {
  .navigation {
    <em>/* When on medium sized screens, we center it by evenly distributing empty space around items */</em>
    justify-content: space-around;
  }
}

<em>/* Small screens */</em>
@media all and (max-width: 500px) {
  .navigation {
    <em>/* On small screens, we are no longer using row direction but column */</em>
    flex-direction: column;
  }
}

See the Pen Demo Flexbox 2 by CSS-Tricks (@css-tricks) on CodePen.

Let’s try something even better by playing with flex items flexibility! What about a mobile-first 3-columns layout with full-width header and footer. And independent from source order.

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

<em>/* We tell all items to be 100% width, via flex-basis */</em>
.wrapper > * {
  flex: 1 100%;
}

<em>/* We rely on source order for mobile-first approach
 * in this case:
 * 1. header
 * 2. article
 * 3. aside 1
 * 4. aside 2
 * 5. footer
 */</em>

<em>/* Medium screens */</em>
@media all and (min-width: 600px) {
  <em>/* We tell both sidebars to share a row */</em>
  .aside { flex: 1 auto; }
}

<em>/* Large screens */</em>
@media all and (min-width: 800px) {
  <em>/* We invert order of first sidebar and main
   * And tell the main element to take twice as much width as the other two sidebars 
   */</em>
  .main { flex: 3 0px; }
  .aside-1 { order: 1; }
  .main    { order: 2; }
  .aside-2 { order: 3; }
  .footer  { order: 4; }
}

See the Pen Demo Flexbox 3 by Chris Coyier (@chriscoyier) on CodePen.

See the Pen FlexBox Basics by Nicholas D’Angelo (@ndangelo) on CodePen.




Code


Art


Design


UI/UX


Video


Projects


Social