Layout (1): Overview / Grid System / Utilities for layout
.container
or a fluid container .container-fluid
is used for a basic layout element.
div.container
⇒ Responsive: width is set to one of the fixed steps (breakpoints), center-aligneddiv.container-fluid
⇒ Fits to the parent (100%)A width of a container .container
is set within the breakpoints as below (see Grid options table for details)
Name | Viewport width | Width of .container |
---|---|---|
(xs) | <576px | Browser default (auto) |
sm | ≧576px | 540px |
md | ≧768px | 720px |
lg | ≧992px | 960px |
xl | ≧1200px | 1140px |
Bootstrap uses class names with breakpoint (sm|md|lg|xl)
for responsive settings (like .col-md
).
.col
): Sets as default (least prior).col-md
): Sets for a specific breakpoint or greater (prior to default)-xs
class-fix (same as setting as default). Using breakpoint priorities, you can switch layouts, or show/hide some contents with specific viewport range.Some Bootstrap components reserve z-indexes (≥1000
) for specific purposes. 1,2,3
are also used for visual customization for element states (hover, active, and focus) (set to internally-created sibling elements for presentation).
Z-index | Usage |
---|---|
1,2,3 | Internally used for element states (hover, active, focus) |
1000 | Dropdown |
1020 | Sticky top (Navbar) |
1030 | Fixed top / Fixed bottom (Navbar) |
1040 | Backdrop (Modal background) |
1050 | Modal |
1060 | Popover |
1070 | Tooltip |
The grid system has the following three layer structure.
div.container
(or div.container-fluid
)
div.row
(Flexbox available inside with display: flex
)
div.col({-breakpoint}{-width})
On layouts with div.container
, paddings called gutters are set to left and right sides (15px
each). Actual widths for inner elements are set to 30px
smaller than breakpoint widths of div.container
s. Gutters are used for horizontal (inner) paddings for multi-row layouts.
This page also uses containers and has gutters on both sides. The example bellow shows how gutters are set within the layout. The container has the gray border. The inner elements have the light gray background. The areas on left and right without background color are gutters.
div.container
(with gray border)
padding-(left|right): 15px
$grid-gutter-width
. You can change it by customizing Bootstrap styles from Sass. See Customizing from Sass source for detail.On Bootstrap grid system, inside containers are rows div.row
(children) and columns div.col
(grandchildren). But gutters are specially treated differently in each level. The example below shows gutter treatments for all levels. Each level is bordered with a different color. But three borders are overlapped because their sizes are the same as the result.
div.container
(with gray border)
padding-(left|right): 15px
div.row
(with red border)
margin-(left|right) : -15px
padding-(left|right): 0
div.col
(with blue border)
margin-(left|right): 0
padding-(left|right): 15px
Setting multiple columns div.col
inside a row div.row
arranges columns horizontally with the same width. A maximum number of columns in a row is determined by a browser from widths of a row and colomns. See examples below (up to 20 columns).
.border
) to columns div.col*
to indicate boundaries. Areas of inner contents also set background colors (.bg-light
) to indicate gutters.The following structure is used (containers div.container
are omitted from here).
div.row
div.col
(× number of columns: 1..16)
{index}/{number of columns}
For equal-width layouts using div.col
, use an empty div.w-100
to insert a break to a new line.
.row
.row
⇒ .w-100
.row
.row
.row
⇒ .w-100
.row
.row
.row
.row
div.row
div.col
(×2)div.w-100
(breaks to a new line)div.col
(×3)div.w-100
(breaks to a new line)div.col
(×4)Width of a grid column can be set from 1/12 (approx. 8.3%) to 12/12 (100%).
div.col-(1|2|3|4|5|6|7|8|9|10|11|12)
.col-1
.col-2
.col-3
.col-4
.col-5
.col-6
.col-7
.col-8
.col-9
.col-10
.col-11
.col-12
.col
1/6(equal).col-6
6/12(set).col
1/6(equal).col
1/6(equal).col
7/36(equal).col
7/36(equal).col-5
5/12(set).col
7/36(equal)div.row
div.col
div.col-6
div.col
(×2)div.row
div.col
(×2)div.col-5
div.col
.col-auto
sets an element width to browser default (by CSS width: auto;
). A width of a element is automatically calculated by a browser from its inner content.
.col-auto
123
.col-auto
1234567890
.col-auto
12345678901234567890
Setting 12 or more columns in total automatically breaks to a next line.
.col-9
.col-4
.col-7
.col-3
div.row
div.col-9
div.col-4
breaks by being greater than 12div.col-7
div.col-3
breaks by being greater than 12On setting each width to columns, div.w-100
also can be used to insert a break to a new line.
.col-3
.col-5
⇒ .w-100
.col-4
div.row
div.col-3
div.col-5
div.w-100
(inserts a break)div.col-4
.col-(sm|md|lg|xl)(-1|-2|-3|...|-12|-auto)?
.col
(always two columns)
.col
.col
.col-sm
(two columns on width≧576px, two rows on width<576px)
.col-sm
.col-sm
.col-md
(two columns on width≧768px, two rows on width<768px)
.col-md
.col-md
.col-lg
(two columns on width≧992px, two rows on width<992px)
.col-lg
.col-lg
.col-xl
(two columns on width≧1200px, two rows on width<1200px)
.col-xl
.col-xl
div.row
div.col(-sm|-md|-lg|-xl)?
(×2).col-3
.col-6
.col-sm-3
.col-sm-6
.col-md-3
.col-md-6
.col-lg-3
.col-lg-6
.col-xl-3
.col-xl-6
div.row
div.col(-sm|-md|-lg|-xl)?-3
div.col(-sm|-md|-lg|-xl)?-6
.col-auto
12345
.col-auto
12345678901234567890
.col-sm-auto
12345
.col-sm-auto
12345678901234567890
.col-md-auto
12345
.col-md-auto
12345678901234567890
.col-lg-auto
12345
.col-lg-auto
12345678901234567890
.col-xl-auto
12345
.col-xl-auto
12345678901234567890
div.row
div.col(-sm|-md|-lg|-xl)?-auto
(×2)
The next example demonstrates switching between stacked (as mobile) and horizontal (as PC) layouts on the md breakpoint (768px) as follows.
Live example below (change the browser window width to switch).
.col-md-8
.col-md-4
.col-md
.col-md
.col-md
div.row
div.col-md-8
div.col-md-4
div.row
div.col-md
(×3)Another example of layout switching on the md breakpoint.
Live example below
.col-12.col-md-8
.col-6.col-md-4
.col-6.col-md-4
.col-6.col-md-4
.col-6.col-md-4
.col-6
.col-6
A complex example using auto-width, flexbox, and responsive behaviors (from the official document).
.justify-content-md-center
(⇒ Flex) to rows div.row
(aligns inner elements center on ≥md).col-md-auto
to the central row of each column (sets browser default width on ≥md).col
to the lower-left column (equal or available maximum width).col.col-lg-2
to all other columns (equal width as default, 2/12 on ≥md)Live example below (central rows .col-md-auto
are shown with orange background)
.col.col-lg-2
.col-md-auto
.col.col-lg-2
.col
.col-md-auto
.col.col-lg-2
Responsive behaviors (three cases)
Use Flex utilities.
In examples below, red border is added and height is fixed to 5rem
for each row.
.row.align-items-start
.col
(1/3)
.col
(2/3)
.col
(3/3)
.row.align-items-center
.col
(1/3)
.col
(2/3)
.col
(3/3)
.row.align-items-end
.col
(1/3)
.col
(2/3)
.col
(3/3)
.col.align-self-(start|center|end)
-start
-center
-end
.row.justify-content-start
.col-2
(1/3).col-2
(2/3).col-2
(3/3).row.justify-content-center
.col-2
(1/3).col-2
(2/3).col-2
(3/3).row.justify-content-end
.col-2
(1/3).col-2
(2/3).col-2
(3/3).row.justify-content-around
.col-2
(1/3).col-2
(2/3).col-2
(3/3).row.justify-content-between
.col-2
(1/3).col-2
(2/3).col-2
(3/3)Adding .no-gutters
to div.row
removes gutters from a row.
div.row
.col-9
.col-3
div.row.no-gutters
.col-9
.col-3
Actual internal behavior is more complex (not just removes gutters: Also see Gutters for details).
div.row.no-gutters
-15px
to 0
) ⇒ margin-(left|right): 0
>
)
.no-gutters > .col* { padding-(left|right): 0 }
The next example is based on the official document.
div.row
.col-12.col-sm-6.col-md-8
.col-6.col-md-4
div.row.no-gutters
.col-12.col-sm-6.col-md-8
.col-6.col-md-4
It is also an example of responsive behaviors (three cases).
You can change columns' order with adding .order-(1|2|3|...|12)
(⇒ Flex utilities ).
.order-12
.order-1
.order-7
div.row
div.col.order-12
div.col.order-1
div.col
div.col.order-7
Use .order-first
to set at first, .order-last
to set at last.
.order-last
.order-first
div.row
div.col
div.col.order-last
div.col.order-first
.offset-(1|2|3|...)
sets offset spacing on the left as the same width as .col-(1|2|3...)
.col-4
.offset-4.col-4
div.row
div.col-4
div.offset-4.col-4
.offset-3.col-3
.offset-3.col-3
div.row
div.offset-3.col-3
div.offset-3.col-3
.offset-3.col-6
div.row
div.offset-3.col-6
Responsive offset: .offset-(xl|lg|md|sm)-(1|2|3|...)
An example below is based on the official document (difficult).
.col-sm-5
.col-md-6
.offset-sm-2.col-sm-5
.offset-md-0.col-md-6
.col-sm-6
.col-md-5
.col-lg-6
.col-sm-6
.offset-md-2.col-md-5
.offset-lg-0.col-lg-6
Overall conditions
xs | sm | md | lg,xl | xs | sm | md | lg,xl | ||
---|---|---|---|---|---|---|---|---|---|
1st row, left (1L) |
None (12/12) |
5/12 | 6/12 | 1st row, right (1R) |
None (12/12) |
Offset 2 and 5/12 |
Offset 0 [Note] and 6/12 |
||
2nd row, left (2L) |
None (12/12) |
6/12 | 5/12 | 6/12 | 2nd row, right (2R) |
None (12/12) |
6/12 | Offset 2 and 5/12 |
Offset 0 [Note] and 6/12 |
Resposive behaviors (four cases)
An example below (from the official document) shows how to use spacing utilities for responsive grid layouts (advanced).
.col-md-4
.col-md-4.ml-auto
.col-md-3.ml-md-auto
.col-md-3.ml-md-auto
.col-auto.mr-auto
.col-auto
Overall conditions
< md | ≧ md | < md | ≧ md | ||
---|---|---|---|---|---|
1st row, left (1L) |
None (12/12) |
4/12 | 1st row, right (1R) |
None (12/12) |
4/12 Left margin auto |
2nd row, right (2L) |
None (12/12) |
3/12 Left margin auto |
2nd row, right (2R) |
None (12/12) |
3/12 Left margin auto |
3rd low, left (3L) |
Width auto Right margin auto |
3rd row, right (3R) |
Width auto |
Responsive behaviors
You can nest grid row/column structures with inserting div.row(-*)
's to div.col(-*)
's (in any level but practically once). It is typically required to make eight (any number that does not divide 12) column layouts with mutiple span(s). See the example below.
div.row
div col-6
(Top-left half)
div.row
(nests and re-divides inner)
div.col-3
(×4)
div.bg-light
(1|2|3|4)
div.row
div.col-6.offset-6
(Bottom-right half)
div.row
(nests and re-divides inner)
div.col-3
(×4)
div.bg-light
(5|6|7|8)
The example below shows structural details of the above.
div.row
div.row