Slaan oor na hoofinhoud Slaan oor na dokumentnavigasie
in English

CSS-rooster

Leer hoe om ons alternatiewe uitlegstelsel wat op CSS Grid gebou is, te aktiveer, te gebruik en aan te pas met voorbeelde en kodebrokkies.

Bootstrap se verstekroosterstelsel verteenwoordig die hoogtepunt van meer as 'n dekade van CSS-uitlegtegnieke, beproef en getoets deur miljoene mense. Maar dit is ook geskep sonder baie van die moderne CSS-kenmerke en -tegnieke wat ons in blaaiers soos die nuwe CSS Grid sien.

Neem kennis – ons CSS Grid-stelsel is eksperimenteel en het intekening vanaf v5.1.0! Ons het dit by ons dokumentasie se CSS ingesluit om dit vir jou te demonstreer, maar dit is by verstek gedeaktiveer. Hou aan lees om te leer hoe om dit in jou projekte te aktiveer.

Hoe dit werk

Met Bootstrap 5 het ons die opsie bygevoeg om 'n aparte roosterstelsel te aktiveer wat op CSS Grid gebou is, maar met 'n Bootstrap-draai. Jy kry steeds klasse wat jy op 'n gril kan toepas om responsiewe uitlegte te bou, maar met 'n ander benadering onder die enjinkap.

  • CSS Grid is intekening. Deaktiveer die verstek roosterstelsel deur in te stel $enable-grid-classes: falseen aktiveer die CSS Grid deur te stel $enable-cssgrid: true. Stel dan jou Sass saam.

  • Vervang gevalle van .rowmet .grid. Die .gridklas stel display: griden skep 'n grid-templatewaarop jy met jou HTML bou.

  • Vervang .col-*klasse met .g-col-*klasse. Dit is omdat ons CSS Grid-kolomme die grid-columneiendom gebruik in plaas van width.

  • Kolomme en geutgroottes word gestel via CSS-veranderlikes. Stel dit op die ouer .griden pasmaak soos jy wil, inlyn of in 'n stylblad, met --bs-columnsen --bs-gap.

In die toekoms sal Bootstrap waarskynlik oorskakel na 'n hibriede oplossing, aangesien die gapeiendom byna volledige blaaierondersteuning vir flexbox bereik het.

Sleutel verskille

In vergelyking met die standaard roosterstelsel:

  • Flex-hulpprogramme beïnvloed nie die CSS Grid-kolomme op dieselfde manier nie.

  • Gapings vervang geute. Die gapeiendom vervang die horisontale paddingvan ons verstek roosterstelsel en funksioneer meer soos margin.

  • As such, unlike .rows, .grids have no negative margins and margin utilities cannot be used to change the grid gutters. Grid gaps are applied horizontally and vertically by default. See the customizing section for more details.

  • Inline and custom styles should be viewed as replacements for modifier classes (e.g., style="--bs-columns: 3;" vs class="row-cols-3").

  • Nesting works similarly, but may require you to reset your column counts on each instance of a nested .grid. See the nesting section for details.

Examples

Three columns

Three equal-width columns across all viewports and devices can be created by using the .g-col-4 classes. Add responsive classes to change the layout by viewport size.

.g-col-4
.g-col-4
.g-col-4
<div class="grid">
  <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.

.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
<div class="grid">
  <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.

.g-col-6
.g-col-6
<div class="grid">
  <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.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
<div class="grid">
  <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.

.g-col-3 .g-start-2
.g-col-4 .g-start-6
<div class="grid">
  <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.

1
1
1
1
1
1
1
1
1
1
1
1
<div class="grid">
  <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.

.g-col-6
1
1
1
1
1
1
<div class="grid">
  <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 .grids. 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.

First auto-column
Auto-column
Auto-column
Second auto-column
6 of 12
4 of 12
2 of 12
Third auto-column
<div class="grid" 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.

Auto-column
Auto-column
Auto-column
<div class="grid" 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.

.g-col-2
.g-col-2
<div class="grid" 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>
.g-col-6
.g-col-4
<div class="grid" 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:

Auto-column
Auto-column
Auto-column
<div class="grid" 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 .grids, but row-gap and column-gap can be modified as needed.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
<div class="grid" 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 gaps, 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.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
<div class="grid" 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).

14 columns
.g-col-4
<div class="grid" 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>