flexbox-grid.md 6.86 KB
Newer Older
1
2
3
---
layout: docs
title: Flexbox grid system
Mark Otto's avatar
Mark Otto committed
4
description: Documentation and examples for using Bootstrap's optional flexbox grid system.
5
6
7
8
9
10
11
12
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 %}
13
**Heads up!** This flexbox grid documentation is powered by an additional CSS file that overrides our default grid system's CSS. This is only available in our hosted docs and is disabled in development.
14
15
16
17
18
19
20
21
22
23
24
25
26
{% 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
27
- Flexbox grid columns without a set width will automatically layout with equal widths. For example, four columns will each automatically be 25% wide.
28
- Flexbox grid columns have significantly more alignment options available, including vertical alignment.
Mark Otto's avatar
Mark Otto committed
29
- 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.
30
31
32
33
34

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

## Auto-layout columns

Mark Otto's avatar
Mark Otto committed
35
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.
36
37
38
39
40

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

63
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. Note that the other columns will resize no matter the width of the center column.
64
65
66
67
68

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row">
69
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
70
      1 of 3
71
    </div>
72
    <div class="col-xs-6">
Mark Otto's avatar
Mark Otto committed
73
      2 of 3 (wider)
74
    </div>
75
    <div class="col-xs">
Mark Otto's avatar
Mark Otto committed
76
77
78
      3 of 3
    </div>
  </div>
79
80
81
82
83
84
85
86
87
88
89
  <div class="row">
    <div class="col-xs">
      1 of 3
    </div>
    <div class="col-xs-5">
      2 of 3 (wider)
    </div>
    <div class="col-xs">
      3 of 3
    </div>
  </div>
Mark Otto's avatar
Mark Otto committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
</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)
107
108
109
110
111
    </div>
  </div>
</div>
{% endexample %}
</div>
112
113
114
115
116
117
118
119
120

## 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">
121
    <div class="col-xs">
122
123
      One of three columns
    </div>
124
    <div class="col-xs">
125
126
      One of three columns
    </div>
127
    <div class="col-xs">
128
129
130
      One of three columns
    </div>
  </div>
131
  <div class="row flex-items-xs-middle">
132
    <div class="col-xs">
133
134
      One of three columns
    </div>
135
    <div class="col-xs">
136
137
      One of three columns
    </div>
138
    <div class="col-xs">
139
140
141
142
      One of three columns
    </div>
  </div>
  <div class="row flex-items-xs-bottom">
143
    <div class="col-xs">
144
145
      One of three columns
    </div>
146
    <div class="col-xs">
147
148
      One of three columns
    </div>
149
    <div class="col-xs">
150
151
152
153
154
155
156
157
158
159
160
      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">
161
    <div class="col-xs flex-xs-top">
162
163
      One of three columns
    </div>
164
    <div class="col-xs flex-xs-middle">
165
166
      One of three columns
    </div>
167
    <div class="col-xs flex-xs-bottom">
168
169
170
171
172
173
      One of three columns
    </div>
  </div>
</div>
{% endexample %}
</div>
174
175
176
177
178
179
180
181
182

## 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">
183
    <div class="col-xs-4">
184
185
      One of two columns
    </div>
186
    <div class="col-xs-4">
187
188
189
190
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-center">
191
    <div class="col-xs-4">
192
193
      One of two columns
    </div>
194
    <div class="col-xs-4">
195
196
197
198
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-right">
199
    <div class="col-xs-4">
200
201
      One of two columns
    </div>
202
    <div class="col-xs-4">
203
204
205
206
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-around">
207
    <div class="col-xs-4">
208
209
      One of two columns
    </div>
210
    <div class="col-xs-4">
211
212
213
214
      One of two columns
    </div>
  </div>
  <div class="row flex-items-xs-between">
215
    <div class="col-xs-4">
216
217
      One of two columns
    </div>
218
    <div class="col-xs-4">
219
220
221
222
223
224
      One of two columns
    </div>
  </div>
</div>
{% endexample %}
</div>
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

## Reordering

Flexbox utilities for controlling the **visual order** of your content.

<div class="bd-example-row">
{% example html %}
<div class="container">
  <div class="row">
    <div class="col-xs flex-xs-unordered">
      First, but unordered
    </div>
    <div class="col-xs flex-xs-last">
      Second, but last
    </div>
    <div class="col-xs flex-xs-first">
      Third, but first
    </div>
  </div>
</div>
{% endexample %}
</div>