Utiliti API
Utilitas API mangrupikeun alat dumasar-Sass pikeun ngahasilkeun kelas utiliti.
Utiliti Bootstrap dihasilkeun ku API utilitas kami sareng tiasa dianggo pikeun ngarobih atanapi ngalegaan set standar kelas utilitas kami via Sass. API utiliti kami dumasar kana runtuyan peta Sass jeung fungsi pikeun generating kulawarga kelas jeung sagala rupa pilihan. Upami anjeun teu biasa sareng peta Sass, baca dina dokumén Sass resmi pikeun ngamimitian.
Peta éta $utilities
ngandung sadaya utilitas kami sareng engké dihijikeun sareng $utilities
peta khusus anjeun, upami aya. Peta utiliti ngandung daptar konci grup utiliti anu nampi pilihan ieu:
Pilihan | Tipe | Nilai standar | Katerangan |
---|---|---|---|
property |
Dibutuhkeun | – | Ngaran sipat, ieu bisa mangrupa string atawa Asép Sunandar Sunarya ti string (misalna paddings horizontal atawa margins). |
values |
Dibutuhkeun | – | Daptar nilai, atanapi peta upami anjeun henteu hoyong nami kelas sami sareng nilaina. Upami null dianggo salaku konci peta, éta henteu dikompilasi. |
class |
Pilihan | null | Ngaran kelas dihasilkeun. Lamun teu disadiakeun sarta property mangrupa Asép Sunandar Sunarya ti string, class bakal dituna kana unsur mimiti property Asép Sunandar Sunarya dina. |
css-var |
Pilihan | false |
Boolean pikeun ngahasilkeun variabel CSS tinimbang aturan CSS. |
local-vars |
Pilihan | null | Peta variabel CSS lokal pikeun ngahasilkeun salian aturan CSS. |
state |
Pilihan | null | Daptar varian pseudo-kelas (misalna, :hover atawa :focus ) pikeun ngahasilkeun. |
responsive |
Pilihan | false |
Boolean nunjukkeun lamun kelas responsif kudu dihasilkeun. |
rfs |
Pilihan | false |
Boolean pikeun ngaktipkeun rescaling cairan sareng RFS . |
print |
Pilihan | false |
Boolean nunjukkeun lamun kelas print kudu dihasilkeun. |
rtl |
Pilihan | true |
Boolean nunjukkeun upami utiliti kedah disimpen dina RTL. |
API dipedar
Sadaya variabel utiliti ditambahkeun kana $utilities
variabel dina _utilities.scss
stylesheet urang. Unggal grup utilitas kasampak kawas kieu:
$utilities: (
"opacity": (
property: opacity,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);
Anu kaluaran di handap ieu:
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }
Harta
Konci anu diperyogikeun property
kedah disetél pikeun utilitas naon waé, sareng éta kedah ngandung sipat CSS anu sah. Sipat ieu dianggo dina set aturan utiliti anu dihasilkeun. Nalika class
konci disingkahkeun, éta ogé janten nami kelas standar. Mertimbangkeun text-decoration
utiliti:
$utilities: (
"text-decoration": (
property: text-decoration,
values: none underline line-through
)
);
Kaluaran:
.text-decoration-none { text-decoration: none !important; }
.text-decoration-underline { text-decoration: underline !important; }
.text-decoration-line-through { text-decoration: line-through !important; }
Nilai-nilai
Paké values
konci pikeun nangtukeun mana nilai pikeun dieusian property
kudu dipaké dina ngaran kelas dihasilkeun sarta aturan. Bisa mangrupa daptar atawa peta (disetél dina Utiliti atawa dina variabel Sass).
Salaku daptar, sapertos text-decoration
utilitas :
values: none underline line-through
Salaku peta, sapertos opacity
utilitas :
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
Salaku variabel Sass anu nyetél daptar atanapi peta, sapertos dina position
utilitas kami :
values: $position-values
Kelas
Anggo class
pilihan pikeun ngarobih awalan kelas anu dianggo dina CSS anu disusun. Contona, pikeun ngarobah tina .opacity-*
ka .o-*
:
$utilities: (
"opacity": (
property: opacity,
class: o,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);
Kaluaran:
.o-0 { opacity: 0 !important; }
.o-25 { opacity: .25 !important; }
.o-50 { opacity: .5 !important; }
.o-75 { opacity: .75 !important; }
.o-100 { opacity: 1 !important; }
Utiliti variabel CSS
Setel css-var
pilihan boolean true
sareng API bakal ngahasilkeun variabel CSS lokal pikeun pamilih anu dipasihkeun tibatan property: value
aturan anu biasa. Pertimbangkeun .text-opacity-*
utilitas kami:
$utilities: (
"text-opacity": (
css-var: true,
class: text-opacity,
values: (
25: .25,
50: .5,
75: .75,
100: 1
)
),
);
Kaluaran:
.text-opacity-25 { --bs-text-opacity: .25; }
.text-opacity-50 { --bs-text-opacity: .5; }
.text-opacity-75 { --bs-text-opacity: .75; }
.text-opacity-100 { --bs-text-opacity: 1; }
variabel CSS lokal
Anggo local-vars
pilihan pikeun nangtukeun peta Sass anu bakal ngahasilkeun variabel CSS lokal dina set aturan kelas utiliti. Perhatikeun yén éta bisa merlukeun gawé tambahan pikeun meakeun eta variabel CSS lokal dina aturan CSS dihasilkeun. Salaku conto, pertimbangkeun .bg-*
utilitas kami:
$utilities: (
"background-color": (
property: background-color,
class: bg,
local-vars: (
"bg-opacity": 1
),
values: map-merge(
$utilities-bg-colors,
(
"transparent": transparent
)
)
)
);
Kaluaran:
.bg-primary {
--bs-bg-opacity: 1;
background-color: rgba(var(--bs-primary-rgb), var(--bs-bg-opacity)) !important;
}
Amérika Sarikat
Paké state
pilihan pikeun ngahasilkeun variasi pseudo-kelas. Conto pseudo-kelas nyaeta :hover
na :focus
. Nalika daptar nagara bagian disadiakeun, classnames dijieun pikeun pseudo-kelas éta. Contona, pikeun ngarobah opacity on hover, tambahkeun state: hover
tur Anjeun bakal meunang .opacity-hover:hover
dina CSS disusun Anjeun.
Peryogi sababaraha kelas pseudo? Paké daptar spasi-dipisahkeun nagara bagian state: hover focus
:.
$utilities: (
"opacity": (
property: opacity,
class: opacity,
state: hover,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);
Kaluaran:
.opacity-0-hover:hover { opacity: 0 !important; }
.opacity-25-hover:hover { opacity: .25 !important; }
.opacity-50-hover:hover { opacity: .5 !important; }
.opacity-75-hover:hover { opacity: .75 !important; }
.opacity-100-hover:hover { opacity: 1 !important; }
Responsif
Tambahkeun responsive
boolean pikeun ngahasilkeun utilitas responsif (contona, .opacity-md-25
) dina sadaya titik putus .
$utilities: (
"opacity": (
property: opacity,
responsive: true,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);
Kaluaran:
.opacity-0 { opacity: 0 !important; }
.opacity-25 { opacity: .25 !important; }
.opacity-50 { opacity: .5 !important; }
.opacity-75 { opacity: .75 !important; }
.opacity-100 { opacity: 1 !important; }
@media (min-width: 576px) {
.opacity-sm-0 { opacity: 0 !important; }
.opacity-sm-25 { opacity: .25 !important; }
.opacity-sm-50 { opacity: .5 !important; }
.opacity-sm-75 { opacity: .75 !important; }
.opacity-sm-100 { opacity: 1 !important; }
}
@media (min-width: 768px) {
.opacity-md-0 { opacity: 0 !important; }
.opacity-md-25 { opacity: .25 !important; }
.opacity-md-50 { opacity: .5 !important; }
.opacity-md-75 { opacity: .75 !important; }
.opacity-md-100 { opacity: 1 !important; }
}
@media (min-width: 992px) {
.opacity-lg-0 { opacity: 0 !important; }
.opacity-lg-25 { opacity: .25 !important; }
.opacity-lg-50 { opacity: .5 !important; }
.opacity-lg-75 { opacity: .75 !important; }
.opacity-lg-100 { opacity: 1 !important; }
}
@media (min-width: 1200px) {
.opacity-xl-0 { opacity: 0 !important; }
.opacity-xl-25 { opacity: .25 !important; }
.opacity-xl-50 { opacity: .5 !important; }
.opacity-xl-75 { opacity: .75 !important; }
.opacity-xl-100 { opacity: 1 !important; }
}
@media (min-width: 1400px) {
.opacity-xxl-0 { opacity: 0 !important; }
.opacity-xxl-25 { opacity: .25 !important; }
.opacity-xxl-50 { opacity: .5 !important; }
.opacity-xxl-75 { opacity: .75 !important; }
.opacity-xxl-100 { opacity: 1 !important; }
}
Nyitak
Aktipkeun print
pilihan ogé bakal ngahasilkeun kelas utiliti pikeun citak, nu ngan dilarapkeun dina @media print { ... }
pamundut média.
$utilities: (
"opacity": (
property: opacity,
print: true,
values: (
0: 0,
25: .25,
50: .5,
75: .75,
100: 1,
)
)
);
Kaluaran:
.opacity-0 { opacity: 0 !important; }
.opacity-25 { opacity: .25 !important; }
.opacity-50 { opacity: .5 !important; }
.opacity-75 { opacity: .75 !important; }
.opacity-100 { opacity: 1 !important; }
@media print {
.opacity-print-0 { opacity: 0 !important; }
.opacity-print-25 { opacity: .25 !important; }
.opacity-print-50 { opacity: .5 !important; }
.opacity-print-75 { opacity: .75 !important; }
.opacity-print-100 { opacity: 1 !important; }
}
pentingna
Kabéh Utiliti dihasilkeun ku API kaasup !important
pikeun mastikeun aranjeunna override komponén tur kelas modifier sakumaha dimaksudkeun. Anjeun tiasa togél setélan ieu sacara global sareng $enable-important-utilities
variabel (standar ka true
).
Ngagunakeun API
Ayeuna anjeun geus wawuh jeung kumaha Utiliti API jalan, diajar kumaha carana nambahkeun kelas custom anjeun sorangan jeung ngaropéa utilitas standar urang.
Utiliti override
Override Utiliti aya ku ngagunakeun konci sarua. Salaku conto, upami anjeun hoyong kelas utiliti overflow responsif tambahan, anjeun tiasa ngalakukeun ieu:
$utilities: (
"overflow": (
responsive: true,
property: overflow,
values: visible hidden scroll auto,
),
);
Tambahkeun utiliti
Utiliti anyar tiasa nambihan kana $utilities
peta standar nganggo map-merge
. Pastikeun file Sass kami anu diperyogikeun sareng _utilities.scss
diimpor heula, teras paké map-merge
pikeun nambihan utilitas tambahan anjeun. Contona, ieu kumaha carana nambahkeun cursor
utilitas responsif kalayan tilu nilai.
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";
$utilities: map-merge(
$utilities,
(
"cursor": (
property: cursor,
class: cursor,
responsive: true,
values: auto pointer grab,
)
)
);
Ngaropéa Utiliti
$utilities
Ngaropéa Utiliti aya dina peta standar kalawan map-get
jeung map-merge
fungsi. Dina conto di handap, urang nambihan nilai tambahan kana width
utilitas. Mimitian ku awal map-merge
teras sebutkeun utilitas anu anjeun hoyong robih. Ti dinya, cokot "width"
peta anu disarangkeun map-get
pikeun ngaksés sareng ngarobih pilihan sareng nilai utiliti.
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";
$utilities: map-merge(
$utilities,
(
"width": map-merge(
map-get($utilities, "width"),
(
values: map-merge(
map-get(map-get($utilities, "width"), "values"),
(10: 10%),
),
),
),
)
);
Aktipkeun responsif
Anjeun tiasa ngaktifkeun kelas responsif pikeun sakumpulan utilitas anu ayeuna henteu responsif sacara standar. Contona, sangkan border
kelas responsif:
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";
$utilities: map-merge(
$utilities, (
"border": map-merge(
map-get($utilities, "border"),
( responsive: true ),
),
)
);
Ieu ayeuna bakal ngahasilkeun variasi responsif .border
sareng .border-0
pikeun tiap breakpoint. CSS Anjeun dihasilkeun bakal kasampak kawas kieu:
.border { ... }
.border-0 { ... }
@media (min-width: 576px) {
.border-sm { ... }
.border-sm-0 { ... }
}
@media (min-width: 768px) {
.border-md { ... }
.border-md-0 { ... }
}
@media (min-width: 992px) {
.border-lg { ... }
.border-lg-0 { ... }
}
@media (min-width: 1200px) {
.border-xl { ... }
.border-xl-0 { ... }
}
@media (min-width: 1400px) {
.border-xxl { ... }
.border-xxl-0 { ... }
}
Ganti ngaran utiliti
Utiliti v4 leungit, atanapi biasa nganggo konvénsi ngaran anu sanés? Utiliti API tiasa dianggo pikeun nimpa hasil class
tina utilitas anu dipasihkeun-contona, pikeun ngaganti ngaran .ms-*
utilitas ka oldish .ml-*
:
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";
$utilities: map-merge(
$utilities, (
"margin-start": map-merge(
map-get($utilities, "margin-start"),
( class: ml ),
),
)
);
Cabut utilitas
Cabut salah sahiji utilitas standar ku netepkeun konci grup ka null
. Contona, pikeun miceun kabeh width
Utiliti kami, nyieun hiji $utilities
map-merge
tur nambahkeun "width": null
dina.
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";
$utilities: map-merge(
$utilities,
(
"width": null
)
);
Cabut utiliti dina RTL
Sababaraha kasus tepi ngajantenkeun gaya RTL sesah , sapertos putus garis dina basa Arab. Janten utilitas tiasa diturunkeun tina kaluaran RTL ku netepkeun rtl
pilihan pikeun false
:
$utilities: (
"word-wrap": (
property: word-wrap word-break,
class: text,
values: (break: break-word),
rtl: false
),
);
Kaluaran:
/* rtl:begin:remove */
.text-break {
word-wrap: break-word !important;
word-break: break-word !important;
}
/* rtl:end:remove */
Ieu henteu ngaluarkeun nanaon dina RTL, hatur nuhun kana diréktif kontrol RTLCSSremove
.