Hla mus rau cov ntsiab lus tseem ceeb Hla mus rau docs navigation
in English

CSS Grid

Kawm paub yuav ua li cas thiaj li pab tau, siv, thiab kho peb cov txheej txheem layout uas tsim los ntawm CSS Grid nrog cov piv txwv thiab cov lej snippets.

Bootstrap's default grid system sawv cev rau qhov kawg ntawm ntau tshaj kaum xyoo ntawm CSS layout cov tswv yim, sim thiab sim los ntawm ntau lab tus tib neeg. Tab sis, nws kuj tau tsim tsis muaj ntau yam ntawm CSS niaj hnub nta thiab cov tswv yim peb pom hauv browsers zoo li CSS Grid tshiab.

Tau taub hau - peb CSS Grid system yog kev sim thiab xaiv nyob rau hauv v5.1.0! Peb muab nws tso rau hauv peb cov ntaub ntawv CSS los ua qauv qhia rau koj, tab sis nws yog neeg xiam oob qhab los ntawm lub neej ntawd. Nyeem ntawv kom kawm paub yuav ua li cas pab nws hauv koj cov haujlwm.

Nws ua haujlwm li cas

Nrog Bootstrap 5, peb tau ntxiv qhov kev xaiv los ua kom muaj kab sib cais cais uas tau tsim ntawm CSS Grid, tab sis nrog Bootstrap twist. Koj tseem tau txais cov chav kawm koj tuaj yeem thov rau lub siab xav tsim cov lus teb, tab sis nrog txoj hauv kev sib txawv hauv qab lub hood.

  • CSS Grid yog opt-in. Disable lub default grid system los ntawm kev teeb tsa $enable-grid-classes: falsethiab pab kom CSS Grid los ntawm kev teeb tsa $enable-cssgrid: true. Tom qab ntawd, recompile koj Sass.

  • Hloov cov piv txwv .rownrog .grid. Cov .gridchav kawm teeb tsa display: gridthiab tsim ib qho grid-templateuas koj tsim nrog koj HTML.

  • Hloov .col-*cov chav kawm nrog .g-col-*cov chav kawm. Qhov no yog vim peb CSS Grid kab siv cov grid-columncuab yeej hloov width.

  • Kem thiab qhov ntau thiab tsawg gutter yog teem los ntawm CSS variables. Teem cov no rau ntawm niam txiv .gridthiab hloov kho txawm li cas los xij koj xav tau, hauv kab lossis hauv daim ntawv style, nrog --bs-columnsthiab --bs-gap.

Nyob rau hauv lub neej yav tom ntej, Bootstrap yuav zoo li hloov mus rau hybrid tov raws li cov gapcuab yeej tau ua tiav yuav luag tag nrho browser txhawb rau flexbox.

Qhov sib txawv tseem ceeb

Piv nrog rau lub default grid system:

  • Flex utilities tsis cuam tshuam rau CSS Grid kab ib yam nkaus.

  • Qhov khoob hloov gutters. Cov gapcuab yeej hloov cov kab rov tav paddinglos ntawm peb lub default grid system thiab ua haujlwm zoo li margin.

  • Raws li xws li, tsis zoo li .rows, .grids tsis muaj cov npoo tsis zoo thiab cov khoom siv hluav taws xob tsis tuaj yeem siv los hloov cov kab sib chaws gutters. Grid gap yog siv horizontally thiab vertically los ntawm lub neej ntawd. Saib qhov customizing seem kom paub meej ntxiv.

  • Inline thiab kev cai style yuav tsum raug saib raws li kev hloov pauv rau cov chav kawm hloov kho (xws li, style="--bs-columns: 3;"vs class="row-cols-3").

  • Zes ua haujlwm zoo ib yam, tab sis tej zaum yuav xav kom koj rov pib dua koj cov suav suav ntawm txhua qhov piv txwv ntawm nested .grid. Saib cov zes seem kom paub meej.

Piv txwv

Peb kab

Peb kab sib npaug-dav thoob plaws txhua qhov chaw saib thiab cov khoom siv tuaj yeem tsim los ntawm kev siv cov .g-col-4chav kawm. Ntxiv cov chav kawm teb los hloov qhov layout los ntawm qhov loj ntawm qhov chaw saib.

.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>

Teb

Siv cov chav kawm teb los kho koj qhov layout hla cov chaw saib. Ntawm no peb pib nrog ob kab ntawm qhov nqaim viewports, thiab tom qab ntawd loj hlob mus rau peb kab ntawm nruab nrab viewports thiab saum toj no.

.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>

Muab piv rau qhov no ob kab layout ntawm txhua qhov chaw saib.

.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>

qhwv

Daim phiaj cov khoom cia li qhwv mus rau kab tom ntej thaum tsis muaj chav nyob horizontally. Nco ntsoov tias qhov gapsiv tau rau kab rov tav thiab ntsug qhov sib txawv ntawm cov kab sib chaws.

.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>

Pib

Pib cov chav kawm tsom los hloov peb cov chav kawm ntawv raws li qhov offset, tab sis lawv tsis yog tib yam nkaus. CSS Grid tsim ib daim phiaj qauv los ntawm cov qauv uas qhia cov browsers kom "pib ntawm kab ntawv no" thiab "kawg ntawm kab ntawv no." Cov khoom ntawd yog grid-column-startthiab grid-column-end. Cov chav kawm pib yog luv luv rau cov qub. Ua ke lawv nrog cov chav kawm kem kom loj thiab kho koj cov kab txawm li cas los xij koj xav tau. Cov chav kawm pib pib ntawm 1qhov 0tsis muaj nqi rau cov khoom no.

.g-col-3 .g-pib-2
.g-col-4 .g-pib-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>

Nws pib kab

Thaum tsis muaj cov chav kawm ntawm cov kab sib chaws (cov me nyuam tam sim ntawd ntawm ib qho .grid), txhua daim phiaj yuav cia li muab qhov loj me rau ib kab.

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>

Qhov kev coj cwj pwm no tuaj yeem sib xyaw nrog cov kab ke kab ke.

.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>

Zes

Zoo ib yam li peb daim phiaj xwm txheej, peb CSS Grid tso cai rau kev ua zes yooj yim ntawm .grids. Txawm li cas los xij, tsis zoo li lub neej ntawd, daim phiaj no tau txais kev hloov pauv hauv kab, kab, thiab qhov khoob. Xav txog qhov piv txwv hauv qab no:

  • Peb override tus naj npawb ntawm kab ke nrog lub zos CSS kuj sib txawv: --bs-columns: 3.
  • Nyob rau hauv thawj auto-kem, kem suav yog tau txais txiaj ntsig thiab txhua kem yog ib feem peb ntawm qhov dav muaj.
  • Hauv qhov thib ob nws pib-kem, peb tau rov pib dua kab suav ntawm qhov zes .gridrau 12 (peb lub neej ntawd).
  • Qhov thib peb auto-kem tsis muaj cov ntsiab lus nested.

Nyob rau hauv kev xyaum, qhov no tso cai rau ntau complex thiab kev cai layouts thaum piv rau peb default grid system.

Thawj auto-column
Nws pib-kem
Nws pib-kem
Qhov thib ob auto-kem
6av 12
4av 12
2 ntawm 12
Peb 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 tus naj npawb ntawm kab, tus naj npawb ntawm kab, thiab qhov dav ntawm qhov khoob nrog cov CSS hauv zos hloov pauv.

Hloov pauv Rov qab tus nqi Kev piav qhia
--bs-rows 1 Tus naj npawb ntawm kab hauv koj daim phiaj template
--bs-columns 12 Tus naj npawb ntawm kab hauv koj daim phiaj template
--bs-gap 1.5rem Qhov loj ntawm qhov sib txawv ntawm txhua kab (ntu thiab kab rov tav)

Cov CSS variables no tsis muaj nqi dab tsi; Hloov chaw, lawv siv cov txiaj ntsig poob rov qab uas tau siv kom txog thaum muab piv txwv hauv zos. Piv txwv li, peb siv var(--bs-rows, 1)rau peb CSS Grid kab, uas tsis quav ntsej --bs-rowsvim tias tseem tsis tau teeb tsa nyob qhov twg. Thaum nws yog, qhov .gridpiv txwv yuav siv tus nqi ntawd tsis yog tus nqi poob ntawm 1.

Tsis muaj kab sib chaws

Cov me nyuam tam sim ntawd cov khoom .gridyog kab sib chaws, yog li lawv yuav loj me me yam tsis tau ntxiv ib .g-colchav kawm.

Nws pib-kem
Nws pib-kem
Nws pib-kem
<div class="grid" style="--bs-columns: 3;">
  <div>Auto-column</div>
  <div>Auto-column</div>
  <div>Auto-column</div>
</div>

Cov kab thiab qhov khoob

Kho tus naj npawb ntawm kab thiab qhov sib txawv.

.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>

Ntxiv kab

Ntxiv ntau kab thiab hloov qhov chaw ntawm kab:

Nws pib-kem
Nws pib-kem
Nws pib-kem
<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>

Npog

Hloov cov qhov khoob ntsug tsuas yog hloov kho cov row-gap. Nco ntsoov tias peb siv gapntawm .grids, tab sis row-gapthiab column-gaptuaj yeem hloov kho raws li xav tau.

.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>

Vim li ntawd, koj tuaj yeem muaj qhov sib txawv ntsug thiab kab rov tav gaps, uas tuaj yeem coj tus nqi ib leeg (txhua sab) lossis ib khub ntawm qhov tseem ceeb (ntu thiab kab rov tav). Qhov no tuaj yeem siv nrog cov style inline rau gap, lossis nrog peb --bs-gapqhov sib txawv CSS.

.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

Ib qho kev txwv ntawm CSS Grid yog tias peb cov chav kawm ua ntej tseem tsim los ntawm ob qhov sib txawv ntawm Sass, $grid-columnsthiab $grid-gutter-width. Qhov no ua tau zoo ua ntej txiav txim seb tus naj npawb ntawm cov chav kawm tsim nyob rau hauv peb cov ntaub ntawv CSS. Koj muaj ob txoj kev xaiv ntawm no:

  • Hloov kho cov kev hloov pauv Sass default thiab rov ua kom koj CSS.
  • Siv inline lossis kev cai style los txhawb cov chav kawm.

Piv txwv li, koj tuaj yeem ua rau suav suav ntau ntxiv thiab hloov qhov sib txawv, thiab tom qab ntawd ua kom loj koj "kem" nrog kev sib xyaw ntawm cov qauv hauv kab thiab cov chav kawm CSS Grid kem ua ntej (xws li, .g-col-4).

14 kab
.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>