CSS 栅格(CSS Grid)
通过示例和代码片段了解如何启用、使用和自定义基于 CSS 网格的备用布局系统。
Bootstrap 的默认网格系统是十多年来 CSS 布局技术的结晶,经过了数百万人的尝试和测试。但是,Bootstrap 在创建之初并没有采用我们在浏览器中看到的许多现代 CSS 功能和技术,例如新的 CSS 网格。
如何工作
注意–我们的 CSS 网格系统从 v5.1.0 版起是试验性的,需要您自己选择! 我们在文档的 CSS 中包含了该系统,以便为您演示,但默认情况下是禁用的。请继续阅读,了解如何在您的项目中启用它。
-
CSS 网格是选择性的 通过设置
$enable-grid-classes:false
禁用默认网格系统,并通过设置$enable-cssgrid:true
启用 CSS 网格。然后,重新编译 Sass。 -
用
.grid
替换.row
实例。.grid
类设置了display: grid
并创建了一个grid-template
,你可以在此基础上创建 HTML。 -
将
.col-*
类替换为.g-col-*
类 这是因为 CSS 网格列使用的是grid-column
属性,而不是width
属性。 -
通过 CSS 变量设置列和间隙的大小。 在父级
.grid
中设置这些变量,然后使用--bs-columns
和--bs-gap
在内联或样式表中进行自定义。
未来,Bootstrap 很可能会转向混合解决方案,因为 gap
属性几乎实现了浏览器对 flexbox 的全面支持。
主要区别
与默认网格系统相比:
-
Flex 实用程序对 CSS 网格列的影响并不相同。
-
缝隙(gaps)取代间隙(gutters) 。
gap
属性取代了默认网格系统中的水平padding
,其功能更类似于margin
。 -
因此,与
.row
不同,.grid
没有负边距,也不能使用边距实用程序来更改网格间隙。默认情况下,会在水平和垂直方向应用网格间隙。更多详情,请参阅自定义部分。 -
内联和自定义样式应被视为修改器类的替代(例如,
style="--bs-columns: 3;"
与class="row-cols-3"
)。 -
嵌套的工作原理与此类似,但可能需要在每个嵌套的
.grid
实例上重置列数。详情请参见嵌套部分。
示例
三列
通过使用 .g-col-4
类,可以在所有视口和设备上创建三列等宽的列。添加 responsive classes 可根据视口大小改变布局。
<div class="grid text-center">
<div class="g-col-4">.g-col-4</div>
<div class="g-col-4">.g-col-4</div>
<div class="g-col-4">.g-col-4</div>
</div>
Responsive
Use responsive classes to adjust your layout across viewports. Here we start with two columns on the narrowest viewports, and then grow to three columns on medium viewports and above.
<div class="grid text-center">
<div class="g-col-6 g-col-md-4">.g-col-6 .g-col-md-4</div>
<div class="g-col-6 g-col-md-4">.g-col-6 .g-col-md-4</div>
<div class="g-col-6 g-col-md-4">.g-col-6 .g-col-md-4</div>
</div>
Compare that to this two column layout at all viewports.
<div class="grid text-center">
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
</div>
Wrapping
Grid items automatically wrap to the next line when there’s no more room horizontally. Note that the gap
applies to horizontal and vertical gaps between grid items.
<div class="grid text-center">
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
</div>
Starts
Start classes aim to replace our default grid’s offset classes, but they’re not entirely the same. CSS Grid creates a grid template through styles that tell browsers to “start at this column” and “end at this column.” Those properties are grid-column-start
and grid-column-end
. Start classes are shorthand for the former. Pair them with the column classes to size and align your columns however you need. Start classes begin at 1
as 0
is an invalid value for these properties.
<div class="grid text-center">
<div class="g-col-3 g-start-2">.g-col-3 .g-start-2</div>
<div class="g-col-4 g-start-6">.g-col-4 .g-start-6</div>
</div>
Auto columns
When there are no classes on the grid items (the immediate children of a .grid
), each grid item will automatically be sized to one column.
<div class="grid text-center">
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
</div>
This behavior can be mixed with grid column classes.
<div class="grid text-center">
<div class="g-col-6">.g-col-6</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
</div>
Nesting
Similar to our default grid system, our CSS Grid allows for easy nesting of .grid
s. However, unlike the default, this grid inherits changes in the rows, columns, and gaps. Consider the example below:
- We override the default number of columns with a local CSS variable:
--bs-columns: 3
. - In the first auto-column, the column count is inherited and each column is one-third of the available width.
- In the second auto-column, we’ve reset the column count on the nested
.grid
to 12 (our default). - The third auto-column has no nested content.
In practice this allows for more complex and custom layouts when compared to our default grid system.
<div class="grid text-center" style="--bs-columns: 3;">
<div>
First auto-column
<div class="grid">
<div>Auto-column</div>
<div>Auto-column</div>
</div>
</div>
<div>
Second auto-column
<div class="grid" style="--bs-columns: 12;">
<div class="g-col-6">6 of 12</div>
<div class="g-col-4">4 of 12</div>
<div class="g-col-2">2 of 12</div>
</div>
</div>
<div>Third auto-column</div>
</div>
Customizing
Customize the number of columns, the number of rows, and the width of the gaps with local CSS variables.
Variable | Fallback value | Description |
---|---|---|
--bs-rows |
1 |
The number of rows in your grid template |
--bs-columns |
12 |
The number of columns in your grid template |
--bs-gap |
1.5rem |
The size of the gap between columns (vertical and horizontal) |
These CSS variables have no default value; instead, they apply fallback values that are used until a local instance is provided. For example, we use var(--bs-rows, 1)
for our CSS Grid rows, which ignores --bs-rows
because that hasn’t been set anywhere yet. Once it is, the .grid
instance will use that value instead of the fallback value of 1
.
No grid classes
Immediate children elements of .grid
are grid items, so they’ll be sized without explicitly adding a .g-col
class.
<div class="grid text-center" style="--bs-columns: 3;">
<div>Auto-column</div>
<div>Auto-column</div>
<div>Auto-column</div>
</div>
Columns and gaps
Adjust the number of columns and the gap.
<div class="grid text-center" style="--bs-columns: 4; --bs-gap: 5rem;">
<div class="g-col-2">.g-col-2</div>
<div class="g-col-2">.g-col-2</div>
</div>
<div class="grid text-center" style="--bs-columns: 10; --bs-gap: 1rem;">
<div class="g-col-6">.g-col-6</div>
<div class="g-col-4">.g-col-4</div>
</div>
Adding rows
Adding more rows and changing the placement of columns:
<div class="grid text-center" style="--bs-rows: 3; --bs-columns: 3;">
<div>Auto-column</div>
<div class="g-start-2" style="grid-row: 2">Auto-column</div>
<div class="g-start-3" style="grid-row: 3">Auto-column</div>
</div>
Gaps
Change the vertical gaps only by modifying the row-gap
. Note that we use gap
on .grid
s, but row-gap
and column-gap
can be modified as needed.
<div class="grid text-center" style="row-gap: 0;">
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
</div>
Because of that, you can have different vertical and horizontal gap
s, which can take a single value (all sides) or a pair of values (vertical and horizontal). This can be applied with an inline style for gap
, or with our --bs-gap
CSS variable.
<div class="grid text-center" style="--bs-gap: .25rem 1rem;">
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
<div class="g-col-6">.g-col-6</div>
</div>
Sass
One limitation of the CSS Grid is that our default classes are still generated by two Sass variables, $grid-columns
and $grid-gutter-width
. This effectively predetermines the number of classes generated in our compiled CSS. You have two options here:
- Modify those default Sass variables and recompile your CSS.
- Use inline or custom styles to augment the provided classes.
For example, you can increase the column count and change the gap size, and then size your “columns” with a mix of inline styles and predefined CSS Grid column classes (e.g., .g-col-4
).
<div class="grid text-center" style="--bs-columns: 18; --bs-gap: .5rem;">
<div style="grid-column: span 14;">14 columns</div>
<div class="g-col-4">.g-col-4</div>
</div>