flexbox-grid.md 5.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
---
layout: docs
title: Flexbox grid system
group: layout
---

Fancy a more modern grid system? [Enable flexbox support in Bootstrap](/getting-started/flexbox) to take full advantage of CSS's Flexible Box module for even more control over your site's layout, alignment, and distribution of content.

Bootstrap's flexbox grid includes support for every feature from our [default grid system](/layout/grid), and then some. Please read the [default grid system docs](/layout/grid) before proceeding through this page. Features that are covered there are only summarized here. Please note that **Internet Explorer 9 does not support flexbox**, so proceed with caution when enabling it.

{% callout warning %}
**Heads up!** The flexbox grid documentation is only functional when flexbox support is explicitly enabled.
{% endcallout %}

## Contents

* Will be replaced with the ToC, excluding the "Contents" header
{:toc}

## How it works

The flexbox grid system behaves similar to our default grid system, but with a few notable differences:

- [Grid mixins](/layout/grid#sass-mixins) and [predefined classes](/layout/grid#predefined-classes) include support for flexbox. Just [enable flexbox support](/getting-started/flexbox) to utilize them as you would otherwise.
- Nesting, offsets, pushes, and pulls are all supported in the flexbox grid system.
Mark Otto's avatar
Mark Otto committed
26
- Flexbox grid columns without a set width will automatically layout with equal widths. For example, four columns will each automatically be 25% wide.
27
- Flexbox grid columns have significantly more alignment options available, including vertical alignment.
Mark Otto's avatar
Mark Otto committed
28
- Unlike the default grid system where a grid column starts as full-width in the `xs` tier, flexbox requires a `.col-{breakpoint}` class for each tier.
29
30
31
32
33

Chill? Awesome—keep reading for more information and some code snippets.

## Auto-layout columns

Mark Otto's avatar
Mark Otto committed
34
When flexbox support is enabled, you can utilize breakpoint-specific column classes for equal-width columns. Add any number of `.col-{breakpoint}`s for each breakpoint you need and you're good to go. For example, here's are two grid layouts that apply to every device and viewport possible.
35
36
37
38
39

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row">
40
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
41
      1 of 2
42
43
    </div>
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
44
      1 of 2
45
46
47
48
    </div>
  </div>
  <div class="row">
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
49
      1 of 3
50
    </div>
51
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
52
      1 of 3
53
    </div>
54
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
55
      1 of 3
56
57
58
59
60
61
62
63
64
65
66
67
    </div>
  </div>
</div>
{% endexample %}
</div>

Auto-layout for flexbox grid columns also means you can set the width of one column and the others will automatically resize around it. You may use predefined grid classes (as shown below), grid mixins, or inline widths.

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row">
68
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
69
      1 of 3
70
    </div>
71
    <div class="col-xs-6">
Mark Otto's avatar
Mark Otto committed
72
      2 of 3 (wider)
73
    </div>
74
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
      3 of 3
    </div>
  </div>
</div>
{% endexample %}
</div>

## Responsive flexbox

Unlike the default grid system, the flexbox grid requires a class for full-width columns. If you have a `.col-sm-6` and don't add `.col-xs-12`, your `xs` grid will not render correctly. Note that flexbox grid tiers still scale up across breakpoints, so if you want two 50% wide columns across `sm`, `md`, and `lg`, you only need to set `.col-sm-6`.

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-6">
      1 of 2 (stacked on mobile)
    </div>
    <div class="col-xs-12 col-sm-6">
      1 of 2 (stacked on mobile)
95
96
97
98
99
    </div>
  </div>
</div>
{% endexample %}
</div>
100
101
102
103
104
105
106
107
108

## Vertical alignment

Use the flexbox alignment utilities to vertically align columns.

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row flex-items-xs-top">
109
    <div class="col-xs">
110
111
      One of three columns
    </div>
112
    <div class="col-xs">
113
114
      One of three columns
    </div>
115
    <div class="col-xs">
116
117
118
      One of three columns
    </div>
  </div>
119
  <div class="row flex-items-xs-middle">
120
    <div class="col-xs">
121
122
      One of three columns
    </div>
123
    <div class="col-xs">
124
125
      One of three columns
    </div>
126
    <div class="col-xs">
127
128
129
130
      One of three columns
    </div>
  </div>
  <div class="row flex-items-xs-bottom">
131
    <div class="col-xs">
132
133
      One of three columns
    </div>
134
    <div class="col-xs">
135
136
      One of three columns
    </div>
137
    <div class="col-xs">
138
139
140
141
142
143
144
145
146
147
148
      One of three columns
    </div>
  </div>
</div>
{% endexample %}
</div>

<div class="bd-example-row bd-example-row-flex-cols">
{% example html %}
<div class="container">
  <div class="row">
149
    <div class="col-xs flex-xs-top">
150
151
      One of three columns
    </div>
152
    <div class="col-xs flex-xs-middle">
153
154
      One of three columns
    </div>
155
    <div class="col-xs flex-xs-bottom">
156
157
158
159
160
161
      One of three columns
    </div>
  </div>
</div>
{% endexample %}
</div>
162
163
164
165
166
167
168
169
170

## Horizontal alignment

Flexbox utilities for horizontal alignment also exist for a number of layout options.

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row flex-items-xs-left">
171
    <div class="col-xs-4">
172
173
      One of two columns
    </div>
174
    <div class="col-xs-4">
175
176
177
178
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-center">
179
    <div class="col-xs-4">
180
181
      One of two columns
    </div>
182
    <div class="col-xs-4">
183
184
185
186
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-right">
187
    <div class="col-xs-4">
188
189
      One of two columns
    </div>
190
    <div class="col-xs-4">
191
192
193
194
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-around">
195
    <div class="col-xs-4">
196
197
      One of two columns
    </div>
198
    <div class="col-xs-4">
199
200
201
202
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-between">
203
    <div class="col-xs-4">
204
205
      One of two columns
    </div>
206
    <div class="col-xs-4">
207
208
209
210
211
212
      One of two columns
    </div>
  </div>
</div>
{% endexample %}
</div>