4 description: Documentation and examples for using Bootstrap's responsive flexbox grid system.
8 Bootstrap includes a powerful mobile-first flexbox grid system for building layouts of all shapes and sizes. It's based on a 12 column layout and has multiple tiers, one for each [media query range]({{ site.baseurl }}/layout/overview/#responsive-breakpoints). You can use it with Sass mixins or our predefined classes.
12 * Will be replaced with the ToC, excluding the "Contents" header
17 Bootstrap's grid system uses a series of containers, rows, and columns to layout and align content. It's built with flexbox and is fully responsive. Below is an example and an in-depth look at how the grid comes together.
19 <div class="bd-example bd-example-row">
20 <div class="container">
35 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`.
37 Breaking it down, here's how it works:
39 - Containers provide a means to center your site's contents. Use `.container` for fixed width or `.container-fluid` for full width.
40 - Rows are horizontal groups of columns that ensure your columns are lined up properly. We use the negative margin method on `.row` to ensure all your content is aligned properly down the left side.
41 - Content should be placed within columns, and only columns may be immediate children of rows.
42 - Thanks to flexbox, grid columns without a set width will automatically layout with equal widths. For example, four instances of `.col-sm` will each automatically be 25% wide for small breakpoints.
43 - 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, you can use `.col-sm-4`.
44 - Column `width`s are set in percentages, so they're always fluid and sized relative to their parent element.
45 - 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`.
46 - There are five grid tiers, one for each [responsive breakpoint]({{ site.baseurl }}/layout/overview/#responsive-breakpoints): all breakpoints (extra small), small, medium, large, and extra large.
47 - Grid tiers are based on minimum widths, meaning they apply to that one tier and all those above it (e.g., `.col-sm-4` applies to small, medium, large, and extra large devices).
48 - You can use predefined grid classes or Sass mixins for more semantic markup.
50 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#9-some-html-elements-cant-be-flex-containers).
52 Sounds good? Great, let's move on to seeing all that in an example.
56 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).
58 See how aspects of the Bootstrap grid system work across multiple devices with a handy table.
60 <table class="table table-bordered table-striped table-responsive">
64 <th class="text-center">
66 <small><576px</small>
68 <th class="text-center">
70 <small>≥576px</small>
72 <th class="text-center">
74 <small>≥768px</small>
76 <th class="text-center">
78 <small>≥992px</small>
80 <th class="text-center">
82 <small>≥1200px</small>
88 <th class="text-nowrap" scope="row">Grid behavior</th>
89 <td>Horizontal at all times</td>
90 <td colspan="4">Collapsed to start, horizontal above breakpoints</td>
93 <th class="text-nowrap" scope="row">Max container width</th>
101 <th class="text-nowrap" scope="row">Class prefix</th>
102 <td><code>.col-</code></td>
103 <td><code>.col-sm-</code></td>
104 <td><code>.col-md-</code></td>
105 <td><code>.col-lg-</code></td>
106 <td><code>.col-xl-</code></td>
109 <th class="text-nowrap" scope="row"># of columns</th>
110 <td colspan="5">12</td>
113 <th class="text-nowrap" scope="row">Gutter width</th>
114 <td colspan="5">30px (15px on each side of a column)</td>
117 <th class="text-nowrap" scope="row">Nestable</th>
118 <td colspan="5">Yes</td>
121 <th class="text-nowrap" scope="row">Offsets</th>
122 <td colspan="5">Yes</td>
125 <th class="text-nowrap" scope="row">Column ordering</th>
126 <td colspan="5">Yes</td>
131 ## Auto-layout columns
133 Utilize breakpoint-specific column classes for equal-width columns. Add any number of unit-less classes for each breakpoint you need and every column will be the same width.
137 For example, here are two grid layouts that apply to every device and viewport, from `xs` to `xl`.
139 <div class="bd-example-row">
141 <div class="container">
165 ### Setting one column width
167 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.
169 <div class="bd-example-row">
171 <div class="container">
198 ### Variable width content
200 Using the `col-{breakpoint}-auto` classes, columns can size itself based on the natural width of its content. This is super handy with single line content like inputs, numbers, etc. This, in conjunction with [horizontal alignment](#horizontal-alignment) classes, is very useful for centering layouts with uneven column sizes as viewport width changes.
202 <div class="bd-example-row">
204 <div class="container">
205 <div class="row justify-content-md-center">
206 <div class="col col-lg-2">
209 <div class="col-12 col-md-auto">
210 Variable width content
212 <div class="col col-lg-2">
220 <div class="col-12 col-md-auto">
221 Variable width content
223 <div class="col col-lg-2">
231 ### Equal-width multi-row
233 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 }}/utilities/display-property/).
235 <div class="bd-example-row">
238 <div class="col">col</div>
239 <div class="col">col</div>
240 <div class="w-100"></div>
241 <div class="col">col</div>
242 <div class="col">col</div>
247 ## Responsive classes
249 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.
253 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`.
255 <div class="bd-example-row">
258 <div class="col">col</div>
259 <div class="col">col</div>
260 <div class="col">col</div>
261 <div class="col">col</div>
264 <div class="col-8">col-8</div>
265 <div class="col-4">col-4</div>
270 ### Stacked to horizontal
272 Using a single set of `.col-sm-*` classes, you can create a basic grid system that starts out stacked on extra small devices before becoming horizontal on desktop (medium) devices.
274 <div class="bd-example-row">
277 <div class="col-sm-8">col-sm-8</div>
278 <div class="col-sm-4">col-sm-4</div>
281 <div class="col-sm">col-sm</div>
282 <div class="col-sm">col-sm</div>
283 <div class="col-sm">col-sm</div>
290 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.
292 <div class="bd-example-row">
294 <!-- Stack the columns on mobile by making one full-width and the other half-width -->
296 <div class="col col-md-8">.col .col-md-8</div>
297 <div class="col-6 col-md-4">.col-6 .col-md-4</div>
300 <!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
302 <div class="col-6 col-md-4">.col-6 .col-md-4</div>
303 <div class="col-6 col-md-4">.col-6 .col-md-4</div>
304 <div class="col-6 col-md-4">.col-6 .col-md-4</div>
307 <!-- Columns are always 50% wide, on mobile and desktop -->
309 <div class="col-6">.col-6</div>
310 <div class="col-6">.col-6</div>
317 Use flexbox alignment utilities to vertically and horizontally align columns.
319 ### Vertical alignment
321 <div class="bd-example-row bd-example-row-flex-cols">
323 <div class="container">
324 <div class="row align-items-start">
335 <div class="row align-items-center">
346 <div class="row align-items-end">
361 <div class="bd-example-row bd-example-row-flex-cols">
363 <div class="container">
365 <div class="col align-self-start">
368 <div class="col align-self-center">
371 <div class="col align-self-end">
379 ### Horizontal alignment
381 <div class="bd-example-row">
383 <div class="container">
384 <div class="row justify-content-start">
392 <div class="row justify-content-center">
400 <div class="row justify-content-end">
408 <div class="row justify-content-around">
416 <div class="row justify-content-between">
430 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.
432 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 }}/utilities/spacing/).
447 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).
449 <div class="bd-example-row">
451 <div class="row no-gutters">
452 <div class="col-12 col-sm-6 col-md-8">.col-12 .col-sm-6 .col-md-8</div>
453 <div class="col-6 col-md-4">.col-6 .col-md-4</div>
460 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.
462 <div class="bd-example-row">
465 <div class="col-9">.col-9</div>
466 <div class="col-4">.col-4<br>Since 9 + 4 = 13 > 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
467 <div class="col-6">.col-6<br>Subsequent columns continue along the new line.</div>
474 With the handful of grid tiers available, you're bound to run into issues where, at certain breakpoints, your columns don't clear quite right as one is taller than the other. To fix that, use a combination of a `.clearfix` and our [responsive utility classes]({{ site.baseurl }}/layout/responsive-utilities/).
476 <div class="bd-example-row">
479 <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
480 <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
482 <!-- Add the extra clearfix for only the required viewport -->
483 <div class="clearfix hidden-sm-up"></div>
485 <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
486 <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
491 In addition to column clearing at responsive breakpoints, you may need to **reset offsets, pushes, or pulls**. See this in action in [the grid example]({{ site.baseurl }}/examples/grid/).
493 <div class="bd-example-row">
496 <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
497 <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>
501 <div class="col-sm-6 col-md-5 col-lg-6">.col.col-sm-6.col-md-5.col-lg-6</div>
502 <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>
511 Use flexbox utilities for controlling the **visual order** of your content.
513 <div class="bd-example-row">
515 <div class="container">
517 <div class="col flex-unordered">
520 <div class="col flex-last">
523 <div class="col flex-first">
531 ### Offsetting columns
533 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.
535 <div class="bd-example-row">
538 <div class="col-md-4">.col-md-4</div>
539 <div class="col-md-4 offset-md-4">.col-md-4 .offset-md-4</div>
542 <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
543 <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
546 <div class="col-md-6 offset-md-3">.col-md-6 .offset-md-3</div>
553 Easily change the order of our built-in grid columns with `.push-md-*` and `.pull-md-*` modifier classes.
555 <div class="bd-example-row">
558 <div class="col-md-9 push-md-3">.col-md-9 .push-md-3</div>
559 <div class="col-md-3 pull-md-9">.col-md-3 .pull-md-9</div>
566 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).
568 <div class="bd-example-row">
571 <div class="col-sm-9">
574 <div class="col-8 col-sm-6">
575 Level 2: .col-8 .col-sm-6
577 <div class="col-4 col-sm-6">
578 Level 2: .col-4 .col-sm-6
588 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](#predefined-classes) use these same variables and mixins to provide a whole suite of ready-to-use classes for fast responsive layouts.
592 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.
596 $grid-gutter-width-base: 30px;
598 $grid-gutter-widths: (
599 xs: $grid-gutter-width-base, // 30px
600 sm: $grid-gutter-width-base, // 30px
601 md: $grid-gutter-width-base, // 30px
602 lg: $grid-gutter-width-base, // 30px
603 xl: $grid-gutter-width-base // 30px
607 // Extra small screen / phone
609 // Small screen / phone
611 // Medium screen / tablet
613 // Large screen / desktop
615 // Extra large screen / wide desktop
619 $container-max-widths: (
629 Mixins are used in conjunction with the grid variables to generate semantic CSS for individual grid columns.
632 // Creates a wrapper for a series of columns
633 @mixin make-row($gutters: $grid-gutter-widths) {
637 @each $breakpoint in map-keys($gutters) {
638 @include media-breakpoint-up($breakpoint) {
639 $gutter: map-get($gutters, $breakpoint);
640 margin-right: ($gutter / -2);
641 margin-left: ($gutter / -2);
646 // Make the element grid-ready (applying everything but the width)
647 @mixin make-col-ready($gutters: $grid-gutter-widths) {
649 // Prevent columns from becoming too narrow when at smaller grid tiers by
650 // always setting `width: 100%;`. This works because we use `flex` values
651 // later on to override this initial width.
653 min-height: 1px; // Prevent collapsing
655 @each $breakpoint in map-keys($gutters) {
656 @include media-breakpoint-up($breakpoint) {
657 $gutter: map-get($gutters, $breakpoint);
658 padding-right: ($gutter / 2);
659 padding-left: ($gutter / 2);
664 @mixin make-col($size, $columns: $grid-columns) {
665 flex: 0 0 percentage($size / $columns);
666 width: percentage($size / $columns);
667 // Add a `max-width` to ensure content within each column does not blow out
668 // the width of the column. Applies to IE10+ and Firefox. Chrome and Safari
669 // do not appear to require this.
670 max-width: percentage($size / $columns);
673 // Get fancy by offsetting, or changing the sort order
674 @mixin make-col-offset($size, $columns: $grid-columns) {
675 margin-left: percentage($size / $columns);
678 @mixin make-col-push($size, $columns: $grid-columns) {
679 left: if($size > 0, percentage($size / $columns), auto);
682 @mixin make-col-pull($size, $columns: $grid-columns) {
683 right: if($size > 0, percentage($size / $columns), auto);
689 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.
691 See it in action in <a href="https://jsbin.com/ruxona/edit?html,output">this rendered example</a>.
696 @include make-container();
702 @include make-col-ready();
704 @media (max-width: 32em) {
705 @include make-col(6);
707 @media (min-width: 32.1em) {
708 @include make-col(8);
712 @include make-col-ready();
714 @media (max-width: 32em) {
715 @include make-col(6);
717 @media (min-width: 32.1em) {
718 @include make-col(4);
724 <div class="container">
726 <div class="content-main">...</div>
727 <div class="content-secondary">...</div>
732 ## Customizing the grid
734 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.
736 ### Columns and gutters
738 The number of grid columns and their horizontal padding (aka, gutters) can be modified via Sass variables. `$grid-columns` is used to generate the widths (in percent) of each individual column while `$grid-gutter-widths` allows breakpoint-specific widths that are divided evenly across `padding-left` and `padding-right` for the column gutters.
741 $grid-columns: 12 !default;
742 $grid-gutter-width-base: 30px !default;
743 $grid-gutter-widths: (
744 xs: $grid-gutter-width-base,
745 sm: $grid-gutter-width-base,
746 md: $grid-gutter-width-base,
747 lg: $grid-gutter-width-base,
748 xl: $grid-gutter-width-base
754 Moving beyond the columns themselves, you may also customize the number of grid tiers. If you wanted just three grid tiers, you'd update the `$grid-breakpoints` and `$container-max-widths` to something like this:
763 $container-max-widths: (
770 When making any changes to the Sass variables or maps, you'll need to save your changes and recompile. Doing so will out a brand new set of predefined grid classes for column widths, offsets, pushes, and pulls. Responsive visibility utilities will also be updated to use the custom breakpoints.