1 <div class="bs-docs-section">
2 <h1 id="grid" class="page-header">Grid system</h1>
4 <p class="lead">Bootstrap includes a responsive, mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. It includes <a href="#grid-example-basic">predefined classes</a> for easy layout options, as well as powerful <a href="#grid-less">mixins for generating more semantic layouts</a>.</p>
6 <h2 id="grid-intro">Introduction</h2>
7 <p>Grid systems are used for creating page layouts through a series of rows and columns that house your content. Here's how the Bootstrap grid system works:</p>
9 <li>Rows must be placed within a <code>.container</code> (fixed-width) or <code>.container-fluid</code> (full-width) for proper alignment and padding.</li>
10 <li>Use rows to create horizontal groups of columns.</li>
11 <li>Content should be placed within columns, and only columns may be immediate children of rows.</li>
12 <li>Predefined grid classes like <code>.row</code> and <code>.col-xs-4</code> are available for quickly making grid layouts. Less mixins can also be used for more semantic layouts.</li>
13 <li>Columns create gutters (gaps between column content) via <code>padding</code>. That padding is offset in rows for the first and last column via negative margin on <code>.row</code>s.</li>
14 <li>The negative margin is why the examples below are outdented. It's so that content within grid columns is lined up with non-grid content.</li>
15 <li>Grid columns are created by specifying the number of twelve available columns you wish to span. For example, three equal columns would use three <code>.col-xs-4</code>.</li>
16 <li>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.</li>
17 <li>Grid classes apply to devices with screen widths greater than or equal to the breakpoint sizes, and override grid classes targeted at smaller devices. Therefore, e.g. applying any <code>.col-md-*</code> class to an element will not only affect its styling on medium devices but also on large devices if a <code>.col-lg-*</code> class is not present.</li>
19 <p>Look to the examples for applying these principles to your code.</p>
21 <h2 id="grid-media-queries">Media queries</h2>
22 <p>We use the following media queries in our Less files to create the key breakpoints in our grid system.</p>
24 /* Extra small devices (phones, less than 768px) */
25 /* No media query since this is the default in Bootstrap */
27 /* Small devices (tablets, 768px and up) */
28 @media (min-width: @screen-sm-min) { ... }
30 /* Medium devices (desktops, 992px and up) */
31 @media (min-width: @screen-md-min) { ... }
33 /* Large devices (large desktops, 1200px and up) */
34 @media (min-width: @screen-lg-min) { ... }
36 <p>We occasionally expand on these media queries to include a <code>max-width</code> to limit CSS to a narrower set of devices.</p>
38 @media (max-width: @screen-xs-max) { ... }
39 @media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
40 @media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... }
41 @media (min-width: @screen-lg-min) { ... }
44 <h2 id="grid-options">Grid options</h2>
45 <p>See how aspects of the Bootstrap grid system work across multiple devices with a handy table.</p>
46 <div class="table-responsive">
47 <table class="table table-bordered table-striped">
53 <small>Phones (<768px)</small>
57 <small>Tablets (≥768px)</small>
61 <small>Desktops (≥992px)</small>
65 <small>Desktops (≥1200px)</small>
71 <th class="text-nowrap" scope="row">Grid behavior</th>
72 <td>Horizontal at all times</td>
73 <td colspan="3">Collapsed to start, horizontal above breakpoints</td>
76 <th class="text-nowrap" scope="row">Container width</th>
83 <th class="text-nowrap" scope="row">Class prefix</th>
84 <td><code>.col-xs-</code></td>
85 <td><code>.col-sm-</code></td>
86 <td><code>.col-md-</code></td>
87 <td><code>.col-lg-</code></td>
90 <th class="text-nowrap" scope="row"># of columns</th>
91 <td colspan="4">12</td>
94 <th class="text-nowrap" scope="row">Column width</th>
95 <td class="text-muted">Auto</td>
101 <th class="text-nowrap" scope="row">Gutter width</th>
102 <td colspan="4">30px (15px on each side of a column)</td>
105 <th class="text-nowrap" scope="row">Nestable</th>
106 <td colspan="4">Yes</td>
109 <th class="text-nowrap" scope="row">Offsets</th>
110 <td colspan="4">Yes</td>
113 <th class="text-nowrap" scope="row">Column ordering</th>
114 <td colspan="4">Yes</td>
120 <h2 id="grid-example-basic">Example: Stacked-to-horizontal</h2>
121 <p>Using a single set of <code>.col-md-*</code> grid classes, you can create a basic grid system that starts out stacked on mobile devices and tablet devices (the extra small to small range) before becoming horizontal on desktop (medium) devices. Place grid columns in any <code>.row</code>.</p>
122 <div class="bs-docs-grid">
123 <div class="row show-grid">
124 <div class="col-md-1">.col-md-1</div>
125 <div class="col-md-1">.col-md-1</div>
126 <div class="col-md-1">.col-md-1</div>
127 <div class="col-md-1">.col-md-1</div>
128 <div class="col-md-1">.col-md-1</div>
129 <div class="col-md-1">.col-md-1</div>
130 <div class="col-md-1">.col-md-1</div>
131 <div class="col-md-1">.col-md-1</div>
132 <div class="col-md-1">.col-md-1</div>
133 <div class="col-md-1">.col-md-1</div>
134 <div class="col-md-1">.col-md-1</div>
135 <div class="col-md-1">.col-md-1</div>
137 <div class="row show-grid">
138 <div class="col-md-8">.col-md-8</div>
139 <div class="col-md-4">.col-md-4</div>
141 <div class="row show-grid">
142 <div class="col-md-4">.col-md-4</div>
143 <div class="col-md-4">.col-md-4</div>
144 <div class="col-md-4">.col-md-4</div>
146 <div class="row show-grid">
147 <div class="col-md-6">.col-md-6</div>
148 <div class="col-md-6">.col-md-6</div>
153 <div class="col-md-1">.col-md-1</div>
154 <div class="col-md-1">.col-md-1</div>
155 <div class="col-md-1">.col-md-1</div>
156 <div class="col-md-1">.col-md-1</div>
157 <div class="col-md-1">.col-md-1</div>
158 <div class="col-md-1">.col-md-1</div>
159 <div class="col-md-1">.col-md-1</div>
160 <div class="col-md-1">.col-md-1</div>
161 <div class="col-md-1">.col-md-1</div>
162 <div class="col-md-1">.col-md-1</div>
163 <div class="col-md-1">.col-md-1</div>
164 <div class="col-md-1">.col-md-1</div>
167 <div class="col-md-8">.col-md-8</div>
168 <div class="col-md-4">.col-md-4</div>
171 <div class="col-md-4">.col-md-4</div>
172 <div class="col-md-4">.col-md-4</div>
173 <div class="col-md-4">.col-md-4</div>
176 <div class="col-md-6">.col-md-6</div>
177 <div class="col-md-6">.col-md-6</div>
181 <h2 id="grid-example-fluid">Example: Fluid container</h2>
182 <p>Turn any fixed-width grid layout into a full-width layout by changing your outermost <code>.container</code> to <code>.container-fluid</code>.</p>
184 <div class="container-fluid">
191 <h2 id="grid-example-mixed">Example: Mobile and desktop</h2>
192 <p>Don't want your columns to simply stack in smaller devices? Use the extra small and medium device grid classes by adding <code>.col-xs-*</code> <code>.col-md-*</code> to your columns. See the example below for a better idea of how it all works.</p>
193 <div class="bs-docs-grid">
194 <div class="row show-grid">
195 <div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div>
196 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
198 <div class="row show-grid">
199 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
200 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
201 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
203 <div class="row show-grid">
204 <div class="col-xs-6">.col-xs-6</div>
205 <div class="col-xs-6">.col-xs-6</div>
209 <!-- Stack the columns on mobile by making one full-width and the other half-width -->
211 <div class="col-xs-12 col-md-8">.col-xs-12 .col-md-8</div>
212 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
215 <!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
217 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
218 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
219 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
222 <!-- Columns are always 50% wide, on mobile and desktop -->
224 <div class="col-xs-6">.col-xs-6</div>
225 <div class="col-xs-6">.col-xs-6</div>
229 <h2 id="grid-example-mixed-complete">Example: Mobile, tablet, desktop</h2>
230 <p>Build on the previous example by creating even more dynamic and powerful layouts with tablet <code>.col-sm-*</code> classes.</p>
231 <div class="bs-docs-grid">
232 <div class="row show-grid">
233 <div class="col-xs-12 col-sm-6 col-md-8">.col-xs-12 .col-sm-6 .col-md-8</div>
234 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
236 <div class="row show-grid">
237 <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
238 <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
239 <!-- Optional: clear the XS cols if their content doesn't match in height -->
240 <div class="clearfix visible-xs-block"></div>
241 <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
246 <div class="col-xs-12 col-sm-6 col-md-8">.col-xs-12 .col-sm-6 .col-md-8</div>
247 <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
250 <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
251 <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
252 <!-- Optional: clear the XS cols if their content doesn't match in height -->
253 <div class="clearfix visible-xs-block"></div>
254 <div class="col-xs-6 col-sm-4">.col-xs-6 .col-sm-4</div>
258 <h2 id="grid-example-wrapping">Example: Column wrapping</h2>
259 <p>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.</p>
260 <div class="bs-docs-grid">
261 <div class="row show-grid">
262 <div class="col-xs-9">.col-xs-9</div>
263 <div class="col-xs-4">.col-xs-4<br>Since 9 + 4 = 13 > 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
264 <div class="col-xs-6">.col-xs-6<br>Subsequent columns continue along the new line.</div>
269 <div class="col-xs-9">.col-xs-9</div>
270 <div class="col-xs-4">.col-xs-4<br>Since 9 + 4 = 13 > 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
271 <div class="col-xs-6">.col-xs-6<br>Subsequent columns continue along the new line.</div>
275 <h2 id="grid-responsive-resets">Responsive column resets</h2>
276 <p>With the four tiers of grids 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 <code>.clearfix</code> and our <a href="#responsive-utilities">responsive utility classes</a>.</p>
277 <div class="bs-docs-grid">
278 <div class="row show-grid">
279 <div class="col-xs-6 col-sm-3">
282 Resize your viewport or check it out on your phone for an example.
284 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
286 <!-- Add the extra clearfix for only the required viewport -->
287 <div class="clearfix visible-xs-block"></div>
289 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
290 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
295 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
296 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
298 <!-- Add the extra clearfix for only the required viewport -->
299 <div class="clearfix visible-xs-block"></div>
301 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
302 <div class="col-xs-6 col-sm-3">.col-xs-6 .col-sm-3</div>
305 <p>In addition to column clearing at responsive breakpoints, you may need to <strong>reset offsets, pushes, or pulls</strong>. See this in action in <a href="../examples/grid/">the grid example</a>.</p>
308 <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
309 <div class="col-sm-5 col-sm-offset-2 col-md-6 col-md-offset-0">.col-sm-5 .col-sm-offset-2 .col-md-6 .col-md-offset-0</div>
313 <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
314 <div class="col-sm-6 col-md-5 col-md-offset-2 col-lg-6 col-lg-offset-0">.col-sm-6 .col-md-5 .col-md-offset-2 .col-lg-6 .col-lg-offset-0</div>
319 <h2 id="grid-offsetting">Offsetting columns</h2>
320 <p>Move columns to the right using <code>.col-md-offset-*</code> classes. These classes increase the left margin of a column by <code>*</code> columns. For example, <code>.col-md-offset-4</code> moves <code>.col-md-4</code> over four columns.</p>
321 <div class="bs-docs-grid">
322 <div class="row show-grid">
323 <div class="col-md-4">.col-md-4</div>
324 <div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>
326 <div class="row show-grid">
327 <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
328 <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
330 <div class="row show-grid">
331 <div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>
336 <div class="col-md-4">.col-md-4</div>
337 <div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>
340 <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
341 <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
344 <div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>
349 <h2 id="grid-nesting">Nesting columns</h2>
350 <p>To nest your content with the default grid, add a new <code>.row</code> and set of <code>.col-sm-*</code> columns within an existing <code>.col-sm-*</code> 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).</p>
351 <div class="row show-grid">
352 <div class="col-sm-9">
354 <div class="row show-grid">
355 <div class="col-xs-8 col-sm-6">
356 Level 2: .col-xs-8 .col-sm-6
358 <div class="col-xs-4 col-sm-6">
359 Level 2: .col-xs-4 .col-sm-6
366 <div class="col-sm-9">
369 <div class="col-xs-8 col-sm-6">
370 Level 2: .col-xs-8 .col-sm-6
372 <div class="col-xs-4 col-sm-6">
373 Level 2: .col-xs-4 .col-sm-6
380 <h2 id="grid-column-ordering">Column ordering</h2>
381 <p>Easily change the order of our built-in grid columns with <code>.col-md-push-*</code> and <code>.col-md-pull-*</code> modifier classes.</p>
382 <div class="row show-grid">
383 <div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
384 <div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
389 <div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
390 <div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
394 <h2 id="grid-less">Less mixins and variables</h2>
395 <p>In addition to <a href="#grid-example-basic">prebuilt grid classes</a> for fast layouts, Bootstrap includes Less variables and mixins for quickly generating your own simple, semantic layouts.</p>
398 <p>Variables 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.</p>
401 @grid-gutter-width: 30px;
402 @grid-float-breakpoint: 768px;
406 <p>Mixins are used in conjunction with the grid variables to generate semantic CSS for individual grid columns.</p>
408 // Creates a wrapper for a series of columns
409 .make-row(@gutter: @grid-gutter-width) {
410 // Then clear the floated columns
413 @media (min-width: @screen-sm-min) {
414 margin-left: (@gutter / -2);
415 margin-right: (@gutter / -2);
418 // Negative margin nested rows out to align the content of columns
420 margin-left: (@gutter / -2);
421 margin-right: (@gutter / -2);
425 // Generate the extra small columns
426 .make-xs-column(@columns; @gutter: @grid-gutter-width) {
428 // Prevent columns from collapsing when empty
430 // Inner gutter via padding
431 padding-left: (@gutter / 2);
432 padding-right: (@gutter / 2);
434 // Calculate width based on number of columns available
435 @media (min-width: @grid-float-breakpoint) {
437 width: percentage((@columns / @grid-columns));
441 // Generate the small columns
442 .make-sm-column(@columns; @gutter: @grid-gutter-width) {
444 // Prevent columns from collapsing when empty
446 // Inner gutter via padding
447 padding-left: (@gutter / 2);
448 padding-right: (@gutter / 2);
450 // Calculate width based on number of columns available
451 @media (min-width: @screen-sm-min) {
453 width: percentage((@columns / @grid-columns));
457 // Generate the small column offsets
458 .make-sm-column-offset(@columns) {
459 @media (min-width: @screen-sm-min) {
460 margin-left: percentage((@columns / @grid-columns));
463 .make-sm-column-push(@columns) {
464 @media (min-width: @screen-sm-min) {
465 left: percentage((@columns / @grid-columns));
468 .make-sm-column-pull(@columns) {
469 @media (min-width: @screen-sm-min) {
470 right: percentage((@columns / @grid-columns));
474 // Generate the medium columns
475 .make-md-column(@columns; @gutter: @grid-gutter-width) {
477 // Prevent columns from collapsing when empty
479 // Inner gutter via padding
480 padding-left: (@gutter / 2);
481 padding-right: (@gutter / 2);
483 // Calculate width based on number of columns available
484 @media (min-width: @screen-md-min) {
486 width: percentage((@columns / @grid-columns));
490 // Generate the medium column offsets
491 .make-md-column-offset(@columns) {
492 @media (min-width: @screen-md-min) {
493 margin-left: percentage((@columns / @grid-columns));
496 .make-md-column-push(@columns) {
497 @media (min-width: @screen-md-min) {
498 left: percentage((@columns / @grid-columns));
501 .make-md-column-pull(@columns) {
502 @media (min-width: @screen-md-min) {
503 right: percentage((@columns / @grid-columns));
507 // Generate the large columns
508 .make-lg-column(@columns; @gutter: @grid-gutter-width) {
510 // Prevent columns from collapsing when empty
512 // Inner gutter via padding
513 padding-left: (@gutter / 2);
514 padding-right: (@gutter / 2);
516 // Calculate width based on number of columns available
517 @media (min-width: @screen-lg-min) {
519 width: percentage((@columns / @grid-columns));
523 // Generate the large column offsets
524 .make-lg-column-offset(@columns) {
525 @media (min-width: @screen-lg-min) {
526 margin-left: percentage((@columns / @grid-columns));
529 .make-lg-column-push(@columns) {
530 @media (min-width: @screen-lg-min) {
531 left: percentage((@columns / @grid-columns));
534 .make-lg-column-pull(@columns) {
535 @media (min-width: @screen-lg-min) {
536 right: percentage((@columns / @grid-columns));
541 <h3>Example usage</h3>
542 <p>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.</p>
552 .make-lg-column-offset(1);
556 <div class="wrapper">
557 <div class="content-main">...</div>
558 <div class="content-secondary">...</div>