Sass ƒe nyawo
Zã míaƒe dzɔtsoƒe Sass faɛlwo nàtsɔ awɔ tɔtrɔwo, anyigbatatawo, mixins, kple dɔwɔwɔwo ŋudɔ atsɔ akpe ɖe ŋuwò nàtu kabakaba eye nàtrɔ asi le wò dɔa ŋu.
Zã míaƒe dzɔtsoƒe Sass faɛlwo nàtsɔ awɔ tɔtrɔwo, anyigbatatawo, mixins, kple bubuwo ŋudɔ.
Faɛl ƒe ɖoɖowɔwɔ
Ɣesiaɣi si wòanya wɔ la, ƒo asa na asitɔtrɔ le Bootstrap ƒe faɛl veviwo ŋu. Le Sass gome la, ema fia be nàwɔ wò ŋutɔ wò atsyãgbalẽ si xɔa Bootstrap ale be nàte ŋu atrɔ asi le eŋu ahakekee ɖe enu. Ne míetsɔe be èle package manager abe npm ene zãm la, àkpɔ faɛl ƒe ɖoɖo si le abe esia ene:
your-project/
├── scss
│ └── custom.scss
└── node_modules/
└── bootstrap
├── js
└── scss
Ne èɖe míaƒe dzɔtsoƒe faɛlwo eye mèle package manager zãm o la, àdi be yeatsɔ asi aɖo nane si sɔ kple ɖoɖo ma, si ana Bootstrap ƒe dzɔtsoƒe faɛlwo nato vovo tso tɔwò gbɔ.
your-project/
├── scss
│ └── custom.scss
└── bootstrap/
├── js
└── scss
Nusiwo wotsɔ tso duta vɛ
Le wò custom.scss
, àxɔ Bootstrap ƒe dzɔtsoƒe Sass faɛlwo. Tiatia eve le asiwò: tsɔ Bootstrap la katã de eme, alo nàtia akpa siwo nèhiã. Míele dzi dem ƒo na mamlɛtɔa, togbɔ be nyae be nudidi aɖewo kple nusiwo dzi woanɔ te ɖo le míaƒe akpawo katã me hã. Ahiã hã be nàde JavaScript aɖewo eme na míaƒe plugins.
// 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
Ne ɖoɖo ma le teƒea la, àte ŋu adze asitɔtrɔ le Sass ƒe tɔtrɔwo kple anyigbatata siwo le wò custom.scss
. Àteŋu adze Bootstrap ƒe akpa aɖewo tsɔtsɔ kpee hã gɔme le // Optional
akpaa te ne ehiã. Míeɖo aɖaŋu be nàzã import stack bliboa tso míaƒe bootstrap.scss
faɛl me abe wò gɔmedzedze ene.
Trɔtrɔ ƒe nɔnɔmewo
Sass ƒe tɔtrɔ ɖesiaɖe le Bootstrap me la de !default
aflaga si ɖe mɔ na wò be nàɖe asi le tɔtrɔ ƒe asixɔxɔ gbãtɔ ŋu le wò ŋutɔ wò Sass me evɔ màtrɔ asi le Bootstrap ƒe dzɔtsoƒe ƒe nuŋɔŋlɔ ŋu o. Kpɔ tɔtrɔwo ƒe kɔpi eye nàde wo eme alesi wòhiã, trɔ asi le woƒe asixɔxɔwo ŋu, eye nàɖe !default
aflagaa ɖa. Ne wotsɔ tɔtrɔ aɖe de asi na ame xoxo la, ekema womagagbugbɔe ade asi na to asixɔxɔ gbãtɔ siwo le Bootstrap me dzi o.
Àkpɔ Bootstrap ƒe tɔtrɔwo ƒe xexlẽdzesi bliboa le scss/_variables.scss
. Woɖo tɔtrɔ aɖewo ɖe null
, tɔtrɔ siawo meɖea nɔnɔme la doa goe o negbe ɖe woɖe wo ɖa le wò ɖoɖowɔɖi me hafi.
Ele be tɔtrɔwo ƒe tsɔtsɔme nava le míaƒe dɔwɔwɔwo ƒe tsɔtsɔ va vɔ megbe, gake do ŋgɔ na xɔxlɔ̃ mamlɛawo.
Kpɔɖeŋu aɖe si trɔa background-color
kple color
na <body>
ne wole Bootstrap xɔm hele eƒom ƒu to npm dzi lae nye esi:
// 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
Gbugbɔ gblɔ alesi wòhiã na tɔtrɔ ɖesiaɖe le Bootstrap me, si me xexeame katã ƒe tiatia siwo le ete hã le.
Anyigbatatawo kple loopwo
Bootstrap la lɔ Sass anyigbatata ʋee aɖewo ɖe eme, asixɔxɔ vevi eve siwo nana wònɔa bɔbɔe be woawɔ CSS siwo do ƒome kplii ƒe ƒomewo. Míezãa Sass ƒe anyigbatatawo na míaƒe amadedewo, grid breakpoints, kple bubuwo. Abe Sass ƒe tɔtrɔwo ene la, Sass ƒe anyigbatatawo katã me !default
aflaga la le eye woateŋu axe mɔ ɖe wo nu ahakeke wo ɖe enu.
Woƒoa míaƒe Sass anyigbatata aɖewo nu ƒu ɖe ƒuƒluwo me le gɔmedzedzea me. Wowɔa esia be woaɖe mɔ be woakeke Sass ƒe anyigbatata aɖe ɖe enu bɔbɔe, gake eva le ga si woatsɔ ana nuwo ɖeɖe le anyigbatata dzi nasesẽ vie ta.
Trɔ asi le anyigbatata ŋu
Woɖe tɔtrɔ siwo katã le $theme-colors
anyigbatataa me gɔme be wonye tɔtrɔ siwo le wo ɖokui si. Ne èdi be yeatrɔ asi le amadede aɖe si li xoxo le míaƒe $theme-colors
anyigbatata dzi ŋu la, tsɔ nusiwo gbɔna kpe ɖe wò Sass faɛl si nèwɔ ɖe ɖoɖo nu ŋu:
$primary: #0074d9;
$danger: #ff4136;
Emegbe la, woɖoa tɔtrɔ siawo ɖe Bootstrap ƒe $theme-colors
anyigbatata dzi:
$theme-colors: (
"primary": $primary,
"danger": $danger
);
Tsɔe kpe ɖe anyigbatata ŋu
Tsɔ amadede yeyewo kpe ɖe $theme-colors
, alo anyigbatata bubu ɖesiaɖe ŋu, to Sass anyigbatata yeye wɔwɔ kple wò asixɔxɔ tɔxɛwo me eye nàtsɔe aƒo ƒu kple anyigbatata gbãtɔa. Le nɔnɔme sia me la, míawɔ $custom-colors
anyigbatata yeye eye míatsɔe aƒo ƒu kple $theme-colors
.
// Create your own map
$custom-colors: (
"custom-color": #900
);
// Merge the maps
$theme-colors: map-merge($theme-colors, $custom-colors);
Ðee ɖa le anyigbatata dzi
Ne èdi be yeaɖe amadedewo ɖa le $theme-colors
, alo anyigbatata bubu ɖesiaɖe dzi la, zã map-remove
. Nyae be ele be nàtsɔe ade míaƒe nudidiwo kple tiatiawɔblɔɖewo dome:
// 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
Safui siwo hiã
Bootstrap tsɔe be safui tɔxɛ aɖewo le Sass ƒe anyigbatatawo me abe alesi míezãe eye mía ŋutɔwo míekeke esiawo ɖe enu ene. Ne èle anyigbatata siwo le eme la trɔm la, àte ŋu ado go vodadawo le afisi wole Sass ƒe anyigbatata aɖe koŋ ƒe safui zãm le.
Le kpɔɖeŋu me, míezãa primary
, success
, kple danger
safui siwo tso $theme-colors
hena kadodowo, abɔtawo, kple nuŋlɔɖi ƒe nɔnɔmewo. Mele be safui siawo ƒe asixɔxɔwo ɖɔliɖɔli nahe nya aɖeke vɛ o, gake wo ɖeɖeɖa ateŋu ahe Sass ƒe nuƒoƒoƒu ƒe kuxiwo vɛ. Le nɔnɔme siawo me la, ahiã be nàtrɔ asi le Sass ƒe kɔda si wɔa asixɔxɔ mawo ŋudɔ la ŋu.
Dɔwɔwɔwo
Amadedewo
Le Sass anyigbatata siwo le mía si la xa la, woate ŋu azã tanya ƒe amadedewo hã abe tɔtrɔ siwo le wo ɖokui si ene, abe $primary
.
.custom-element {
color: $gray-100;
background-color: $dark;
}
Àte ŋu ana amadedewo nakɔ alo ado viviti kple Bootstrap ƒe tint-color()
kple shade-color()
dɔwɔwɔwo. Dɔwɔwɔ siawo atsaka amadedewo kple yevu alo ɣi, to vovo na Sass ƒe dzɔdzɔme lighten()
kple darken()
dɔwɔwɔ siwo atrɔ kekeli la le agbɔsɔsɔ aɖe si woɖo ɖi me, si zi geɖe la, mekplɔa ame yia ŋusẽkpɔɖeamedzi si nèdi gbɔ o.
// 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));
}
Le nuwɔna me la, àyɔ dɔwɔwɔa eye nàto amadede kple kpekpeme ƒe nɔnɔmewo me.
.custom-element {
color: tint-color($primary, 10%);
}
.custom-element-2 {
color: shade-color($danger, 30%);
}
Amadede ƒe vovototo
Be woate ŋu aɖo WCAG 2.0 ƒe asixɔxɔ ƒe dzidzenuwo gbɔ na amadede ƒe vovototodedeameme , ele be agbalẽŋlɔlawo nana vovototo ƒe xexlẽme si nye 4.5:1 ya teti , negbe ʋɛ aɖewo ko koe mele eme o.
Dɔ bubu aɖe si míetsɔ de Bootstrap mee nye amadede ƒe vovototodedeameme ƒe dɔwɔwɔ, color-contrast
. Ezãa WCAG 2.0 ƒe akɔntabubu hena vovototo ƒe dzidzenuwo ƒe akɔntabubu si wotu ɖe keklẽ si sɔ gbɔ le amadede ƒe teƒe aɖe sRGB
dzi be wòatrɔ kekeli ( #fff
), viviti ( #212529
) alo yevu ( #000
) ƒe vovototo ƒe amadede le eɖokui si si wotu ɖe gɔmeɖoanyi ƒe amadede si woɖo dzi. Dɔwɔwɔ sia ɖea vi ŋutɔ na mixins alo loops afisi nèle klass geɖewo wɔm le.
Le kpɔɖeŋu me, be nàwɔ amadede ƒe nɔnɔmetatawo tso míaƒe $theme-colors
anyigbatata dzi la:
@each $color, $value in $theme-colors {
.swatch-#{$color} {
color: color-contrast($value);
}
}
Woate ŋu azãe hã na vovototodedeameme ƒe hiahiã siwo hiã zi ɖeka:
.custom-element {
color: color-contrast(#000); // returns `color: #fff`
}
Àte ŋu agblɔ gɔmeɖoanyi ƒe amadede hã kple míaƒe amadede ƒe nɔnɔmetata ƒe dɔwɔwɔwo:
.custom-element {
color: color-contrast($dark); // returns `color: #fff`
}
Si le SVG nu
Míezãa escape-svg
dɔwɔwɔa tsɔ sina le <
, >
kple #
ŋɔŋlɔdzesiwo na SVG megbe nɔnɔmetatawo. Ne èle escape-svg
dɔwɔwɔa zãm la, ele be woayɔ nyatakakawo ƒe URIwo.
Dɔwɔwɔwo Tsɔ kpe ɖe eŋu eye nàɖee ɖa
Míezãa add
kple subtract
dɔwɔwɔwo tsɔ blaa CSS calc
dɔwɔwɔa. Taɖodzinu vevitɔ si le dɔwɔwɔ siawo ŋue nye be woaƒo asa na vodadawo ne wotsɔ 0
asixɔxɔ si “maɖe ɖeka o” de calc
nyagbɔgblɔ aɖe me. Nyagbɔgblɔwo abe calc(10px - 0)
atrɔ vodada le web-browserwo katã me, togbɔ be wosɔ le akɔntabubu nu hã.
Kpɔɖeŋu si me calc la sɔ le:
$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);
}
Kpɔɖeŋu si me calc la mele eteƒe o:
$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);
}
Nusiwo wotsɔ tsakae
Míaƒe scss/mixins/
nyatakakadzraɖoƒea le mixins tɔn ɖeka si naa ŋusẽ Bootstrap ƒe akpa aɖewo eye woate ŋu azãe hã le wò ŋutɔ wò dɔa katã me.
Amadedewo ƒe ɖoɖowo
Kpuie ƒe mixin na prefers-color-scheme
media biabia la li kple kpekpeɖeŋu na light
, dark
, kple amadede ƒe ɖoɖo tɔxɛwo.
@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
}
}