Flexbox

Use flexbox to layout.

Standalone Library

You can download the plugin as a standalone library in this page.

Or simply insert this into your <head> element:

<link rel="stylesheet" href="https://unpkg.com/mobi-plugin-flexbox/dist/mobi-plugin-flexbox.min.css" />

Container and units

First of all, let's create a .flex-left container which contains several .units. In this case, .flex-left's width is divided equally between .units.

unit
unit
unit
unit
unit
unit
unit
unit
<div class="flex-left">
  <div class="unit">unit</div>
  <div class="unit">unit</div>
  <div class="unit">unit</div>
</div>
<div class="flex-left">
  <div class="unit">unit</div>
  <div class="unit">unit</div>
  <div class="unit">unit</div>
  <div class="unit">unit</div>
  <div class="unit">unit</div>
</div>

Secondly, let's see how to achieve a maximum 4# grid.

.unit-1
.unit-1-2
.unit-1-2
.unit-1-3
.unit-2-3
.unit-1-4
.unit-3-4
.unit-1-2
.unit-1-4
<div class="flex-left">
  <div class="unit-1">.unit-1</div>
</div>
<div class="flex-left">
  <div class="unit-1-2">.unit-1-2</div>
  <div class="unit-1-2">.unit-1-2</div>
</div>
<div class="flex-left">
  <div class="unit-1-3">.unit-1-3</div>
  <div class="unit-2-3">.unit-2-3</div>
</div>
<div class="flex-left">
  <div class="unit-1-4">.unit-1-4</div>
  <div class="unit-3-4">.unit-3-4</div>
</div>
<div class="flex-left">
  <div class="unit-1-2">.unit-1-2</div>
  <div class="unit-1-4">.unit-1-4</div>
</div>

Finally, we can use .unit-0 as a custom size unit. Set a width to it, or let the content decide the width.

.unit
.unit-0 which has 200px width
.unit
.unit-0 which has no width set
.unit
.unit-1-4
<div class="flex-left">
  <div class="unit">.unit</div>
  <div class="unit-0" style="width:200px">.unit-0 which has 200px width</div>
  <div class="unit">.unit</div>
</div>
<div class="flex-left">
  <div class="unit-0">.unit-0 which has no width set</div>
  <div class="unit">.unit</div>
  <div class="unit-1-4">.unit-1-4</div>
</div>

Flexbox aligning

Forget margin:0 auto. We can align easily with flexbox!

.flex-left > div
.flex-center > div
.flex-right > div
.flex-top > div
.flex-middle > div
.flex-bottom > div
.flex-center.flex-middle > div
<div class="flex-left">
  <div>.flex-left > div</div>
</div>
<div class="flex-center">
  <div>.flex-center > div</div>
</div>
<div class="flex-right">
  <div>.flex-right > div</div>
</div>
<div class="flex-top" style="height:60px;">
  <div>.flex-top > div</div>
</div>
<div class="flex-middle" style="height:60px;">
  <div>.flex-middle > div</div>
</div>
<div class="flex-bottom" style="height:60px;">
  <div>.flex-bottom > div</div>
</div>
<div class="flex-center flex-middle" style="height:60px;">
  <div>.flex-center.flex-middle > div</div>
</div>

Vertical flexbox

We can use .flex-vertical to easily achieve vertical flexbox layout.

.unit-0
.unit
.unit-1-4
<div class="flex-vertical" style="height:200px;">
  <div class="unit-0">.unit-0</div>
  <div class="unit">.unit</div>
  <div class="unit-1-4">.unit-1-4</div>
</div>

Wrapable flexbox

By default the flex-wrap of flexbox container is set to nowrap, this is to prevent some unexpected wrap. We can set it to wrap using .flex-wrap:

.unit-0 with a lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot of text
.unit
.unit-1-4
.flex-wrap > .unit-0 with a lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot of text
.unit
.unit-1-4
<div class="flex-left">
  <div class="unit-0">.unit-0 with a lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot of text</div>
  <div class="unit">.unit</div>
  <div class="unit-1-4">.unit-1-4</div>
</div>
<div class="flex-left flex-wrap">
  <div class="unit-0">.flex-wrap > .unit-0 with a lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot lot of text</div>
  <div class="unit">.unit</div>
  <div class="unit-1-4">.unit-1-4</div>
</div>

Unit 1 on mobile

Sometimes we need to show .unit-1-3 on desktop, and .unit-1 on mobile. We can use .unit-1-on-mobile inside .flex-wrap to achieve this:

.flex-wrap > .unit-1-3.unit-1-on-mobile
.flex-wrap > .unit-2-3.unit-1-on-mobile
<div class="flex-left flex-wrap">
  <div class="unit-1-3 unit-1-on-mobile">.flex-wrap > .unit-1-3.unit-1-on-mobile</div>
  <div class="unit-2-3 unit-1-on-mobile">.flex-wrap > .unit-2-3.unit-1-on-mobile</div>
</div>

Units gap

In all of the above examples, the .unit* has no left and right paddings, it may not convenient sometimes. We can add .units-gap and .units-gap-big to the flexbox container to add gaps between .unit*s:

.units-gap > .unit-0
.units-gap > .unit
.units-gap > .unit-1-4
<div class="flex-left units-gap top-gap text-center">
  <div class="unit-0 site-box">.units-gap > .unit-0</div>
  <div class="unit site-box">.units-gap > .unit</div>
  <div class="unit-1-4 site-box">.units-gap > .unit-1-4</div>
</div>
.units-gap-big > .unit-0
.units-gap-big > .unit
.units-gap-big > .unit-1-4
<div class="flex-left units-gap-big top-gap text-center">
  <div class="unit-0 site-box">.units-gap-big > .unit-0</div>
  <div class="unit site-box">.units-gap-big > .unit</div>
  <div class="unit-1-4 site-box">.units-gap-big > .unit-1-4</div>
</div>