grid.md 25.7 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
---
Mark Otto's avatar
Mark Otto committed
2
layout: docs
Mark Otto's avatar
Mark Otto committed
3
title: Grid system
Mark Otto's avatar
Mark Otto committed
4
description: Use our powerful mobile-first flexbox grid to build layouts of all shapes and sizes thanks to a twelve column system, five default responsive tiers, Sass variables and mixins, and dozens of predefined classes.
5
group: layout
Mark Otto's avatar
Mark Otto committed
6
toc: true
Mark Otto's avatar
Mark Otto committed
7
---
Mark Otto's avatar
Mark Otto committed
8

Mark Otto's avatar
Mark Otto committed
9
## How it works
Mark Otto's avatar
Mark Otto committed
10

11
Bootstrap's grid system uses a series of containers, rows, and columns to layout and align content. It's built with [flexbox](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox) and is fully responsive. Below is an example and an in-depth look at how the grid comes together.
Mark Otto's avatar
Mark Otto committed
12

13
14
15
**New to or unfamiliar with flexbox?** [Read this CSS Tricks flexbox guide](https://css-tricks.com/snippets/css/a-guide-to-flexbox/#flexbox-background) for background, terminology, guidelines, and code snippets.

<div class="bd-example-row">
m5o's avatar
m5o committed
16
{% capture example %}
Mark Otto's avatar
Mark Otto committed
17
<div class="container">
Mark Otto's avatar
Mark Otto committed
18
  <div class="row">
19
    <div class="col-sm">
Mark Otto's avatar
Mark Otto committed
20
      One of three columns
Mark Otto's avatar
Mark Otto committed
21
    </div>
22
    <div class="col-sm">
Mark Otto's avatar
Mark Otto committed
23
      One of three columns
Mark Otto's avatar
Mark Otto committed
24
    </div>
25
    <div class="col-sm">
Mark Otto's avatar
Mark Otto committed
26
27
28
29
      One of three columns
    </div>
  </div>
</div>
m5o's avatar
m5o committed
30
31
{% endcapture %}
{% include example.html content=example %}
32
</div>
Mark Otto's avatar
Mark Otto committed
33

Bardi Harborow's avatar
Bardi Harborow committed
34
The above example creates three equal-width columns on small, medium, large, and extra large devices using our predefined grid classes. Those columns are centered in the page with the parent `.container`.
Mark Otto's avatar
Mark Otto committed
35

36
37
Breaking it down, here's how it works:

Mark Otto's avatar
Mark Otto committed
38
- Containers provide a means to center and horizontally pad your site's contents. Use `.container` for a responsive pixel width or `.container-fluid` for `width: 100%` across all viewport and device sizes.
39
40
- Rows are wrappers for columns. Each column has horizontal `padding` (called a gutter) for controlling the space between them. This `padding` is then counteracted on the rows with negative margins. This way, all the content in your columns is visually aligned down the left side.
- In a grid layout, content must be placed within columns and only columns may be immediate children of rows.
41
42
- Thanks to flexbox, grid columns without a specified `width` will automatically layout as equal width columns. For example, four instances of `.col-sm` will each automatically be 25% wide from the small breakpoint and up. See the [auto-layout columns](#auto-layout-columns) section for more examples.
- Column classes indicate the number of columns you'd like to use out of the possible 12 per row. So, if you want three equal-width columns across, you can use `.col-4`.
43
44
- Column `width`s are set in percentages, so they're always fluid and sized relative to their parent element.
- Columns have horizontal `padding` to create the gutters between individual columns, however, you can remove the `margin` from rows and `padding` from columns with `.no-gutters` on the `.row`.
45
46
47
- To make the grid responsive, there are five grid breakpoints, one for each [responsive breakpoint]({{ site.baseurl }}/docs/{{ site.docs_version }}/layout/overview/#responsive-breakpoints): all breakpoints (extra small), small, medium, large, and extra large.
- Grid breakpoints are based on minimum width media queries, meaning **they apply to that one breakpoint and all those above it** (e.g., `.col-sm-4` applies to small, medium, large, and extra large devices, but not the first `xs` breakpoint).
- You can use predefined grid classes (like `.col-4`) or [Sass mixins](#sass-mixins) for more semantic markup.
48

49
Be aware of the limitations and [bugs around flexbox](https://github.com/philipwalton/flexbugs), like the [inability to use some HTML elements as flex containers](https://github.com/philipwalton/flexbugs#flexbug-9).
50

Mark Otto's avatar
Mark Otto committed
51
## Grid options
Mark Otto's avatar
Mark Otto committed
52

Mark Otto's avatar
Mark Otto committed
53
While Bootstrap uses `em`s or `rem`s for defining most sizes, `px`s are used for grid breakpoints and container widths. This is because the viewport width is in pixels and does not change with the [font size](https://drafts.csswg.org/mediaqueries-3/#units).
54

Mark Otto's avatar
Mark Otto committed
55
56
See how aspects of the Bootstrap grid system work across multiple devices with a handy table.

Mark Otto's avatar
Mark Otto committed
57
<table class="table table-bordered table-striped">
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  <thead>
    <tr>
      <th></th>
      <th class="text-center">
        Extra small<br>
        <small>&lt;576px</small>
      </th>
      <th class="text-center">
        Small<br>
        <small>&ge;576px</small>
      </th>
      <th class="text-center">
        Medium<br>
        <small>&ge;768px</small>
      </th>
      <th class="text-center">
        Large<br>
        <small>&ge;992px</small>
      </th>
      <th class="text-center">
        Extra large<br>
        <small>&ge;1200px</small>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th class="text-nowrap" scope="row">Max container width</th>
      <td>None (auto)</td>
      <td>540px</td>
      <td>720px</td>
      <td>960px</td>
      <td>1140px</td>
    </tr>
    <tr>
      <th class="text-nowrap" scope="row">Class prefix</th>
      <td><code>.col-</code></td>
      <td><code>.col-sm-</code></td>
      <td><code>.col-md-</code></td>
      <td><code>.col-lg-</code></td>
      <td><code>.col-xl-</code></td>
    </tr>
    <tr>
      <th class="text-nowrap" scope="row"># of columns</th>
      <td colspan="5">12</td>
    </tr>
    <tr>
      <th class="text-nowrap" scope="row">Gutter width</th>
      <td colspan="5">30px (15px on each side of a column)</td>
    </tr>
    <tr>
      <th class="text-nowrap" scope="row">Nestable</th>
      <td colspan="5">Yes</td>
    </tr>
    <tr>
      <th class="text-nowrap" scope="row">Column ordering</th>
      <td colspan="5">Yes</td>
    </tr>
  </tbody>
</table>
Mark Otto's avatar
Mark Otto committed
118

119
## Auto-layout columns
120

121
Utilize breakpoint-specific column classes for easy column sizing without an explicit numbered class like `.col-sm-6`.
Kovah's avatar
Kovah committed
122

123
### Equal-width
Mark Otto's avatar
Mark Otto committed
124

125
For example, here are two grid layouts that apply to every device and viewport, from `xs` to `xl`. Add any number of unit-less classes for each breakpoint you need and every column will be the same width.
Mark Otto's avatar
Mark Otto committed
126

127
<div class="bd-example-row">
m5o's avatar
m5o committed
128
{% capture example %}
129
130
131
132
133
134
<div class="container">
  <div class="row">
    <div class="col">
      1 of 2
    </div>
    <div class="col">
135
      2 of 2
136
137
138
139
140
141
142
    </div>
  </div>
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col">
143
      2 of 3
144
145
    </div>
    <div class="col">
146
      3 of 3
147
148
149
    </div>
  </div>
</div>
m5o's avatar
m5o committed
150
151
{% endcapture %}
{% include example.html content=example %}
152
</div>
153

154
Equal-width columns can be broken into multiple lines, but there was a [Safari flexbox bug](https://github.com/philipwalton/flexbugs#flexbug-11) that prevented this from working without an explicit `flex-basis` or `border`. There are workarounds for older browser versions, but they shouldn't be necessary if you're up-to-date.
Mark Otto's avatar
Mark Otto committed
155

156
<div class="bd-example-row">
m5o's avatar
m5o committed
157
{% capture example %}
158
159
160
161
162
163
164
165
166
<div class="container">
  <div class="row">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="w-100"></div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>
m5o's avatar
m5o committed
167
168
{% endcapture %}
{% include example.html content=example %}
169
170
</div>

171
### Setting one column width
Mark Otto's avatar
Mark Otto committed
172

173
Auto-layout for flexbox grid columns also means you can set the width of one column and have the sibling columns 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.
Mark Otto's avatar
Mark Otto committed
174

175
<div class="bd-example-row">
m5o's avatar
m5o committed
176
{% capture example %}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<div class="container">
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col-6">
      2 of 3 (wider)
    </div>
    <div class="col">
      3 of 3
    </div>
  </div>
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col-5">
      2 of 3 (wider)
    </div>
    <div class="col">
      3 of 3
    </div>
  </div>
</div>
m5o's avatar
m5o committed
201
202
{% endcapture %}
{% include example.html content=example %}
203
</div>
Mark Otto's avatar
Mark Otto committed
204

205
### Variable width content
Mark Otto's avatar
Mark Otto committed
206

Mark Otto's avatar
Mark Otto committed
207
Use `col-{breakpoint}-auto` classes to size columns based on the natural width of their content.
Mark Otto's avatar
Mark Otto committed
208

209
<div class="bd-example-row">
m5o's avatar
m5o committed
210
{% capture example %}
Mark Otto's avatar
Mark Otto committed
211
<div class="container">
212
  <div class="row justify-content-md-center">
213
214
215
    <div class="col col-lg-2">
      1 of 3
    </div>
Mark Otto's avatar
Mark Otto committed
216
    <div class="col-md-auto">
217
218
219
220
221
222
      Variable width content
    </div>
    <div class="col col-lg-2">
      3 of 3
    </div>
  </div>
Mark Otto's avatar
Mark Otto committed
223
  <div class="row">
224
225
226
    <div class="col">
      1 of 3
    </div>
Mark Otto's avatar
Mark Otto committed
227
    <div class="col-md-auto">
228
229
230
231
232
      Variable width content
    </div>
    <div class="col col-lg-2">
      3 of 3
    </div>
Mark Otto's avatar
Mark Otto committed
233
234
  </div>
</div>
m5o's avatar
m5o committed
235
236
{% endcapture %}
{% include example.html content=example %}
237
</div>
Mark Otto's avatar
Mark Otto committed
238

239
240
### Equal-width multi-row

Mark Otto's avatar
Mark Otto committed
241
Create equal-width columns that span multiple rows by inserting a `.w-100` where you want the columns to break to a new line. Make the breaks responsive by mixing the `.w-100` with some [responsive display utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/display/).
242
243

<div class="bd-example-row">
m5o's avatar
m5o committed
244
{% capture example %}
245
246
247
248
249
250
251
<div class="row">
  <div class="col">col</div>
  <div class="col">col</div>
  <div class="w-100"></div>
  <div class="col">col</div>
  <div class="col">col</div>
</div>
m5o's avatar
m5o committed
252
253
{% endcapture %}
{% include example.html content=example %}
254
255
</div>

256
## Responsive classes
257

258
Bootstrap's grid includes five tiers of predefined classes for building complex responsive layouts. Customize the size of your columns on extra small, small, medium, large, or extra large devices however you see fit.
259

260
### All breakpoints
Mark Otto's avatar
Mark Otto committed
261

262
For grids that are the same from the smallest of devices to the largest, use the `.col` and `.col-*` classes. Specify a numbered class when you need a particularly sized column; otherwise, feel free to stick to `.col`.
Mark Otto's avatar
Mark Otto committed
263

Mark Otto's avatar
Mark Otto committed
264
<div class="bd-example-row">
m5o's avatar
m5o committed
265
{% capture example %}
Mark Otto's avatar
Mark Otto committed
266
<div class="row">
267
268
269
270
  <div class="col">col</div>
  <div class="col">col</div>
  <div class="col">col</div>
  <div class="col">col</div>
Mark Otto's avatar
Mark Otto committed
271
272
</div>
<div class="row">
273
274
275
  <div class="col-8">col-8</div>
  <div class="col-4">col-4</div>
</div>
m5o's avatar
m5o committed
276
277
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
278
</div>
279
280
281

### Stacked to horizontal

282
Using a single set of `.col-sm-*` classes, you can create a basic grid system that starts out stacked and becomes horizontal at the small breakpoint (`sm`).
283
284

<div class="bd-example-row">
m5o's avatar
m5o committed
285
{% capture example %}
Mark Otto's avatar
Mark Otto committed
286
<div class="row">
287
288
  <div class="col-sm-8">col-sm-8</div>
  <div class="col-sm-4">col-sm-4</div>
Mark Otto's avatar
Mark Otto committed
289
290
</div>
<div class="row">
291
292
293
  <div class="col-sm">col-sm</div>
  <div class="col-sm">col-sm</div>
  <div class="col-sm">col-sm</div>
Mark Otto's avatar
Mark Otto committed
294
</div>
m5o's avatar
m5o committed
295
296
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
297
</div>
Mark Otto's avatar
Mark Otto committed
298

299
### Mix and match
Mark Otto's avatar
Mark Otto committed
300

301
Don't want your columns to simply stack in some grid tiers? Use a combination of different classes for each tier as needed. See the example below for a better idea of how it all works.
Mark Otto's avatar
Mark Otto committed
302

Mark Otto's avatar
Mark Otto committed
303
<div class="bd-example-row">
m5o's avatar
m5o committed
304
{% capture example %}
Mark Otto's avatar
Mark Otto committed
305
306
<!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="row">
307
  <div class="col-12 col-md-8">.col-12 .col-md-8</div>
308
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
Mark Otto's avatar
Mark Otto committed
309
310
311
312
</div>

<!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
<div class="row">
313
314
315
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
Mark Otto's avatar
Mark Otto committed
316
317
318
319
</div>

<!-- Columns are always 50% wide, on mobile and desktop -->
<div class="row">
320
321
  <div class="col-6">.col-6</div>
  <div class="col-6">.col-6</div>
Mark Otto's avatar
Mark Otto committed
322
</div>
m5o's avatar
m5o committed
323
324
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
325
</div>
Mark Otto's avatar
Mark Otto committed
326

Mark Otto's avatar
Mark Otto committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
### Gutters

Gutters can be responsively adjusted by breakpoint-specific padding and negative margin utility classes. To change the gutters in a given row, pair a negative margin utility on the `.row` and matching padding utilities on the `.col`s.

Here's an example of customizing the Bootstrap grid at the large (`lg`) breakpoint and above. We've increased the `.col` padding with `.px-lg-5` and then counteracted that with `.mx-lg-n5` on the parent `.row`.

{% capture example %}
<div class="row mx-lg-n5">
  <div class="col py-3 px-lg-5 border bg-light">Custom column padding</div>
  <div class="col py-3 px-lg-5 border bg-light">Custom column padding</div>
</div>
{% endcapture %}
{% include example.html content=example %}

341
342
343
## Alignment

Use flexbox alignment utilities to vertically and horizontally align columns.
Mark Otto's avatar
Mark Otto committed
344

345
### Vertical alignment
Mark Otto's avatar
Mark Otto committed
346

347
<div class="bd-example-row bd-example-row-flex-cols">
m5o's avatar
m5o committed
348
{% capture example %}
349
<div class="container">
350
  <div class="row align-items-start">
351
352
353
354
355
356
357
358
359
360
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
361
  <div class="row align-items-center">
362
363
364
365
366
367
368
369
370
371
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
372
  <div class="row align-items-end">
373
374
375
376
377
378
379
380
381
382
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
Mark Otto's avatar
Mark Otto committed
383
</div>
m5o's avatar
m5o committed
384
385
{% endcapture %}
{% include example.html content=example %}
386
387
388
</div>

<div class="bd-example-row bd-example-row-flex-cols">
m5o's avatar
m5o committed
389
{% capture example %}
390
391
<div class="container">
  <div class="row">
392
    <div class="col align-self-start">
393
394
      One of three columns
    </div>
395
    <div class="col align-self-center">
396
397
      One of three columns
    </div>
398
    <div class="col align-self-end">
399
400
401
402
      One of three columns
    </div>
  </div>
</div>
m5o's avatar
m5o committed
403
404
{% endcapture %}
{% include example.html content=example %}
405
406
407
408
409
</div>

### Horizontal alignment

<div class="bd-example-row">
m5o's avatar
m5o committed
410
{% capture example %}
411
<div class="container">
412
  <div class="row justify-content-start">
413
414
415
416
417
418
419
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
420
  <div class="row justify-content-center">
421
422
423
424
425
426
427
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
428
  <div class="row justify-content-end">
429
430
431
432
433
434
435
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
436
  <div class="row justify-content-around">
437
438
439
440
441
442
443
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
444
  <div class="row justify-content-between">
445
446
447
448
449
450
451
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
Mark Otto's avatar
Mark Otto committed
452
</div>
m5o's avatar
m5o committed
453
454
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
455
</div>
Mark Otto's avatar
Mark Otto committed
456

457
### No gutters
458

459
The gutters between columns in our predefined grid classes can be removed with `.no-gutters`. This removes the negative `margin`s from `.row` and the horizontal `padding` from all immediate children columns.
460

Mark Otto's avatar
Mark Otto committed
461
Here's the source code for creating these styles. Note that column overrides are scoped to only the first children columns and are targeted via [attribute selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors). While this generates a more specific selector, column padding can still be further customized with [spacing utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/spacing/).
462

463
464
**Need an edge-to-edge design?** Drop the parent `.container` or `.container-fluid`.

465
466
467
468
469
{% highlight sass %}
.no-gutters {
  margin-right: 0;
  margin-left: 0;

470
  > .col,
471
472
473
474
475
476
477
478
479
480
  > [class*="col-"] {
    padding-right: 0;
    padding-left: 0;
  }
}
{% endhighlight %}

In practice, here's how it looks. Note you can continue to use this with all other predefined grid classes (including column widths, responsive tiers, reorders, and more).

<div class="bd-example-row">
m5o's avatar
m5o committed
481
{% capture example %}
482
483
484
485
<div class="row no-gutters">
  <div class="col-12 col-sm-6 col-md-8">.col-12 .col-sm-6 .col-md-8</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
m5o's avatar
m5o committed
486
487
{% endcapture %}
{% include example.html content=example %}
488
489
</div>

490
### Column wrapping
Mark Otto's avatar
Mark Otto committed
491
492
493

If more than 12 columns are placed within a single row, each group of extra columns will, as one unit, wrap onto a new line.

Mark Otto's avatar
Mark Otto committed
494
<div class="bd-example-row">
m5o's avatar
m5o committed
495
{% capture example %}
Mark Otto's avatar
Mark Otto committed
496
<div class="row">
497
498
499
  <div class="col-9">.col-9</div>
  <div class="col-4">.col-4<br>Since 9 + 4 = 13 &gt; 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
  <div class="col-6">.col-6<br>Subsequent columns continue along the new line.</div>
Mark Otto's avatar
Mark Otto committed
500
</div>
m5o's avatar
m5o committed
501
502
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
503
</div>
Mark Otto's avatar
Mark Otto committed
504

505
### Column breaks
Mark Otto's avatar
Mark Otto committed
506

Gijs Boddeus's avatar
Gijs Boddeus committed
507
Breaking columns to a new line in flexbox requires a small hack: add an element with `width: 100%` wherever you want to wrap your columns to a new line. Normally this is accomplished with multiple `.row`s, but not every implementation method can account for this.
Mark Otto's avatar
Mark Otto committed
508

Mark Otto's avatar
Mark Otto committed
509
<div class="bd-example-row">
m5o's avatar
m5o committed
510
{% capture example %}
Mark Otto's avatar
Mark Otto committed
511
<div class="row">
512
513
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
Mark Otto's avatar
Mark Otto committed
514

515
516
  <!-- Force next columns to break to new line -->
  <div class="w-100"></div>
Mark Otto's avatar
Mark Otto committed
517

518
519
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
  <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
Mark Otto's avatar
Mark Otto committed
520
</div>
m5o's avatar
m5o committed
521
522
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
523
</div>
Mark Otto's avatar
Mark Otto committed
524

525
You may also apply this break at specific breakpoints with our [responsive display utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/display/).
Mark Otto's avatar
Mark Otto committed
526

527
<div class="bd-example-row">
m5o's avatar
m5o committed
528
{% capture example %}
529
<div class="row">
530
531
  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
Mark Otto's avatar
Mark Otto committed
532

533
534
535
536
537
  <!-- Force next columns to break to new line at md breakpoint and up -->
  <div class="w-100 d-none d-md-block"></div>

  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
  <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
538
</div>
m5o's avatar
m5o committed
539
540
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
541
542
</div>

543
544
## Reordering

545
### Order classes
546

547
Use `.order-` classes for controlling the **visual order** of your content. These classes are responsive, so you can set the `order` by breakpoint (e.g., `.order-1.order-md-2`). Includes support for `1` through `12` across all five grid tiers.
548
549

<div class="bd-example-row">
m5o's avatar
m5o committed
550
{% capture example %}
551
552
<div class="container">
  <div class="row">
553
    <div class="col">
554
555
      First, but unordered
    </div>
556
    <div class="col order-12">
557
558
      Second, but last
    </div>
559
    <div class="col order-1">
560
561
562
563
      Third, but first
    </div>
  </div>
</div>
m5o's avatar
m5o committed
564
565
{% endcapture %}
{% include example.html content=example %}
566
567
</div>

568
There are also responsive `.order-first` and `.order-last` classes that change the `order` of an element by applying `order: -1` and `order: 13` (`order: $columns + 1`), respectively. These classes can also be intermixed with the numbered `.order-*` classes as needed.
569
570

<div class="bd-example-row">
m5o's avatar
m5o committed
571
{% capture example %}
572
573
<div class="container">
  <div class="row">
574
575
    <div class="col order-last">
      First, but last
576
577
578
579
580
581
582
583
584
    </div>
    <div class="col">
      Second, but unordered
    </div>
    <div class="col order-first">
      Third, but first
    </div>
  </div>
</div>
m5o's avatar
m5o committed
585
586
{% endcapture %}
{% include example.html content=example %}
587
588
</div>

589
### Offsetting columns
Mark Otto's avatar
Mark Otto committed
590

Mark Otto's avatar
Mark Otto committed
591
592
593
594
595
596
597
You can offset grid columns in two ways: our responsive `.offset-` grid classes and our [margin utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/spacing/). Grid classes are sized to match columns while margins are more useful for quick layouts where the width of the offset is variable.

#### Offset classes

Move columns to the right using `.offset-md-*` classes. These classes increase the left margin of a column by `*` columns. For example, `.offset-md-4` moves `.col-md-4` over four columns.

<div class="bd-example-row">
m5o's avatar
m5o committed
598
{% capture example %}
Mark Otto's avatar
Mark Otto committed
599
600
601
602
603
604
605
606
607
608
609
<div class="row">
  <div class="col-md-4">.col-md-4</div>
  <div class="col-md-4 offset-md-4">.col-md-4 .offset-md-4</div>
</div>
<div class="row">
  <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
  <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
</div>
<div class="row">
  <div class="col-md-6 offset-md-3">.col-md-6 .offset-md-3</div>
</div>
m5o's avatar
m5o committed
610
611
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
612
613
</div>

614
615
616
In addition to column clearing at responsive breakpoints, you may need to reset offsets. See this in action in [the grid example]({{ site.baseurl }}/docs/{{ site.docs_version }}/examples/grid/).

<div class="bd-example-row">
m5o's avatar
m5o committed
617
{% capture example %}
618
619
620
621
622
623
<div class="row">
  <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
  <div class="col-sm-5 offset-sm-2 col-md-6 offset-md-0">.col-sm-5 .offset-sm-2 .col-md-6 .offset-md-0</div>
</div>

<div class="row">
Rychu's avatar
Rychu committed
624
  <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
625
626
  <div class="col-sm-6 col-md-5 offset-md-2 col-lg-6 offset-lg-0">.col-sm-6 .col-md-5 .offset-md-2 .col-lg-6 .offset-lg-0</div>
</div>
m5o's avatar
m5o committed
627
628
{% endcapture %}
{% include example.html content=example %}
629
630
</div>

Mark Otto's avatar
Mark Otto committed
631
632
633
#### Margin utilities

With the move to flexbox in v4, you can use margin utilities like `.mr-auto` to force sibling columns away from one another.
Mark Otto's avatar
Mark Otto committed
634

Mark Otto's avatar
Mark Otto committed
635
<div class="bd-example-row">
m5o's avatar
m5o committed
636
{% capture example %}
Mark Otto's avatar
Mark Otto committed
637
<div class="row">
638
  <div class="col-md-4">.col-md-4</div>
639
  <div class="col-md-4 ml-auto">.col-md-4 .ml-auto</div>
Mark Otto's avatar
Mark Otto committed
640
641
</div>
<div class="row">
642
643
  <div class="col-md-3 ml-md-auto">.col-md-3 .ml-md-auto</div>
  <div class="col-md-3 ml-md-auto">.col-md-3 .ml-md-auto</div>
Mark Otto's avatar
Mark Otto committed
644
</div>
645
<div class="row">
646
647
  <div class="col-auto mr-auto">.col-auto .mr-auto</div>
  <div class="col-auto">.col-auto</div>
648
</div>
m5o's avatar
m5o committed
649
650
{% endcapture %}
{% include example.html content=example %}
651
652
653
</div>

## Nesting
Mark Otto's avatar
Mark Otto committed
654

Mark Otto's avatar
Mark Otto committed
655
To nest your content with the default grid, add a new `.row` and set of `.col-sm-*` columns within an existing `.col-sm-*` column. Nested rows should include a set of columns that add up to 12 or fewer (it is not required that you use all 12 available columns).
Mark Otto's avatar
Mark Otto committed
656

Mark Otto's avatar
Mark Otto committed
657
<div class="bd-example-row">
m5o's avatar
m5o committed
658
{% capture example %}
Mark Otto's avatar
Mark Otto committed
659
<div class="row">
660
661
  <div class="col-sm-9">
    Level 1: .col-sm-9
Mark Otto's avatar
Mark Otto committed
662
    <div class="row">
663
664
      <div class="col-8 col-sm-6">
        Level 2: .col-8 .col-sm-6
Mark Otto's avatar
Mark Otto committed
665
      </div>
666
667
      <div class="col-4 col-sm-6">
        Level 2: .col-4 .col-sm-6
Mark Otto's avatar
Mark Otto committed
668
669
670
671
      </div>
    </div>
  </div>
</div>
m5o's avatar
m5o committed
672
673
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
674
</div>
Mark Otto's avatar
Mark Otto committed
675

676
## Sass mixins
Mark Otto's avatar
Mark Otto committed
677

Bardi Harborow's avatar
Bardi Harborow committed
678
When using Bootstrap's source Sass files, you have the option of using Sass variables and mixins to create custom, semantic, and responsive page layouts. Our predefined grid classes use these same variables and mixins to provide a whole suite of ready-to-use classes for fast responsive layouts.
Mark Otto's avatar
Mark Otto committed
679

680
681
682
683
684
685
### Variables

Variables and maps determine the number of columns, the gutter width, and the media query point at which to begin floating columns. We use these to generate the predefined grid classes documented above, as well as for the custom mixins listed below.

{% highlight scss %}
$grid-columns:      12;
686
$grid-gutter-width: 30px;
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714

$grid-breakpoints: (
  // Extra small screen / phone
  xs: 0,
  // Small screen / phone
  sm: 576px,
  // Medium screen / tablet
  md: 768px,
  // Large screen / desktop
  lg: 992px,
  // Extra large screen / wide desktop
  xl: 1200px
);

$container-max-widths: (
  sm: 540px,
  md: 720px,
  lg: 960px,
  xl: 1140px
);
{% endhighlight %}

### Mixins

Mixins are used in conjunction with the grid variables to generate semantic CSS for individual grid columns.

{% highlight scss %}
// Creates a wrapper for a series of columns
715
@include make-row();
716
717

// Make the element grid-ready (applying everything but the width)
718
@include make-col-ready();
Mark Otto's avatar
Mark Otto committed
719
@include make-col($size, $columns: $grid-columns);
Mark Otto's avatar
Mark Otto committed
720

721
// Get fancy by offsetting, or changing the sort order
Mark Otto's avatar
Mark Otto committed
722
@include make-col-offset($size, $columns: $grid-columns);
723
724
725
726
727
728
729
{% endhighlight %}

### Example usage

You can modify the variables to your own custom values, or just use the mixins with their default values. Here's an example of using the default settings to create a two-column layout with a gap between.

{% highlight scss %}
730
731
.example-container {
  width: 800px;
732
733
  @include make-container();
}
734
735

.example-row {
736
737
  @include make-row();
}
738
739

.example-content-main {
740
741
  @include make-col-ready();

742
  @include media-breakpoint-up(sm) {
743
744
    @include make-col(6);
  }
745
  @include media-breakpoint-up(lg) {
746
747
748
    @include make-col(8);
  }
}
749
750

.example-content-secondary {
751
752
  @include make-col-ready();

753
  @include media-breakpoint-up(sm) {
754
755
    @include make-col(6);
  }
756
  @include media-breakpoint-up(lg) {
757
758
759
760
761
    @include make-col(4);
  }
}
{% endhighlight %}

m5o's avatar
m5o committed
762
{% capture example %}
763
764
765
766
<div class="example-container">
  <div class="example-row">
    <div class="example-content-main">Main content</div>
    <div class="example-content-secondary">Secondary content</div>
767
  </div>
Mark Otto's avatar
Mark Otto committed
768
</div>
m5o's avatar
m5o committed
769
770
{% endcapture %}
{% include example.html content=example %}
771
772
773
774
775

## Customizing the grid

Using our built-in grid Sass variables and maps, it's possible to completely customize the predefined grid classes. Change the number of tiers, the media query dimensions, and the container widths—then recompile.

776
777
### Columns and gutters

778
The number of grid columns can be modified via Sass variables. `$grid-columns` is used to generate the widths (in percent) of each individual column while `$grid-gutter-width` sets the width for the column gutters.
779
780

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
781
782
$grid-columns: 12 !default;
$grid-gutter-width: 30px !default;
783
784
785
786
{% endhighlight %}

### Grid tiers

Mark Otto's avatar
Mark Otto committed
787
Moving beyond the columns themselves, you may also customize the number of grid tiers. If you wanted just four grid tiers, you'd update the `$grid-breakpoints` and `$container-max-widths` to something like this:
788
789
790

{% highlight scss %}
$grid-breakpoints: (
Mark Otto's avatar
Mark Otto committed
791
  xs: 0,
792
793
794
795
796
797
798
799
  sm: 480px,
  md: 768px,
  lg: 1024px
);

$container-max-widths: (
  sm: 420px,
  md: 720px,
800
  lg: 960px
801
);
802
803
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
804
When making any changes to the Sass variables or maps, you'll need to save your changes and recompile. Doing so will output a brand new set of predefined grid classes for column widths, offsets, and ordering. Responsive visibility utilities will also be updated to use the custom breakpoints. Make sure to set grid values in `px` (not `rem`, `em`, or `%`).