Svetuka kune chikuru content Svetuka kuenda kudocs navigation
Check
in English

CSS Grid

Dzidza maitiro ekugonesa, kushandisa, uye kugadzirisa yedu imwe dhizaini sisitimu yakavakirwa paCSS Grid nemienzaniso uye zvimedu zvekodhi.

Bootstrap's default grid system inomiririra kupera kweanopfuura makore gumi eCSS magadzirirwo maitiro, akaedzwa uye akaedzwa nemamiriyoni evanhu. Asi, yakagadzirwawo pasina akawanda emazuva ano eCSS maficha uye matekiniki atiri kuona mumabhurawuza senge CSS Grid itsva.

Misoro - yedu CSS Grid sisitimu ndeyekuyedza uye yekupinda kubva v5.1.0! Isu takaisanganisira muCSS yezvinyorwa zvedu kuti tikuratidze iwe, asi yakavharwa nekusarudzika. Ramba uchiverenga kuti udzidze nzira yekuigonesa mumapurojekiti ako.

Zvinoshanda sei

NeBootstrap 5, takawedzera sarudzo yekugonesa yakaparadzana grid system yakavakirwa paCSS Grid, asi neBootstrap twist. Iwe uchiri kuwana makirasi iwe aunogona kunyorera pane whim kuti uvake anoteerera marongero, asi neimwe nzira pasi pehodhi.

  • CSS Grid yave kusarudza-kupinda. Dzima iyo default grid system nekuseta $enable-grid-classes: falseuye gonesa iyo CSS Grid nekuseta $enable-cssgrid: true. Zvadaro, dzokorora Sass yako.

  • Tsiva zviitiko .rowzve .grid. Kirasi .gridinoseta display: griduye inogadzira grid-templateiyo yaunovaka pairi neHTML yako.

  • Tsiva .col-*makirasi .g-col-*nemakirasi. Izvi zvinodaro nekuti yedu CSS Grid makoramu anoshandisa iyo grid-columnpfuma pachinzvimbo che width.

  • Makolamu uye saizi yegutter inoiswa kuburikidza neCSS zvinosiyana. Isa izvi pamubereki .griduye gadzirisa chero chaunoda, inline kana musheetti, ine --bs-columnsuye --bs-gap.

Mune ramangwana, Bootstrap inogona kuchinjika kune yakasanganiswa mhinduro sezvo chivakwa gapchakawana chinenge chakazara chebhurawuza rutsigiro rweflexbox.

Misiyano mikuru

Kuenzaniswa neiyo default grid system:

  • Flex zvishandiso hazvikanganisi iyo CSS Grid makoramu nenzira imwechete.

  • Magapu anotsiva magata. Iyo gapmidziyo inotsiva yakachinjika paddingkubva kune yedu default grid system uye inoshanda zvakanyanya senge margin.

  • Saka nekudaro, kusiyana .rownes, .grids haina migero isina kunaka uye margin zvishandiso hazvigone kushandiswa kushandura magita egidhi. Grid gap inoiswa yakachinjika uye yakatwasuka nekukasira. Ona chikamu chekugadzirisa kuti uwane mamwe mashoko.

  • Inline uye masitaera etsika anofanirwa kutariswa seanotsiva makirasi ekugadzirisa (semuenzaniso, style="--bs-columns: 3;"vs class="row-cols-3").

  • Nesting inoshanda zvakafanana, asi ingada kuti iwe uise patsva makoramu ako pane imwe neimwe chiitiko chenested .grid. Ona chikamu chekuisa dendere kuti uwane ruzivo.

Mienzaniso

Makoramu matatu

Makoramu matatu akaenzana-akaenzana pamativi ese ekuona uye zvishandiso zvinogona kugadzirwa nekushandisa .g-col-4makirasi. Wedzera makirasi anoteerera kuti uchinje marongero nehukuru hwekutarisa.

.g-col-4
.g-col-4
.g-col-4
html
<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>

Anoteerera

Shandisa makirasi anopindura kugadzirisa dhizaini yako pamativi ekuona. Pano tinotanga nemakoramu maviri pane yakamanikana kuona nzvimbo, uye tobva takura kusvika kumakoramu matatu pane epakati maonero uye pamusoro.

.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
.g-col-6 .g-col-md-4
html
<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>

Enzanisa izvo kune iyi mbiri mbiru dhizaini pane ese ekuona nzvimbo.

.g-col-6
.g-col-6
html
<div class="grid text-center">
  <div class="g-col-6">.g-col-6</div>
  <div class="g-col-6">.g-col-6</div>
</div>

Kuputira

Grid zvinhu zvinozviputira kumutsara unotevera kana pasisina nzvimbo yakachinjika. Ziva kuti iyo gapinoshanda kune yakachinjika uye yakatwasuka mikaha pakati pezvinhu zvegiridhi.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
html
<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

Makirasi ekutanga anovavarira kutsiva makirasi edu egidhi ekugadzirisa, asi haana kufanana zvachose. CSS Grid inogadzira template yegridi kuburikidza nezvitaera zvinoudza mabhurawuza kuti "tanga pakoramu iyi" uye "kupedzisa pane ino." Zvinhu izvozvo ndezvi grid-column-startuye grid-column-end. Makirasi ekutanga mapfupi kune ekare. Vabatanidze nemakirasi emakoramu kuhukuru uye gadzirisa makoramu ako zvisinei iwe zvaunoda. Makirasi ekutanga anotanga 1semutengo 0usiri wakakodzera wezvivakwa izvi.

.g-col-3 .g-start-2
.g-col-4 .g-start-6
html
<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

Kana pasina makirasi pazvinhu zvegidhi (vana vekare vea .grid), chinhu chimwe nechimwe chegiridhi chinozongove chakakura kune imwe koramu.

1
1
1
1
1
1
1
1
1
1
1
1
html
<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>

Maitiro aya anogona kusanganiswa negrid column makirasi.

.g-col-6
1
1
1
1
1
1
html
<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

Zvakafanana neyedu yekusarudzika grid system, yedu CSS Grid inobvumira kureruka kwe .grids. Nekudaro, kusiyana nekusarudzika, iyi grid inogara nhaka shanduko mumitsara, makoramu, uye mapundu. Chimbofunga muenzaniso uri pasi apa:

  • Isu tinodarika nhamba yekusarudzika yemakoramu neCSS yemunharaunda: --bs-columns: 3.
  • Mune yekutanga-auto-column, nhamba yekoramu inogarwa uye yega yega chikamu chimwe muzvitatu chehupamhi huripo.
  • Mune yechipiri otomatiki-column, isu takaseta zvakare nhamba yekoramu pane yakavakirwa .gridkusvika gumi nemaviri (yedu default).
  • Yechitatu auto-column haina nested content.

Mukuita izvi zvinobvumira mamwe akaomesesa uye etsika marongero kana ichienzaniswa neyedu default grid system.

First auto-column
Auto-column
Auto-column
Second auto-column
6 pa12
4 ye12
2 ye12
Yechitatu auto-column
html
<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

Gadzirisa nhamba yemakoramu, nhamba yemitsara, uye hupamhi hwemagapu nemhando dzeCSS dzemunharaunda.

Variable Fallback value Tsanangudzo
--bs-rows 1 Huwandu hwemitsara mune yako grid template
--bs-columns 12 Huwandu hwemakoramu mune yako grid template
--bs-gap 1.5rem Saizi yemukana uri pakati pemakoramu (yakatwasuka uye yakachinjika)

Izvi zvakasiyana-siyana zveCSS hazvina kukwana kukosha; pachinzvimbo, vanoshandisa fallback values ​​dzinoshandiswa kusvikira muenzaniso wemunharaunda wapihwa. Semuenzaniso, isu tinoshandisa var(--bs-rows, 1)yedu CSS Grid mitsara, iyo inofuratira --bs-rowsnekuti iyo haisati yaiswa chero kupi. Kana zvadaro, .gridmuenzaniso wacho uchashandisa kukosha ikoko pachinzvimbo cheiyo fallback kukosha kwe 1.

Hapana makirasi egridi

Zvekare vana zvinhu .gridzvegidhi zvinhu, saka ivo vanozokura pasina kuwedzera .g-colkirasi.

Auto-column
Auto-column
Auto-column
html
<div class="grid text-center" style="--bs-columns: 3;">
  <div>Auto-column</div>
  <div>Auto-column</div>
  <div>Auto-column</div>
</div>

Columns nemagapu

Gadzirisa nhamba yemakoramu uye gap.

.g-col-2
.g-col-2
html
<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>
.g-col-6
.g-col-4
html
<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>

Kuwedzera mitsara

Kuwedzera mamwe mitsara uye kushandura kuiswa kwemakoramu:

Auto-column
Auto-column
Auto-column
html
<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

Shandura migero yakatwasuka chete nekugadzirisa iyo row-gap. Ziva kuti isu tinoshandisa gappa .grids, asi row-gapuye column-gapinogona kugadziridzwa sezvinodiwa.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
html
<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>

Nekuda kweizvozvo, iwe unogona kuve neyakasiyana yakatwasuka uye yakachinjika gaps, iyo inogona kutora imwechete kukosha (mativi ese) kana peya yemhando (yakatwasuka uye yakachinjika). Izvi zvinogona kuiswa neinline style ye gap, kana --bs-gapneCSS yedu inosiyana.

.g-col-6
.g-col-6
.g-col-6
.g-col-6
html
<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

Imwe miganho yeCSS Grid ndeyekuti makirasi edu ekutanga achiri kugadzirwa nemhando mbiri dzeSass, $grid-columnsuye $grid-gutter-width. Izvi zvinonyatsofanoona huwandu hwemakirasi akagadzirwa muCSS yedu yakaunganidzwa. Une sarudzo mbiri pano:

  • Shandura izvo zvimiro zveSass zvakasiyana uye dzorera CSS yako.
  • Shandisa inline kana masitaera etsika kuti uwedzere makirasi akapihwa.

Semuenzaniso, unogona kuwedzera kuverenga kwekoramu uye kushandura saizi yegep, uye wobva wawedzera "columns" dzako nemusanganiswa weinline masitaera uye yakafanotsanangurwa CSS Grid makoramu makirasi (semuenzaniso, .g-col-4).

14 columns
.g-col-4
html
<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>