Sass
Sebenzisa umthombo wethu wamafayela e-Sass ukuze usizakale ngezinto eziguquguqukayo, amamephu, izingxube, nemisebenzi ukukusiza ukuthi wakhe ngokushesha futhi wenze iphrojekthi yakho ngendlela oyifisayo.
Sebenzisa umthombo wethu wamafayela e-Sass ukuze uthathe ithuba lokuhlukahluka, amamephu, imiksi, nokuningi.
Isakhiwo sefayela
Noma nini lapho kunokwenzeka, gwema ukulungisa amafayela ayinhloko e-Bootstrap. Kwa-Sass, lokho kusho ukudala eyakho ishidi lesitayela elingenisa i-Bootstrap ukuze ukwazi ukuyilungisa futhi uyinwebe. Uma ucabanga ukuthi usebenzisa umphathi wephakheji njenge-npm, uzoba nesakhiwo sefayela esibukeka kanje:
your-project/
├── scss
│ └── custom.scss
└── node_modules/
└── bootstrap
├── js
└── scss
Uma ulande amafayela ethu omthombo futhi ungasebenzisi umphathi wephakheji, uzofuna ukumisa mathupha into efana naleso sakhiwo, ugcine amafayela omthombo we-Bootstrap ehlukene kwewakho.
your-project/
├── scss
│ └── custom.scss
└── bootstrap/
├── js
└── scss
Iyangenisa
Kweyakho custom.scss
, uzongenisa amafayela we-Sass womthombo we-Bootstrap. Unezinketho ezimbili: faka yonke i-Bootstrap, noma khetha izingxenye ozidingayo. Sikhuthaza lokhu okwakamuva, nakuba sazi ukuthi kunezimfuneko ezithile nokuncika kuzo zonke izingxenye zethu. Uzodinga futhi ukufaka i-JavaScript yama-plugin ethu.
// Custom.scss
// Option A: Include all of Bootstrap
// Include any default variable overrides here (though functions won't be available)
@import "../node_modules/bootstrap/scss/bootstrap";
// Then add additional custom code here
// Custom.scss
// Option B: Include parts of Bootstrap
// 1. Include functions first (so you can manipulate colors, SVGs, calc, etc)
@import "../node_modules/bootstrap/scss/functions";
// 2. Include any default variable overrides here
// 3. Include remainder of required Bootstrap stylesheets
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
// 4. Include any optional Bootstrap CSS as needed
@import "../node_modules/bootstrap/scss/utilities";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/containers";
@import "../node_modules/bootstrap/scss/grid";
@import "../node_modules/bootstrap/scss/helpers";
// 5. Optionally include utilities API last to generate classes based on the Sass map in `_utilities.scss`
@import "../node_modules/bootstrap/scss/utilities/api";
// 6. Add additional custom code here
Uma lokho kusetha kusendaweni, ungaqala ukulungisa noma yikuphi okuguquguqukayo kwe-Sass namamephu kufayela lakho le- custom.scss
. Ungaqala futhi ukwengeza izingxenye ze-Bootstrap ngaphansi // Optional
kwesigaba njengoba kudingeka. Siphakamisa ukuthi usebenzise isitaki sokungenisa esigcwele esivela bootstrap.scss
kufayela lethu njengendawo yakho yokuqala.
Okuzenzakalelayo okuguquguqukayo
Konke okuguquguqukayo kwe-Sass ku-Bootstrap kufaka phakathi !default
ifulegi elikuvumela ukuthi ubhale ngaphezulu inani elizenzakalelayo lokuguquguquka ku-Sass yakho ngaphandle kokulungisa ikhodi yomthombo ye-Bootstrap. Kopisha futhi unamathisele okuguquguqukayo njengoba kudingeka, lungisa amanani azo, futhi ususe !default
ifulegi. Uma okuguquguqukayo sekuvele kwabelwe, ngakho-ke ngeke kuphinde kunikezwe amanani azenzakalelayo ku-Bootstrap.
Uzothola uhlu oluphelele lweziguquguquko ze-Bootstrap ku- scss/_variables.scss
. Ezinye eziguquguqukayo zisethelwe kokuthi null
, lezi ziguquko azikhiphi isici ngaphandle kokuthi zikhishwe ekucushweni kwakho.
Ukukhishwa okuguquguqukayo kufanele kuze ngemva kokuba imisebenzi yethu isingenisiwe, kodwa ngaphambi kokunye okuthengisiwe.
Nasi isibonelo esishintsha i- kanye background-color
neyokuthi lapho ungenisa futhi uhlanganisa i-Bootstrap nge-npm:color
<body>
// Required
@import "../node_modules/bootstrap/scss/functions";
// Default variable overrides
$body-bg: #000;
$body-color: #111;
// Required
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
// Optional Bootstrap components here
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc
Phinda njengoba kudingekile kunoma yikuphi okuguquguqukayo ku-Bootstrap, kuhlanganise nezinketho zomhlaba jikelele ngezansi.
Amamephu namaluphu
I-Bootstrap ifaka idlanzana lamamephu e-Sass, amapheya enani angukhiye akwenza kube lula ukukhiqiza imindeni ye-CSS ehlobene. Sisebenzisa amamephu e-Sass ngemibala yethu, izindawo zokunqamuka kwegridi, nokuningi. Njengezinto eziguquguqukayo ze-Sass, wonke amamephu akwa-Sass afaka !default
ifulegi futhi angabhalwa phansi futhi andwe.
Amanye amamephu ethu e-Sass ahlanganiswa abe angenalutho ngokuzenzakalelayo. Lokhu kwenzelwa ukuvumela ukunwetshwa okulula kwemephu ye-Sass enikeziwe, kodwa kuza ngezindleko zokwenza ukususa izinto kumephu kube nzima kakhulu.
Lungisa imephu
Konke okuguquguqukayo $theme-colors
kumephu kuchazwa njengokuguquguquka okuzimele. Ukuze ulungise umbala okhona $theme-colors
kumephu yethu, engeza okulandelayo kufayela lakho langokwezifiso le-Sass:
$primary: #0074d9;
$danger: #ff4136;
Kamuva, lokhu okuguquguqukayo kusethwe $theme-colors
kumephu ye-Bootstrap:
$theme-colors: (
"primary": $primary,
"danger": $danger
);
Engeza kumephu
Engeza imibala emisha ku- $theme-colors
, nanoma iyiphi enye imephu, ngokudala imephu ye-Sass entsha ngamavelu akho angokwezifiso futhi uyihlanganise nemephu yoqobo. Kulokhu, sizodala $custom-colors
imephu entsha futhi siyihlanganise ne- $theme-colors
.
// Create your own map
$custom-colors: (
"custom-color": #900
);
// Merge the maps
$theme-colors: map-merge($theme-colors, $custom-colors);
Susa kumephu
Ukuze ususe imibala ku- $theme-colors
, nanoma iyiphi enye imephu, sebenzisa map-remove
. Qaphela ukuthi kufanele uyifake phakathi kwezimfuneko zethu nezinketho:
// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
$theme-colors: map-remove($theme-colors, "info", "light", "dark");
// Optional
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc
Okhiye abadingekayo
I-Bootstrap ithatha ubukhona bokhiye abathile ngaphakathi kwamamephu e-Sass njengoba siwasebenzisile futhi sizinweba thina ngokwethu. Njengoba wenza ngendlela oyifisayo amamephu afakiwe, ungase uhlangabezane namaphutha lapho kusetshenziswa khona ukhiye othile wemephu ye-Sass.
Isibonelo, sisebenzisa i- primary
, success
, kanye danger
nokhiye abavela kuzo $theme-colors
ukuze uthole izixhumanisi, izinkinobho, nezimo zefomu. Ukushintsha amanani alaba khiye akumele kuveze izinkinga, kodwa ukuwasusa kungase kubangele izinkinga zokuhlanganisa i-Sass. Kulezi zimo, uzodinga ukushintsha ikhodi ye-Sass esebenzisa lawo manani.
Imisebenzi
Imibala
Eduze kwamamephu e-Sass esinawo, imibala yetimu ingasetshenziswa futhi njengokuguquguquka okuzimele, njenge- $primary
.
.custom-element {
color: $gray-100;
background-color: $dark;
}
Ungakhanyisa noma wenze mnyama imibala nge-Bootstrap tint-color()
kanye shade-color()
nemisebenzi. Le misebenzi izohlanganisa imibala emnyama noma emhlophe, ngokungafani nomdabu wakwa-Sass lighten()
kanye darken()
nemisebenzi ezoshintsha ukukhanya ngenani eligxilile, ngokuvamile elingalethi umphumela oyifunayo.
// Tint a color: mix a color with white
@function tint-color($color, $weight) {
@return mix(white, $color, $weight);
}
// Shade a color: mix a color with black
@function shade-color($color, $weight) {
@return mix(black, $color, $weight);
}
// Shade the color if the weight is positive, else tint it
@function shift-color($color, $weight) {
@return if($weight > 0, shade-color($color, $weight), tint-color($color, -$weight));
}
Empeleni, ungabiza umsebenzi bese udlulisela umbala nemingcele yesisindo.
.custom-element {
color: tint-color($primary, 10%);
}
.custom-element-2 {
color: shade-color($danger, 30%);
}
Ukungafani kombala
Ukuze uhlangabezane nezindinganiso zokufinyeleleka ze-WCAG 2.0 zokugqama kombala , ababhali kufanele banikeze isilinganiso sokungafani okungenani esingu-4.5:1 , ngaphandle kokuhlukile okumbalwa kakhulu.
Umsebenzi owengeziwe esiwufaka ku-Bootstrap umsebenzi wokugqama kombala, color-contrast
. Isebenzisa i- algorithm ye-WCAG 2.0 ukubala amathreshold wokugqama ngokusekelwe ekukhanyeni okuhlobene endaweni sRGB
yombala ukuze ibuyisele ngokuzenzakalelayo ukukhanya ( #fff
), okumnyama ( #212529
) noma okumnyama ( #000
) umbala wokungafani ngokusekelwe kumbala wesisekelo oshiwo. Lo msebenzi uwusizo ikakhulukazi kumamiksi noma amaluphu lapho ukhiqiza khona amakilasi amaningi.
Isibonelo, ukwenza ama-swatches ombala $theme-colors
kumephu yethu:
@each $color, $value in $theme-colors {
.swatch-#{$color} {
color: color-contrast($value);
}
}
Ingase futhi isetshenziselwe izidingo zokuqhathanisa okukodwa:
.custom-element {
color: color-contrast(#000); // returns `color: #fff`
}
Ungaphinda ucacise umbala oyisisekelo ngemisebenzi yethu yemephu yombala:
.custom-element {
color: color-contrast($dark); // returns `color: #fff`
}
Escape SVG
Sisebenzisa escape-svg
umsebenzi ukubalekela <
, >
nezinhlamvu #
zezithombe zangemuva ze-SVG. Uma usebenzisa escape-svg
umsebenzi, ama-URL wedatha kufanele acashunwe.
Engeza futhi Ususe imisebenzi
Sisebenzisa add
kanye subtract
nemisebenzi ukusonga umsebenzi we-CSS calc
. Injongo eyinhloko yale misebenzi iwukugwema amaphutha lapho inani elithi “unitless” 0
lidluliselwa calc
enkulumweni. Izinkulumo ezifana nalezi calc(10px - 0)
zizobuyisela iphutha kuzo zonke iziphequluli, ngaphandle kokuthi zilungile ngokwezibalo.
Isibonelo lapho i-calc isebenza khona:
$border-radius: .25rem;
$border-width: 1px;
.element {
// Output calc(.25rem - 1px) is valid
border-radius: calc($border-radius - $border-width);
}
.element {
// Output the same calc(.25rem - 1px) as above
border-radius: subtract($border-radius, $border-width);
}
Isibonelo lapho i-calc ingavumelekile:
$border-radius: .25rem;
$border-width: 0;
.element {
// Output calc(.25rem - 0) is invalid
border-radius: calc($border-radius - $border-width);
}
.element {
// Output .25rem
border-radius: subtract($border-radius, $border-width);
}
Imiksi
Inkomba yethu scss/mixins/
inethani lamamiksi anika amandla izingxenye ze-Bootstrap futhi ingasetshenziswa kuyo yonke iphrojekthi yakho.
Izikimu zombala
I-mixin ye-shorthand prefers-color-scheme
yombuzo wemidiya iyatholakala ngokusekelwa light
, dark
, kanye nezikimu zemibala yangokwezifiso.
@mixin color-scheme($name) {
@media (prefers-color-scheme: #{$name}) {
@content;
}
}
.custom-element {
@include color-scheme(dark) {
// Insert dark mode styles here
}
@include color-scheme(custom-named-scheme) {
// Insert custom color scheme styles here
}
}