grid.md 26.6 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">
XhmikosR's avatar
XhmikosR committed
16
{{< 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>
XhmikosR's avatar
XhmikosR committed
30
{{< /example >}}
31
</div>
Mark Otto's avatar
Mark Otto committed
32

Bardi Harborow's avatar
Bardi Harborow committed
33
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
34

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

Mark Otto's avatar
Mark Otto committed
37
- 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.
38
39
- 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.
40
41
- 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`.
42
43
- 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`.
XhmikosR's avatar
XhmikosR committed
44
- To make the grid responsive, there are five grid breakpoints, one for each [responsive breakpoint]({{< docsref "/layout/overview#responsive-breakpoints" >}}): all breakpoints (extra small), small, medium, large, and extra large.
45
46
- 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.
47

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

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

Mark Otto's avatar
Mark Otto committed
52
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).
53

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

Mark Otto's avatar
Mark Otto committed
56
<table class="table">
57
58
59
  <thead>
    <tr>
      <th></th>
Mark Otto's avatar
Mark Otto committed
60
      <th>
61
        Extra small<br>
Mark Otto's avatar
Mark Otto committed
62
        <span class="font-weight-normal">&lt;576px</span>
63
      </th>
Mark Otto's avatar
Mark Otto committed
64
      <th>
65
        Small<br>
Mark Otto's avatar
Mark Otto committed
66
        <span class="font-weight-normal">&ge;576px</span>
67
      </th>
Mark Otto's avatar
Mark Otto committed
68
      <th>
69
        Medium<br>
Mark Otto's avatar
Mark Otto committed
70
        <span class="font-weight-normal">&ge;768px</span>
71
      </th>
Mark Otto's avatar
Mark Otto committed
72
      <th>
73
        Large<br>
Mark Otto's avatar
Mark Otto committed
74
        <span class="font-weight-normal">&ge;992px</span>
75
      </th>
Mark Otto's avatar
Mark Otto committed
76
      <th>
77
        Extra large<br>
Mark Otto's avatar
Mark Otto committed
78
        <span class="font-weight-normal">&ge;1200px</span>
79
80
81
82
83
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
Mark Otto's avatar
Mark Otto committed
84
      <th class="text-nowrap" scope="row">Container <code class="font-weight-normal">max-width</code></th>
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
      <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
117

118
## Auto-layout columns
119

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

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

124
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
125

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

152
153
### Equal-width multi-line

XhmikosR's avatar
XhmikosR committed
154
Create equal-width columns that span multiple lines by inserting a `.w-100` where you want the columns to break to a new line. Make the breaks responsive by mixing `.w-100` with some [responsive display utilities]({{< docsref "/utilities/display" >}}).
155

XhmikosR's avatar
XhmikosR committed
156
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 your target browsers don't fall into the buggy versions.
Mark Otto's avatar
Mark Otto committed
157

158
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
159
{{< example >}}
160
161
<div class="container">
  <div class="row">
162
163
    <div class="col">col</div>
    <div class="col">col</div>
164
    <div class="w-100"></div>
165
166
    <div class="col">col</div>
    <div class="col">col</div>
167
168
  </div>
</div>
XhmikosR's avatar
XhmikosR committed
169
{{< /example >}}
170
171
</div>

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

174
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
175

176
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
177
{{< example >}}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<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>
XhmikosR's avatar
XhmikosR committed
202
{{< /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">
XhmikosR's avatar
XhmikosR committed
210
{{< 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>
XhmikosR's avatar
XhmikosR committed
235
{{< /example >}}
236
</div>
Mark Otto's avatar
Mark Otto committed
237

238
## Responsive classes
239

240
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.
241

242
### All breakpoints
Mark Otto's avatar
Mark Otto committed
243

244
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
245

Mark Otto's avatar
Mark Otto committed
246
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
247
{{< example >}}
248
249
250
251
252
253
254
255
256
257
258
<div class="container">
  <div class="row">
    <div class="col">col</div>
    <div class="col">col</div>
    <div class="col">col</div>
    <div class="col">col</div>
  </div>
  <div class="row">
    <div class="col-8">col-8</div>
    <div class="col-4">col-4</div>
  </div>
259
</div>
XhmikosR's avatar
XhmikosR committed
260
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
261
</div>
262
263
264

### Stacked to horizontal

265
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`).
266
267

<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
268
{{< example >}}
269
270
271
272
273
274
275
276
277
278
<div class="container">
  <div class="row">
    <div class="col-sm-8">col-sm-8</div>
    <div class="col-sm-4">col-sm-4</div>
  </div>
  <div class="row">
    <div class="col-sm">col-sm</div>
    <div class="col-sm">col-sm</div>
    <div class="col-sm">col-sm</div>
  </div>
Mark Otto's avatar
Mark Otto committed
279
</div>
XhmikosR's avatar
XhmikosR committed
280
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
281
</div>
Mark Otto's avatar
Mark Otto committed
282

283
### Mix and match
Mark Otto's avatar
Mark Otto committed
284

285
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
286

Mark Otto's avatar
Mark Otto committed
287
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
288
{{< example >}}
289
290
291
<div class="container">
  <!-- Stack the columns on mobile by making one full-width and the other half-width -->
  <div class="row">
292
    <div class="col-md-8">.col-md-8</div>
293
294
    <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  </div>
Mark Otto's avatar
Mark Otto committed
295

296
297
298
299
300
301
  <!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
  <div class="row">
    <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>
  </div>
Mark Otto's avatar
Mark Otto committed
302

303
304
305
306
307
  <!-- Columns are always 50% wide, on mobile and desktop -->
  <div class="row">
    <div class="col-6">.col-6</div>
    <div class="col-6">.col-6</div>
  </div>
Mark Otto's avatar
Mark Otto committed
308
</div>
XhmikosR's avatar
XhmikosR committed
309
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
310
</div>
Mark Otto's avatar
Mark Otto committed
311

Mark Otto's avatar
Mark Otto committed
312
313
### Gutters

314
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. The `.container` or `.container-fluid` parent may need to be adjusted too to avoid unwanted overflow, using again matching padding utility.
Mark Otto's avatar
Mark Otto committed
315

316
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`, counteracted that with `.mx-lg-n5` on the parent `.row` and then adjusted the `.container` wrapper with `.px-lg-5`.
Mark Otto's avatar
Mark Otto committed
317

XhmikosR's avatar
XhmikosR committed
318
{{< example >}}
319
320
321
322
323
<div class="container px-lg-5">
  <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>
Mark Otto's avatar
Mark Otto committed
324
</div>
XhmikosR's avatar
XhmikosR committed
325
{{< /example >}}
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
### Row columns

Use the responsive `.row-cols-*` classes to quickly set the number of columns that best render your content and layout. Whereas normal `.col-*` classes apply to the individual columns (e.g., `.col-md-4`), the row columns classes are set on the parent `.row` as a shortcut.

Use these row columns classes to quickly create basic grid layouts or to control your card layouts.

<div class="bd-example-row">
{{< example >}}
<div class="container">
  <div class="row row-cols-2">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>
{{< /example >}}
</div>

<div class="bd-example-row">
{{< example >}}
<div class="container">
  <div class="row row-cols-3">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>
{{< /example >}}
</div>

<div class="bd-example-row">
{{< example >}}
<div class="container">
  <div class="row row-cols-4">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>
{{< /example >}}
</div>

Mark Otto's avatar
Cleanup    
Mark Otto committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
You can also use the accompanying Sass mixin, `row-cols()`:

{{< highlight scss >}}
.element {
  // Three columns to start
  @include row-cols(3);

  // Five columns from medium breakpoint up
  @include media-breakpoint-up(md) {
    @include row-cols(5);
  }
}
{{< /highlight >}}

386
387
## Alignment

388
Use flexbox alignment utilities to vertically and horizontally align columns. **Internet Explorer 10-11 do not support vertical alignment of flex items when the flex container has a `min-height` as shown below.** [See Flexbugs #3 for more details.](https://github.com/philipwalton/flexbugs#flexbug-3)
Mark Otto's avatar
Mark Otto committed
389

390
### Vertical alignment
Mark Otto's avatar
Mark Otto committed
391

392
<div class="bd-example-row bd-example-row-flex-cols">
XhmikosR's avatar
XhmikosR committed
393
{{< example >}}
394
<div class="container">
395
  <div class="row align-items-start">
396
397
398
399
400
401
402
403
404
405
    <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>
406
  <div class="row align-items-center">
407
408
409
410
411
412
413
414
415
416
    <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>
417
  <div class="row align-items-end">
418
419
420
421
422
423
424
425
426
427
    <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
428
</div>
XhmikosR's avatar
XhmikosR committed
429
{{< /example >}}
430
431
432
</div>

<div class="bd-example-row bd-example-row-flex-cols">
XhmikosR's avatar
XhmikosR committed
433
{{< example >}}
434
435
<div class="container">
  <div class="row">
436
    <div class="col align-self-start">
437
438
      One of three columns
    </div>
439
    <div class="col align-self-center">
440
441
      One of three columns
    </div>
442
    <div class="col align-self-end">
443
444
445
446
      One of three columns
    </div>
  </div>
</div>
XhmikosR's avatar
XhmikosR committed
447
{{< /example >}}
448
449
450
451
452
</div>

### Horizontal alignment

<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
453
{{< example >}}
454
<div class="container">
455
  <div class="row justify-content-start">
456
457
458
459
460
461
462
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
463
  <div class="row justify-content-center">
464
465
466
467
468
469
470
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
471
  <div class="row justify-content-end">
472
473
474
475
476
477
478
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
479
  <div class="row justify-content-around">
480
481
482
483
484
485
486
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
487
  <div class="row justify-content-between">
488
489
490
491
492
493
494
    <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
495
</div>
XhmikosR's avatar
XhmikosR committed
496
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
497
</div>
Mark Otto's avatar
Mark Otto committed
498

499
### No gutters
500

501
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.
502

XhmikosR's avatar
XhmikosR committed
503
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]({{< docsref "/utilities/spacing" >}}).
504

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

XhmikosR's avatar
XhmikosR committed
507
{{< highlight scss >}}
508
509
510
511
.no-gutters {
  margin-right: 0;
  margin-left: 0;

512
  > .col,
513
514
515
516
517
  > [class*="col-"] {
    padding-right: 0;
    padding-left: 0;
  }
}
XhmikosR's avatar
XhmikosR committed
518
{{< /highlight >}}
519
520
521
522

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">
XhmikosR's avatar
XhmikosR committed
523
{{< example >}}
524
<div class="row no-gutters">
525
  <div class="col-sm-6 col-md-8">.col-sm-6 .col-md-8</div>
526
527
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
XhmikosR's avatar
XhmikosR committed
528
{{< /example >}}
529
530
</div>

531
### Column wrapping
Mark Otto's avatar
Mark Otto committed
532
533
534

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
535
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
536
{{< example >}}
537
538
539
540
541
542
<div class="container">
  <div class="row">
    <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>
  </div>
Mark Otto's avatar
Mark Otto committed
543
</div>
XhmikosR's avatar
XhmikosR committed
544
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
545
</div>
Mark Otto's avatar
Mark Otto committed
546

547
### Column breaks
Mark Otto's avatar
Mark Otto committed
548

Gijs Boddeus's avatar
Gijs Boddeus committed
549
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
550

Mark Otto's avatar
Mark Otto committed
551
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
552
{{< example >}}
553
554
555
556
<div class="container">
  <div class="row">
    <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
557

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

561
562
563
    <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>
  </div>
Mark Otto's avatar
Mark Otto committed
564
</div>
XhmikosR's avatar
XhmikosR committed
565
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
566
</div>
Mark Otto's avatar
Mark Otto committed
567

XhmikosR's avatar
XhmikosR committed
568
You may also apply this break at specific breakpoints with our [responsive display utilities]({{< docsref "/utilities/display" >}}).
Mark Otto's avatar
Mark Otto committed
569

570
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
571
{{< example >}}
572
573
574
575
<div class="container">
  <div class="row">
    <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
576

577
578
    <!-- Force next columns to break to new line at md breakpoint and up -->
    <div class="w-100 d-none d-md-block"></div>
579

580
581
582
    <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>
  </div>
583
</div>
XhmikosR's avatar
XhmikosR committed
584
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
585
586
</div>

587
588
## Reordering

589
### Order classes
590

591
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 `5` across all five grid tiers.
592
593

<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
594
{{< example >}}
595
596
<div class="container">
  <div class="row">
597
    <div class="col">
598
      First in DOM, no order applied
599
    </div>
600
    <div class="col order-5">
601
      Second in DOM, with a larger order
602
    </div>
603
    <div class="col order-1">
604
      Third in DOM, with an order of 1
605
606
607
    </div>
  </div>
</div>
XhmikosR's avatar
XhmikosR committed
608
{{< /example >}}
609
610
</div>

611
There are also responsive `.order-first` and `.order-last` classes that change the `order` of an element by applying `order: -1` and `order: 6`, respectively. These classes can also be intermixed with the numbered `.order-*` classes as needed.
612
613

<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
614
{{< example >}}
615
616
<div class="container">
  <div class="row">
617
    <div class="col order-last">
618
      First in DOM, ordered last
619
620
    </div>
    <div class="col">
621
      Second in DOM, unordered
622
623
    </div>
    <div class="col order-first">
624
      Third in DOM, ordered first
625
626
627
    </div>
  </div>
</div>
XhmikosR's avatar
XhmikosR committed
628
{{< /example >}}
629
630
</div>

631
### Offsetting columns
Mark Otto's avatar
Mark Otto committed
632

XhmikosR's avatar
XhmikosR committed
633
You can offset grid columns in two ways: our responsive `.offset-` grid classes and our [margin utilities]({{< docsref "/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.
Mark Otto's avatar
Mark Otto committed
634
635
636
637
638
639

#### 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">
XhmikosR's avatar
XhmikosR committed
640
{{< example >}}
641
642
643
644
645
646
647
648
649
650
651
652
<div class="container">
  <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>
Mark Otto's avatar
Mark Otto committed
653
</div>
XhmikosR's avatar
XhmikosR committed
654
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
655
656
</div>

XhmikosR's avatar
XhmikosR committed
657
In addition to column clearing at responsive breakpoints, you may need to reset offsets. See this in action in [the grid example]({{< docsref "/examples/grid" >}}).
658
659

<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
660
{{< example >}}
661
662
663
664
665
666
667
668
669
<div class="container">
  <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">
    <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
    <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>
670
</div>
XhmikosR's avatar
XhmikosR committed
671
{{< /example >}}
672
673
</div>

Mark Otto's avatar
Mark Otto committed
674
675
676
#### 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
677

Mark Otto's avatar
Mark Otto committed
678
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
679
{{< example >}}
680
681
682
683
684
685
686
687
688
689
690
691
692
<div class="container">
  <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-4 ml-auto">.col-md-4 .ml-auto</div>
  </div>
  <div class="row">
    <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>
  </div>
  <div class="row">
    <div class="col-auto mr-auto">.col-auto .mr-auto</div>
    <div class="col-auto">.col-auto</div>
  </div>
693
</div>
XhmikosR's avatar
XhmikosR committed
694
{{< /example >}}
695
696
697
</div>

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

Mark Otto's avatar
Mark Otto committed
699
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
700

Mark Otto's avatar
Mark Otto committed
701
<div class="bd-example-row">
XhmikosR's avatar
XhmikosR committed
702
{{< example >}}
703
704
705
706
707
708
709
710
711
712
713
<div class="container">
  <div class="row">
    <div class="col-sm-9">
      Level 1: .col-sm-9
      <div class="row">
        <div class="col-8 col-sm-6">
          Level 2: .col-8 .col-sm-6
        </div>
        <div class="col-4 col-sm-6">
          Level 2: .col-4 .col-sm-6
        </div>
Mark Otto's avatar
Mark Otto committed
714
715
716
717
      </div>
    </div>
  </div>
</div>
XhmikosR's avatar
XhmikosR committed
718
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
719
</div>
Mark Otto's avatar
Mark Otto committed
720

721
## Sass mixins
Mark Otto's avatar
Mark Otto committed
722

Bardi Harborow's avatar
Bardi Harborow committed
723
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
724

725
726
727
728
### 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.

XhmikosR's avatar
XhmikosR committed
729
{{< highlight scss >}}
730
$grid-columns:      12;
731
$grid-gutter-width: 30px;
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

$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
);
XhmikosR's avatar
XhmikosR committed
752
{{< /highlight >}}
753
754
755
756
757

### Mixins

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

XhmikosR's avatar
XhmikosR committed
758
{{< highlight scss >}}
759
// Creates a wrapper for a series of columns
760
@include make-row();
761
762

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

766
// Get fancy by offsetting, or changing the sort order
Mark Otto's avatar
Mark Otto committed
767
@include make-col-offset($size, $columns: $grid-columns);
XhmikosR's avatar
XhmikosR committed
768
{{< /highlight >}}
769
770
771
772
773

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

XhmikosR's avatar
XhmikosR committed
774
{{< highlight scss >}}
775
776
.example-container {
  width: 800px;
777
778
  @include make-container();
}
779
780

.example-row {
781
782
  @include make-row();
}
783
784

.example-content-main {
785
786
  @include make-col-ready();

787
  @include media-breakpoint-up(sm) {
788
789
    @include make-col(6);
  }
790
  @include media-breakpoint-up(lg) {
791
792
793
    @include make-col(8);
  }
}
794
795

.example-content-secondary {
796
797
  @include make-col-ready();

798
  @include media-breakpoint-up(sm) {
799
800
    @include make-col(6);
  }
801
  @include media-breakpoint-up(lg) {
802
803
804
    @include make-col(4);
  }
}
XhmikosR's avatar
XhmikosR committed
805
{{< /highlight >}}
806

XhmikosR's avatar
XhmikosR committed
807
{{< example >}}
808
809
810
811
<div class="example-container">
  <div class="example-row">
    <div class="example-content-main">Main content</div>
    <div class="example-content-secondary">Secondary content</div>
812
  </div>
Mark Otto's avatar
Mark Otto committed
813
</div>
XhmikosR's avatar
XhmikosR committed
814
{{< /example >}}
815
816
817
818
819

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

820
821
### Columns and gutters

822
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.
823

XhmikosR's avatar
XhmikosR committed
824
{{< highlight scss >}}
Mark Otto's avatar
Mark Otto committed
825
826
$grid-columns: 12 !default;
$grid-gutter-width: 30px !default;
XhmikosR's avatar
XhmikosR committed
827
{{< /highlight >}}
828
829
830

### Grid tiers

Mark Otto's avatar
Mark Otto committed
831
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:
832

XhmikosR's avatar
XhmikosR committed
833
{{< highlight scss >}}
834
$grid-breakpoints: (
Mark Otto's avatar
Mark Otto committed
835
  xs: 0,
836
837
838
839
840
841
842
843
  sm: 480px,
  md: 768px,
  lg: 1024px
);

$container-max-widths: (
  sm: 420px,
  md: 720px,
844
  lg: 960px
845
);
XhmikosR's avatar
XhmikosR committed
846
{{< /highlight >}}
847

Mark Otto's avatar
Mark Otto committed
848
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 `%`).