Bootstrap 简明教程

Bootstrap - Grid system

本章将讨论 Bootstrap 网格系统。在 Bootstrap 中,网格系统允许横跨页面最多 12 列,并且使用 flexbox 构建

Basic example

  1. Bootstrap 的网格系统是一个自适应布局工具,使用容器、行和列来对齐内容。

为自适应固定宽度容器使用 .container 类。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Grid</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      <div class="container text-center">
        <div class="row mt-2">
          <div class="col p-2 bg-primary">
            First Column
          </div>
          <div class="col p-2 bg-warning">
            Second Column
          </div>
          <div class="col p-2 bg-info">
            Third Column
          </div>
        </div>
      </div>
    </body>
    </html>

How it works

  1. Bootstrap 网格支持 six responsive breakpoints 。断点会影响其上方所有的大小(例如 sm、md、lg、xl、xxl),允许你控制每个断点处的容器和列大小及行为。

  2. 容器会对齐内容并水平填充。为自适应像素宽度使用 .container ,或为所有视口和设备大小使用 .container-fluid 。对于各个断点,会使用自适应容器类。

  3. 行是列的包装器。每列都包含水平 padding 。此 padding 随后应用到具有负边距的行。按照此方式,列中的所有内容都会左对齐。

  4. 行支持用于统一列大小的修改类 column sizing 和用于更改文本间距的边距类。

  5. 列非常灵活。你可以使用每行可用的 12 个模板列中的任意数量的列来创建各种元素组合。

  6. 列宽按百分比设定,以针对父元素实现流式和相对大小。

  7. 边距自适应且可定制。它们适用于所有视口,并且大小与 margin and padding 相同。你可以使用 .gx- (适用于水平边距)、 .gy- (适用于垂直边距)、 .g- (适用于所有边距)和 *.g-0 (用于移除边距)来修改边距。

  8. 要创建更多的语义标记,你可以使用预定义的网格源 Sass 混合。

Bootstrap 网格系统中有六个类:

  1. Extra small (.col-xs)

  2. Small (.col-sm-)

  3. Medium (.col-md-)

  4. Large (.col-lg-)

  5. Extra large (.col-xl-)

  6. Extra extra large (.col-xxl-)

网格在这些断点上的变化如下表所示:

Extra small <576px

Small ≥576px

Medium ≥768px

Large ≥992px

X-Large ≥1200px

XX-Large ≥1400px

Container max-width

None (auto)

540px

720px

960px

1140px

1320px

Class prefix

.col-

.col-sm-

.col-md-

.col-lg-

.col-xl-

.col-xxl-

# of columns

12

-

-

-

-

-

Gutter width

1.5rem(两侧均有 .75rem 边距。)

-

-

-

-

-

Custom gutters

Yes

-

-

-

-

-

Nestable

Yes

-

-

-

-

-

Column ordering

Yes

-

-

-

-

-

Auto-layout columns

要轻松调整列大小而不使用显示编号的类,请使用特定于断点的列类,例如 .col-sm-6

Equal-width

  1. 使用相等宽度的网格系统来创建大小相等的网格。

  2. 两个网格布局适用于从 xsxxl 的任何设备大小。对于每个断点,你可以添加很多无单位类,并且每列/行都将具有相同的宽度。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Grid</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      <div class="container text-center">
        <h5>Equal columns</h5>
        <div class="row mt-2">
          <div class="col  p-2 bg-primary">
            First Column
          </div>
          <div class="col  p-2 bg-info">
            Second Column
          </div>
        </div>
        <h5>Equal rows</h5>
        <div class="col mt-2">
          <div class="row  p-2 bg-warning text-white">
            First Row
          </div>
          <div class="row  p-2 bg-secondary text-white">
            Second Row
          </div>
          <div class="row  p-2 bg-success text-white">
            Third Row
          </div>
        </div>
      </div>
    </body>
    </html>

Setting one column width

  1. 你可以选择一列宽度,而其他列会自动调整它的大小。

  2. 使用网格类、混合类型或内联宽度。无论中心列的宽度如何,都可以调整其他列的大小。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Grid</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      <div class="container text-center">
        <div class="row mt-2">
          <div class="col-6  p-2 bg-primary text-white">
            First Column (col-6)
          </div>
          <div class="col-3  p-2 bg-secondary text-white">
           Second Column (col-3)
          </div>
          <div class="col p-2 bg-warning text-white">
            Third Column (col)
          </div>
        </div>
      </div>
    </body>
    </html>

Variable width content

使用 col-{breakpoint}-auto 类根据内容的自然宽度调整列的大小。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap - Grid</title>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
      <div class="container text-center">
        <div class="row justify-content-md-center mt-2">
          <div class="col col-lg-2  p-2 bg-primary ">
           First Column
          </div>
          <div class="col-md-auto  p-2 bg-info ">
            Second column with variable width content.
          </div>
          <div class="col col-lg-2  p-2 bg-warning">
           Third Column
          </div>
        </div>
      </div>
    </body>
    </html>

Responsive classes

在 Bootstrap 中,网格具有六层预定义类,用于创建复杂的响应式布局。你可以将列/行自定义为小、中、大或超大型设备。

All breakpoints

对所有大小的设备都一致的网格使用 .col.col- * 类。如果你需要特定大小的列,请使用编号类。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <h5>Columns</h5>
      <div class="row mt-2">
        <div class="col p-2 bg-primary">First Column</div>
        <div class="col p-2 bg-warning">Second Column</div>
        <div class="col p-2 bg-light">Third Column</div>
      </div>
      <h5>Rows</h5>
      <div class="col mt-2">
        <div class="row-9 p-2 bg-info">First Row</div>
        <div class="row-3 p-2 bg-success">Second Row</div>
      </div>
    </div>
  </body>
  </html>

Stacked to horizontal

使用 .col-sm- * 类构建一个简单的网格系统,该系统在超小和小的设备上堆叠,并在较大的设备上变成水平。

*注意:*调整浏览器的尺寸以检查对不同设备的影响。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <h5>Columns</h5>
      <div class="row mt-2">
        <div class="col-sm-6 p-2 bg-primary text-white">First Column</div>
        <div class="col-sm-3 p-2 bg-success text-white">Second Column</div>
        <div class="col-sm-2 p-2 bg-dark text-white">Third Column</div>
      </div>
      <h5>Rows</h5>
      <div class="col mt-2">
        <div class="row-sm p-2 bg-info">First row</div>
        <div class="row-sm p-2 bg-warning">Second row</div>
      </div>
    </div>
  </body>
  </html>

Mix and match

将不同层级的各种类相互组合,可以根据需要轻松地将某些网格层级中的列堆叠在一起。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <div class="row mt-2">
        <div class="col-sm-4 p-2 bg-primary text-white">First column of col-sm-4</div>
        <div class="col-3  p-2 bg-secondary text-white">Second column of col-3</div>
      </div>
      <div class="row mt-2">
        <div class="col-4 col-md-2 p-2 bg-warning text-white">First column of col-4 col-md-2</div>
        <div class="col-4 col-md-2 p-2 bg-secondary text-white">Second column of col-4 col-md-2</div>
        <div class="col-4 col-md-2 p-2 bg-info text-white">Third column of col-4 col-md-2</div>
      </div>
      <div class="row mt-2">
        <div class="col-sm-3 p-2 bg-dark text-white">First column of col-sm-3</div>
        <div class="col-md-6 p-2 bg-success text-white">Second column of col-md-6</div>
      </div>
    </div>
  </body>
  </html>

Row columns

  1. Bootstrap 网格提供了行和列类,可以创建简单的网格布局。

  2. 单个行包含在 .row * 类中。

  3. 单个列包含在 .col * 类中。

  4. 使用 .row-cols- * 类设置一行中的列数。

  5. 使用 .row-cols-auto 根据列的内容调整列的大小。

===

使用 .row-cols-2 类创建两列。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <div class="row row-cols-2">
        <div class="col bg-info p-2">First Row First Column</div>
        <div class="col bg-primary p-2">First Row Second Column</div>
        <div class="col bg-warning p-2">Second Row First Column</div>
      </div>
    </div>
  </body>
  </html>

使用 .row-cols-3 类创建三列。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container  text-center mt-2">
      <div class="row row-cols-3 mt-2">
        <div class="col bg-info p-2">First Row First Column</div>
        <div class="col bg-light p-2">First Row Second Column</div>
        <div class="col bg-primary p-2">First Row Third Column</div>
        <div class="col bg-secondary p-2">Second Row First Column</div>
        <div class="col bg-success p-2">Second Row Second Column</div>
        <div class="col bg-warning p-2">Second Row Third Column</div>
      </div>
    </div>
  </body>
  </html>

使用 row-cols-auto 类创建行和列的网格。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <div class="row row-cols-auto">
        <div class="col bg-primary p-2">First Row First Column</div>
        <div class="col bg-secondary p-2">First Row Second Column</div>
        <div class="col bg-warning p-2">First Row Third Column</div>
        <div class="col bg-info p-2">First Row Fourth Column</div>
        <div class="col bg-success p-2">First Row Fifth Column</div>
        <div class="col bg-light p-2">First Row sixth Column</div>
        <div class="col bg-danger p-2">Second Row First Column</div>
      </div>
    </div>
  </body>
  </html>

使用 row-cols-4 类创建四列。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <div class="row row-cols-4">
        <div class="col bg-primary p-2">First Row First Column</div>
        <div class="col bg-secondary p-2">First Row Second Column</div>
        <div class="col bg-warning p-2">First Row Third Column</div>
        <div class="col bg-info p-2">First Row Fourth Column</div>
        <div class="col bg-light p-2">Second Row First Column</div>
        <div class="col bg-danger p-2">Second Row Second Column</div>
      </div>
    </div>
  </body>
  </html>

使用 row-cols-1row-cols-sm-3 类和 row-cols-md-6 类。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <div class="row row-cols-1 row-cols-sm-3 row-cols-md-6 mt-2">
        <div class="col bg-primary p-2">First Row First Column</div>
        <div class="col bg-light p-2">First Row Second Column</div>
        <div class="col bg-warning p-2">First Row Third Column</div>
        <div class="col bg-info p-2">First Row Fourth Column</div>
        <div class="col bg-danger p-2">First Row Fifth Column</div>
      </div>
    </div>
  </body>
  </html>

使用 row-cols() Sass 混入。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    <style>
      .custom_grid{
        @media row-cols(2);
        @media media-breakpoint-up(lg) {
          @media row-cols(6);
        }
      }
    </style>
  </head>
  <body>
    <div class="container custom_grid text-center">
      <div class="row">
        <div class="col bg-primary p-2">First Row First Column</div>
        <div class="col bg-light p-2">First Row Second Column</div>
        <div class="col bg-warning p-2">First Row Third Column</div>
        <div class="col bg-info p-2">First Row Fourth Column</div>
        <div class="col bg-danger p-2">First Row Fifth Column</div>
      </div>
    </div>
  </body>
  </html>

Nesting

嵌套网格系统在现有网格的单格中添加网格的行和列。嵌套行应由一组列组成,其总数不得超过 12(没有必要使用全部 12 列)。

Example

您可以使用*编辑和运行*选项编辑并尝试运行此代码。

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap - Grid</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container text-center">
      <div class="row mt-2">
        <div class="col-sm bg-primary p-2">
          First Column
        </div>
        <div class="col-sm bg-info p-2">
          <div class="container">
            <div class="row">
              <div class="col col-sm bg-light">
                Second Column
              </div>
              <div class="col col-sm bg-light">
                Second Column
              </div>
            </div>
          </div>
        </div>
        <div class="col-sm bg-primary p-2">
          Third Column
        </div>
      </div>
    </div>
  </body>
  </html>