下面我们收集了一些 Bootstrap 4 网格布局的示例。
使用.col
指定数量的元素上的类,Bootstrap 将识别有多少个元素(并创建等宽的列)。在下面的示例中,我们使用三个 col 元素,每个元素的宽度为 33.33%。
<div class="row">
<div class="col">col</div>
<div class="col">col</div>
<div class="col">col</div>
</div>
亲自试一试 »
您还可以使用数字来控制列宽。只需确保总和等于或小于 12(不需要使用所有 12 个可用列):
<div class="row">
<div class="col-4">col-4</div>
<div class="col-4">col-4</div>
<div class="col-4">col-4</div>
</div>
亲自试一试 »
要创建不相等的列,您必须使用数字。以下示例将创建 25%/50%/25% 分割:
<div class="row">
<div class="col-3">col-3</div>
<div class="col-6">col-6</div>
<div class="col-3">col-3</div>
</div>
亲自试一试 »
但是,仅设置一列的宽度就足够了,并且让同级列在其周围自动调整大小。以下示例将创建 25%/50%/25% 分割:
<div class="row">
<div class="col">col</div>
<div class="col-6">col-6</div>
<div class="col">col</div>
</div>
亲自试一试 »
<!-- Two equal columns -->
<div class="row">
<div class="col">1 of 2</div>
<div class="col">2 of 2</div>
</div>
<!-- Four equal columns -->
<div class="row">
<div class="col">1 of 4</div>
<div class="col">2 of 4</div>
<div class="col">3 of 4</div>
<div class="col">4 of 4</div>
</div>
<!-- Six equal columns -->
<div class="row">
<div class="col">1 of 6</div>
<div class="col">2 of 6</div>
<div class="col">3 of 6</div>
<div class="col">4 of 6</div>
<div class="col">5 of 6</div>
<div class="col">6 of 6</div>
</div>
亲自试一试 »
您还可以控制应出现在彼此旁边的列数(无论有多少列),使用.row-cols-*
课程:
<div class="row row-cols-1">
<div class="col">1 of 2</div>
<div class="col">2 of 2</div>
</div>
<div class="row row-cols-2">
<div class="col">1 of 4</div>
<div class="col">2 of 4</div>
<div class="col">3 of 4</div>
<div class="col">4 of 4</div>
</div>
<div class="row row-cols-3">
<div class="col">1 of 6</div>
<div class="col">2 of 6</div>
<div class="col">3 of 6</div>
<div class="col">4 of 6</div>
<div class="col">5 of 6</div>
<div class="col">6 of 6</div>
</div>
亲自试一试 »
<!-- Two Unequal Columns -->
<div class="row">
<div class="col-8">1 of 2</div>
<div class="col-4">2 of 2</div>
</div>
<!-- Four Unequal Columns -->
<div class="row">
<div class="col-2">1 of 4</div>
<div class="col-2">2 of 4</div>
<div class="col-2">3 of 4</div>
<div class="col-6">4 of 4</div>
</div>
<!-- Setting two column widths -->
<div class="row">
<div class="col-4">1 of 4</div>
<div class="col-6">2 of 4</div>
<div class="col">3 of 4</div>
<div class="col">4 of 4</div>
</div>
亲自试一试 »
如果其中一列比另一列高(由于文本或 CSS 高度),其余列将遵循:
<div class="row">
<div class="col">Lorem ipsum...</div>
<div class="col">col</div>
<div class="col">col</div>
</div>
亲自试一试 »
以下示例显示如何创建两列布局,其中一列内还包含另外两列:
<div class="row">
<div class="col-8">
.col-8
<div class="row">
<div class="col-6">.col-6</div>
<div class="col-6">.col-6</div>
</div>
</div>
<div class="col-4">.col-4</div>
</div>
亲自试一试 »
Bootstrap 4 网格系统有五个类:
.col-
(超小型设备 - 屏幕宽度小于 576 像素).col-sm-
(小型设备 - 屏幕宽度等于或大于 576px).col-md-
(中型设备 - 屏幕宽度等于或大于 768px).col-lg-
(大型设备 - 屏幕宽度等于或大于 992 像素).col-xl-
(超大设备 - 屏幕宽度等于或大于 1200 像素)可以组合上面的类来创建更加动态和灵活的布局。
提示:每个类都会按比例放大,因此如果您希望为sm
和md
,你只需要指定sm
。
以下示例展示了如何创建一个列布局,该布局在超小型设备上开始堆叠,然后在较大设备(sm、md、lg 和 xl)上变为水平:
<div class="row">
<div class="col-sm-9">col-sm-9</div>
<div class="col-sm-3">col-sm-3</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>
亲自试一试 »
<!-- 50%/50% split on extra small devices and 75%/25% split on larger devices -->
<div class="row">
<div class="col-6 col-sm-9">col-6 col-sm-9</div>
<div class="col-6 col-sm-3">col-6 col-sm-3</div>
</div>
<!-- 58%/42% split on extra small, small and medium devices and 66.3%/33.3% split on large and xlarge devices -->
<div class="row">
<div class="col-7 col-lg-8">col-7 col-lg-8</div>
<div class="col-5 col-lg-4">col-5 col-lg-4</div>
</div>
<!--
25%/75% split on small devices, a 50%/50% split on medium devices, and a 33%/66% split on large and xlarge devices. On extra small devices, it will automatically stack (100%) -->
<div class="row">
<div class="col-sm-3 col-md-6 col-lg-4">col-sm-3 col-md-6 col-lg-4</div>
<div class="col-sm-9 col-md-6 col-lg-8">col-sm-9 col-md-6 col-lg-8</div>
</div>
亲自试一试 »
添加.no-gutters
类到.row
用于移除排水沟的容器(额外空间):