Bootstrap 4 - 网格系统
描述
Bootstrap 4 网格系统使用Flexbox构建,完全响应式,通过在页面上创建行和列的布局,可扩展到 12 列(根据设备的大小)。它提供响应式、移动优先的流体网格系统,可随着设备或视口尺寸的增加而缩放列。
网格系统的工作
行必须放置在.container类中,以便正确对齐和填充。
对于响应式宽度,请使用.container类;对于所有视口的固定宽度,请使用.container-fluid类。
使用行创建水平列组。
内容应放置在列内,并且只有列可以是行的直接子级。
列包含用于控制列之间空间的填充。
如果一行中放置的列超过 12 列,则这些列将放置在新行中。
列通过填充在列内容之间创建间隙。因此,您可以使用行上的.no-gutters类删除行中的边距和列中的填充。
您可以通过使用五个网格断点(例如超小、小、中、大和特大)使网格系统具有响应能力。
预定义的网格类(如 .col-4)可用于快速制作网格布局。LESS mixin 也可用于更多语义布局。
网格选项
下表总结了 Bootstrap 4 网格系统如何跨多个设备工作的各个方面 -
超小型设备 (<576px) | 小型设备(≥576px) | 中型设备(≥768px) | 大型设备(≥992px) | 超大设备(≥1200px) | |
---|---|---|---|---|---|
网格Behave | 始终水平 | 折叠开始,水平位于断点上方 | 折叠开始,水平位于断点上方 | 折叠开始,水平位于断点上方 | 折叠开始,水平位于断点上方 |
最大集装箱宽度 | 无(自动) | 540像素 | 720像素 | 960像素 | 1140像素 |
班级班级 | .col- | .col-sm- | .col-md- | .col-lg- | .col-xl- |
列数 | 12 | 12 | 12 | 12 | 12 |
天沟宽度 |
30像素 (列每边 15 像素) |
30像素 (列每边 15 像素) |
30像素 (列每边 15 像素) |
30像素 (列每边 15 像素) |
30像素 (列每边 15 像素) |
可嵌套 | 是的 | 是的 | 是的 | 是的 | 是的 |
列排序 | 是的 | 是的 | 是的 | 是的 | 是的 |
基本网格结构
以下是 Bootstrap 4 网格的基本结构 -
<div class = "container"> <div class = "row"> <div class = "col-*-*"></div> <div class = "col-*-*"></div> </div> <div class = "row"> <div class = "col-*-*"></div> <div class = "col-*-*"></div> <div class = "col-*-*"></div> </div> <div class = "row">...</div> </div>
网格系统示例
以下是 Bootstrap 4 网格系统的示例 -
例子
<html lang = "en"> <head> <!-- Meta tags --> <meta charset = "utf-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no"> <!-- Bootstrap CSS --> <link rel = "stylesheet" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin = "anonymous"> <title>Bootstrap 4 Example</title> </style> .grid_system div[class^="col"] { border: 1px solid white; background: #e4dcdc; text-align: center; padding-top: 5px; padding-bottom: 5px } </style> </head> <body> <div class = "grid_system"> <div class = "row"> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> <div class = "col-sm-1">.col-sm-1</div> </div> <div class = "row"> <div class = "col-sm-3">.col-sm-3</div> <div class = "col-sm-3">.col-sm-3</div> <div class = "col-sm-3">.col-sm-3</div> <div class = "col-sm-3">.col-sm-3</div> </div> <div class = "row"> <div class = "col-sm-4">.col-sm-4</div> <div class = "col-sm-4">.col-sm-4</div> <div class = "col-sm-4">.col-sm-4</div> </div> <div class = "row"> <div class =" col-sm-3">.col-sm-3</div> <div class = "col-sm-3">.col-sm-3</div> <div class = "col-sm-6">.col-sm-6</div> </div> <div class = "row"> <div class = "col-sm-5">.col-sm-5</div> <div class = "col-sm-7">.col-sm-7</div> </div> <div class = "row"> <div class = "col-sm-6">.col-sm-6</div> <div class = "col-sm-6">.col-sm-6</div> </div> <div class = "row"> <div class = "col-sm-12">.col-sm-12</div> </div> </div> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin = "anonymous"> </script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin = "anonymous"> </script> <script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin = "anonymous"> </script> </body> </html>
它将产生以下结果 -
输出
创建两列布局
以下示例描述了为小型、中型和大型设备创建两个列布局。在手机等小型设备上,默认情况下,列将自动变为水平。
例子
<html lang = "en"> <head> <!-- Meta tags --> <meta charset = "utf-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no"> <!-- Bootstrap CSS --> <link rel = "stylesheet" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin = "anonymous"> <title>Bootstrap 4 Example</title> </head> <body> <div class = "grid_system"> <div class = "row"> <div class = "col-sm-6">.col-sm-6</div> <div class = "col-sm-6">.col-sm-6</div> </div> <div class = "row"> <div class = "col-sm-7">.col-sm-7</div> <div class = "col-sm-5">.col-sm-5</div> </div> <div class = "row"> <div class = "col-sm-4">.col-sm-4</div> <div class = "col-sm-8">.col-sm-8</div> </div> <div class = "row"> <div class = "col-sm-9">.col-sm-9</div> <div class = "col-sm-3">.col-sm-3</div> </div> </div> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin = "anonymous"> </script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin = "anonymous"> </script> <script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin = "anonymous"> </script> </body> </html>
它将产生以下结果 -
输出
创建三列布局
以下示例描述了为中型和大型设备创建三列布局。如果屏幕分辨率大于或等于 992 像素,那么它将在平板电脑中以横向模式显示,并且像往常一样,它将以纵向模式显示。
例子
<html lang = "en"> <head> <!-- Meta tags --> <meta charset = "utf-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no"> <!-- Bootstrap CSS --> <link rel = " stylesheet" href = " https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin = "anonymous"> <title>Bootstrap 4 Example</title> </head> <body> <div class = "grid_system"> <div class = "row"> <div class = "col-sm-4">.col-sm-4</div> <div class = "col-sm-4">.col-sm-4</div> <div class = "col-sm-4">.col-sm-4</div> </div> <div class = "row"> <div class = "col-sm-3">.col-sm-3</div> <div class = "col-sm-4">.col-sm-4</div> <div class = "col-sm-5">.col-sm-5</div> </div> <div class = "row"> <div class = "col-sm-2">.col-sm-2</div> <div class = "col-sm-8">.col-sm-8</div> <div class = "col-sm-2">.col-sm-2</div> </div> <div class = "row"> <div class = "col-sm-2">.col-sm-2</div> <div class = "col-sm-3">.col-sm-3</div> <div class = "col-sm-7">.col-sm-7</div> </div> </div> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin = "anonymous"> </script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin = "anonymous"> </script> <script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin = "anonymous"> </script> </body> </html>
它将产生以下结果 -