XSmall | Small | Medium | Large | Extra Large | XXL | |
---|---|---|---|---|---|---|
Class prefix | .col- |
.col-sm- |
.col-md- |
.col-lg- |
.col-xl- |
.col-xxl- |
Screen width | <576px | >=576px | >=768px | >=992px | >=1200px | >=1400px |
在上一章中,我们介绍了一个带有小型设备类的网格示例。我们使用了两个 div(列),并给它们分配了 25%/75% 的比例:
<div class="col-sm-3">....</div>
<div class="col-sm-9">....</div>
但在中型设备上,50%/50% 分割的设计可能会更好。
中型设备被定义为具有屏幕宽度 从768像素到991像素。
对于中型设备,我们将使用 .col-md-*
类:
<div class="col-sm-3 col-md-6">....</div>
<div class="col-sm-9 col-md-6">....</div>
现在 Bootstrap 会说“在小尺寸下,查看带有 -sm- 的类并使用它们。在中等尺寸下,查看带有 -md- 的类” > 在其中并使用它们”。
以下示例将导致在小型设备上进行 25%/75% 的拆分,在中型(以及大型、xlarge 和 xxlarge)设备上进行 50%/50% 的拆分。在超小型设备上,它将自动堆叠(100%):
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-6">
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-9 col-md-6">
<p>Sed ut perspiciatis...</p>
</div>
</div>
</div>
自己尝试一下 →
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container-fluid mt-3">
<h1>Medium Grid</h1>
<p>The following example will result in a 25%/75% split on small devices and a 50%/50% split on medium (and large, xlarge, xxlarge) devices. On extra small devices, it will automatically stack (100%).</p>
<p>Resize the browser window to see the effect.</p>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-6 bg-primary text-white">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br>
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<div class="col-sm-9 col-md-6 bg-dark text-white">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
</div>
</div>
</div>
</div>
</body>
</html>
注意:确保总和等于或小于 12(不需要使用所有 12 个可用列):
在下面的示例中,我们仅指定 .col-md-6
类(没有 .col-sm-*
)。这意味着中型、大型、超大型和 XXL 设备将分成 50%/50% - 因为类别会扩大。但是,对于小型和超小型设备,它将垂直堆叠(100% 宽度):
<div class="row">
<div class="col-md-6">
<p>Lorem ipsum...</p>
</div>
<div class="col-md-6">
<p>Sed ut perspiciatis...</p>
</div>
</div>
自己尝试一下→
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container-fluid mt-3">
<h1>Medium Grid</h1>
<p>The following example will result in a 50%/50% split on medium, large, xlarge and xxlarge devices (<strong>768px and above</strong>). On small (and extra small) devices, it will automatically stack (100%).</p>
<p>Resize the browser window to see the effect.</p>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 bg-primary text-white">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br>
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<div class="col-md-6 bg-dark text-white">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
</div>
</div>
</div>
</div>
</body>
</html>
在 Bootstrap 5 中,有一种简单的方法可以为所有设备创建等宽的列:只需从 .col-md-*
中删除数字并仅使用 col 元素上的 ="w3-codespan">.col-md
类。 Bootstrap 将识别有多少列,并且每列将获得相同的宽度。
如果屏幕尺寸小于 768px,列将水平堆叠:
<!-- Two columns: 50% width on medium and up-->
<div class="row">
<div class="col-md">1 of 2</div>
<div class="col-md">2 of 2</div>
</div>
<!-- Four columns: 25% width on medium and up -->
<div class="row">
<div class="col-md">1 of 4</div>
<div class="col-md">2 of 4</div>
<div class="col-md">3 of 4</div>
<div class="col-md">4 of 4</div>
</div>
自己尝试一下 →
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container-fluid mt-3">
<h1>Responsive Auto Layout Columns</h1>
<p>In Bootstrap 5, there is an easy way to create equal width columns: just use the <code>.col-md</code> class on a specified number of col elements. Bootstrap will recognize how many columns there are, and each column will get the same width.</p>
<p>If the screen size is <strong>less than 768px</strong>, the columns will stack horizontally.</p>
<div class="container-fluid">
<div class="row">
<div class="col-md bg-primary text-white">1 of 2</div>
<div class="col-md bg-dark text-white">2 of 2</div>
</div>
</div>
<br>
<div class="container-fluid">
<div class="row">
<div class="col-md bg-primary text-white">1 of 4</div>
<div class="col-md bg-dark text-white">2 of 4</div>
<div class="col-md bg-primary text-white">3 of 4</div>
<div class="col-md bg-dark text-white">4 of 4</div>
</div>
</div>
</div>
</body>
</html>
下一章介绍如何为大型设备添加不同的分割百分比。