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.
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: false
thiab pab kom CSS Grid los ntawm kev teeb tsa$enable-cssgrid: true
. Tom qab ntawd, recompile koj Sass. -
Hloov cov piv txwv
.row
nrog.grid
. Cov.grid
chav kawm teeb tsadisplay: grid
thiab tsim ib qhogrid-template
uas 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 covgrid-column
cuab yeej hloovwidth
. -
Kem thiab qhov ntau thiab tsawg gutter yog teem los ntawm CSS variables. Teem cov no rau ntawm niam txiv
.grid
thiab hloov kho txawm li cas los xij koj xav tau, hauv kab lossis hauv daim ntawv style, nrog--bs-columns
thiab--bs-gap
.
Nyob rau hauv lub neej yav tom ntej, Bootstrap yuav zoo li hloov mus rau hybrid tov raws li cov gap
cuab 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
gap
cuab yeej hloov cov kab rov tavpadding
los ntawm peb lub default grid system thiab ua haujlwm zoo limargin
. -
Raws li xws li, tsis zoo li
.row
s,.grid
s 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;"
vsclass="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-4
chav kawm. Ntxiv cov chav kawm teb los hloov qhov layout los ntawm qhov loj ntawm qhov chaw saib.
<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.
<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.
<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 gap
siv tau rau kab rov tav thiab ntsug qhov sib txawv ntawm cov kab sib chaws.
<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-start
thiab 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 1
qhov 0
tsis muaj nqi rau cov khoom no.
<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.
<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.
<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 .grid
s. 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
.grid
rau 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.
<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-rows
vim tias tseem tsis tau teeb tsa nyob qhov twg. Thaum nws yog, qhov .grid
piv 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 .grid
yog kab sib chaws, yog li lawv yuav loj me me yam tsis tau ntxiv ib .g-col
chav kawm.
<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.
<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>
<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:
<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 gap
ntawm .grid
s, tab sis row-gap
thiab column-gap
tuaj yeem hloov kho raws li xav tau.
<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 gap
s, 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-gap
qhov sib txawv CSS.
<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-columns
thiab $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
).
<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>