Theming Bootstrap
Hazie Bootstrap 4 site na iji mgbanwe Sass ọhụrụ arụnyere n'ime ya maka mmasị ụdị ụwa maka mgbanwe isiokwu dị mfe yana mgbanwe akụrụngwa.
Okwu mmalite
Na Bootstrap 3, a na-ebute isiokwu site na mgbanwe mgbanwe na LESS, omenala CSS na ụdị isiokwu dị iche nke anyị tinyere na dist
faịlụ anyị. Site na mgbalị ụfọdụ, mmadụ nwere ike megharịa ọdịdị Bootstrap 3 kpamkpam na-enweghị imetụ faịlụ ndị bụ isi. Bootstrap 4 na-enye usoro a maara nke ọma mana ọ dịtụ iche.
Ugbu a, Sass variables, maapụ Sass, na CSS omenala na-arụzu isiokwu. Enweghị akwụkwọ ụdị isiokwu raara onwe ya nye ọzọ; Kama, ị nwere ike ime ka isiokwu arụnyere n'ime ya tinye gradients, onyinyo, na ndị ọzọ.
Sass
Jiri faịlụ Sass isi mmalite anyị nweta ohere dị iche iche, maapụ, mixins na ndị ọzọ mgbe ị na- eji pipeline akụrụngwa gị na -achịkọta Sass .
Ọdịdị faịlụ
Mgbe ọ bụla enwere ike, zere imegharị faịlụ isi Bootstrap. Maka Sass, nke ahụ pụtara ịmepụta ụdị gị nke na-ebubata Bootstrap ka ị nwee ike gbanwee ma gbasaa ya. Na-eche na ị na-eji njikwa ngwugwu dị ka npm, ị ga-enwe nhazi faịlụ nke dị ka nke a:
your-project/
├── scss
│ └── custom.scss
└── node_modules/
└── bootstrap
├── js
└── scss
Ọ bụrụ na ị budatala faịlụ isi mmalite anyị ma ị naghị eji njikwa ngwugwu, ị ga-achọ iji aka gị hazie ihe yiri usoro ahụ, debe faịlụ isi mmalite Bootstrap na nke gị.
your-project/
├── scss
│ └── custom.scss
└── bootstrap/
├── js
└── scss
Na-ebubata
Na nke gị custom.scss
, ị ga-ebubata faịlụ Sass isi mmalite Bootstrap. Ị nwere nhọrọ abụọ: tinye ihe niile Bootstrap, ma ọ bụ họrọ akụkụ ndị ị chọrọ. Anyị na-akwado nke ikpeazụ, n'agbanyeghị na mara na e nwere ụfọdụ ihe a chọrọ na adabere na akụkụ anyị. Ị ga-achọkwa itinye ụfọdụ Javascript maka plugins anyị.
// 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";
// 4. Include any optional Bootstrap components as you like
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/code";
@import "../node_modules/bootstrap/scss/grid";
Site na ntọala ahụ, ị nwere ike ịmalite gbanwee mgbanwe Sass ọ bụla na maapụ gị custom.scss
. Ị nwekwara ike ịmalite itinye akụkụ nke Bootstrap n'okpuru // Optional
ngalaba dịka ọ dị mkpa. Anyị na-atụ aro ka iji nchịkọta mbubata zuru ezu sitere na bootstrap.scss
faịlụ anyị ka mmalite gị.
Ndabara dị iche iche
Sass ọ bụla na-agbanwe na Bootstrap na-agụnye !default
ọkọlọtọ na-enye gị ohere ịkwụsị uru ndabara nke mgbanwe na Sass nke gị na-enweghị gbanwee koodu isi mmalite Bootstrap. Detuo na mado mgbanwe dị ka achọrọ, gbanwee ụkpụrụ ha, wee wepụ !default
ọkọlọtọ. Ọ bụrụ na ekenyelarị mgbanwe, mgbe ahụ, ụkpụrụ ndabara agaghị ekenye ya ọzọ na Bootstrap.
Ị ga-ahụ ndepụta zuru oke nke mgbanwe Bootstrap na scss/_variables.scss
. Adobere ụfọdụ mgbanwe na null
, mgbanwe ndị a anaghị ewepụta ihe onwunwe ma ọ bụrụ na ewepụghị ha na nhazi gị.
Mgbanwe mgbanwe ga-abịarịrị mgbe ebubata ọrụ anyị, mgbanwe na mixins, mana tupu mbubata ndị ọzọ.
Nke a bụ ọmụmaatụ na-agbanwe background-color
na color
maka <body>
mgbe ị na-ebubata ma na-achịkọta Bootstrap site na npm:
@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";
// Optional Bootstrap components here
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc
Tinyegharịa dịka ọ dị mkpa maka mgbanwe ọ bụla na Bootstrap, gụnyere nhọrọ zuru ụwa ọnụ n'okpuru.
Maapụ na loops
Bootstrap 4 gụnyere maapụ Sass ole na ole, isi uru ụzọ abụọ na-eme ka ọ dịrị mfe ịmepụta ezinụlọ CSS nwere njikọ. Anyị na-eji maapụ Sass maka agba anyị, ebe nkwụsịtụ grid na ndị ọzọ. Dịka mgbanwe Sass, maapụ Sass niile gụnyere !default
ọkọlọtọ ma enwere ike ịkagbu ya na ịgbatị.
Ụfọdụ maapụ Sass anyị jikọtara ọnụ na nke efu na ndabara. Emere nke a ka o kwe ka mgbasawanye nke maapụ Sass dị mfe, mana ọ na-abịa na ọnụ ahịa iji mee ka iwepụ ihe na maapụ sie ike karị.
Megharịa map
Ka ịgbanwee agba dị na $theme-colors
maapụ anyị, tinye ihe ndị a na faịlụ Sass gị:
$theme-colors: (
"primary": #0074d9,
"danger": #ff4136
);
Tinye na map
Ka ịgbakwunye agba ọhụrụ na $theme-colors
, tinye igodo na uru ọhụrụ:
$theme-colors: (
"custom-color": #900
);
Wepu na maapụ
Iji wepu agba na $theme-colors
, ma ọ bụ maapụ ọ bụla ọzọ, jiri map-remove
. Mara na ị ga-etinyerịrị ya n'etiti ihe achọrọ na nhọrọ anyị:
// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
$theme-colors: map-remove($theme-colors, "info", "light", "dark");
// Optional
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
...
Igodo achọrọ
Bootstrap na-eche na ọnụnọ ụfọdụ igodo dị n'ime maapụ Sass ka anyị na-eji wee gbasaa ndị a n'onwe anyị. Ka ị na-ahazi maapụ ndị agbakwunyere, ị nwere ike izute mperi ebe a na-eji igodo Sass akọwapụtara.
Dịka ọmụmaatụ, anyị na-eji igodo primary
, success
, na danger
igodo sitere na $theme-colors
njikọ, bọtịnụ, na steeti ụdị. Dochie ụkpụrụ nke igodo ndị a ekwesịghị iwepụta nsogbu ọ bụla, mana iwepu ha nwere ike bute nsogbu mkpokọta Sass. N'ọnọdụ ndị a, ị ga-achọ ịgbanwe koodu Sass nke na-eji ụkpụrụ ndị ahụ.
Ọrụ
Bootstrap na-eji ọtụtụ ọrụ Sass, mana ọ bụ naanị obere nkeji ka ọdabara na isiokwu izugbe. Anyị etinyela ọrụ atọ maka inweta ụkpụrụ site na maapụ agba:
@function color($key: "blue") {
@return map-get($colors, $key);
}
@function theme-color($key: "primary") {
@return map-get($theme-colors, $key);
}
@function gray($key: "100") {
@return map-get($grays, $key);
}
Ndị a na-enye gị ohere ịhọrọ otu agba na maapụ Sass dị ka otu ị ga-esi jiri agba agbanwe site na v3.
.custom-element {
color: gray("100");
background-color: theme-color("dark");
}
Anyị nwekwara ọrụ ọzọ iji nweta ọkwa agba site na $theme-colors
maapụ. Ụkpụrụ na-adịghị mma ga-eme ka agba dị mfe, ebe ọkwa dị elu ga-agba ọchịchịrị.
@function theme-color-level($color-name: "primary", $level: 0) {
$color: theme-color($color-name);
$color-base: if($level > 0, #000, #fff);
$level: abs($level);
@return mix($color-base, $color, $level * $theme-color-interval);
}
Na omume, ị ga-akpọ ọrụ ahụ wee gafee n'ime akụkụ abụọ: aha agba sitere na $theme-colors
(dịka, isi ma ọ bụ ihe egwu) na ọkwa ọnụọgụ.
.custom-element {
color: theme-color-level(primary, -10);
}
Enwere ike ịgbakwunye ọrụ ndị ọzọ n'ọdịniihu ma ọ bụ omenala Sass nke gị iji mepụta ọrụ dị larịị maka maapụ Sass agbakwunyere, ma ọ bụ ọbụna nke ọ bụla ma ọ bụrụ na ịchọrọ ka ị na-ekwu okwu ọnụ.
Agba dị iche
Ọrụ ọzọ anyị gụnyere na Bootstrap bụ ọrụ ọdịiche agba, color-yiq
. Ọ na-eji oghere agba YIQ weghachi ọkụ na-akpaghị aka ( #fff
) ma ọ bụ ọchịchịrị ( #111
) agba dị iche dabere na agba ntọala akọwapụtara. Ọrụ a bara uru karịsịa maka mixins ma ọ bụ loops ebe ị na-emepụta ọtụtụ klaasị.
Dịka ọmụmaatụ, iji mepụta swatches agba na $theme-colors
maapụ anyị:
@each $color, $value in $theme-colors {
.swatch-#{$color} {
color: color-yiq($value);
}
}
Enwere ike iji ya maka mkpa iche iche nke otu oge:
.custom-element {
color: color-yiq(#000); // returns `color: #fff`
}
Ị nwekwara ike ịkọwapụta agba isi na ọrụ maapụ agba anyị:
.custom-element {
color: color-yiq(theme-color("dark")); // returns `color: #fff`
}
Wepụ SVG
Anyị na-eji escape-svg
ọrụ ahụ gbanarị <
, >
na #
mkpụrụedemede maka onyonyo ndabere SVG. Ọ dị mkpa ka a gbanarị mkpụrụedemede ndị a iji nye foto ndabere na ntụgharị nke ọma. Mgbe ị na-eji escape-svg
ọrụ ahụ, a ga-ehotarịrị URI data.
Tinye na Wepụ ọrụ
Anyị na-eji add
na subtract
ọrụ kechie ọrụ CSS calc
. Ebumnuche bụ isi nke ọrụ ndị a bụ iji zere njehie mgbe 0
uru "unitless" na-agafe na calc
nkwupụta. Okwu dị ka calc(10px - 0)
ga-eweghachi mperi na ihe nchọgharị niile, n'agbanyeghị na ọ bụ eziokwu na mgbakọ na mwepụ.
Ọmụmaatụ ebe calc dị irè:
$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);
}
Ọmụmaatụ ebe calc adịghị mma:
$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);
}
Nhọrọ Sass
$enable-*
Hazie Bootstrap 4 site na iji faịlụ mgbanwe omenala arụnyere n'ime ya wee jiri mgbanwe Sass ọhụrụ gbanwee mmasị CSS zuru ụwa ọnụ . Wetuo uru nke mgbanwe wee chịkọta ya npm run test
dịka achọrọ ya.
Ị nwere ike ịchọta ma hazie mgbanwe ndị a maka isi nhọrọ zuru ụwa ọnụ na scss/_variables.scss
faịlụ Bootstrap.
Na-agbanwe agbanwe | Ụkpụrụ | Nkọwa |
---|---|---|
$spacer |
1rem (ndabere), ma ọ bụ uru ọ bụla> 0 |
Na-akọwapụta uru spacer ndabara iji mepụta ngwa spacer anyị na mmemme . |
$enable-rounded |
true (ndabere) ma ọ bụfalse |
Na-akwado ụdị akọpụtagoro border-radius na ngwa dị iche iche. |
$enable-shadows |
true ma ọ bụ false (ndabere) |
Na-akwado ụdị ịchọ mma eburu ụzọ kọwaa box-shadow n'ụdị dị iche iche. Adịghị emetụta box-shadow s eji maka steeti elekwasị anya. |
$enable-gradients |
true ma ọ bụ false (ndabere) |
Na-akwado gradients akọwapụtagoro site na background-image ụdị n'ụdị dị iche iche. |
$enable-transitions |
true (ndabere) ma ọ bụfalse |
Na-akwado s akọwapụtagoro transition na ngwa dị iche iche. |
$enable-prefers-reduced-motion-media-query |
true (ndabere) ma ọ bụfalse |
Na- prefers-reduced-motion akwado ajụjụ mgbasa ozi , nke na-egbochi ụfọdụ animation/ngbanwe dabere na mmasị nchọgharị/sistemụ arụmọrụ nke ndị ọrụ. |
$enable-hover-media-query |
true ma ọ bụ false (ndabere) |
Akwụsịla |
$enable-grid-classes |
true (ndabere) ma ọ bụfalse |
Na-akwado ọgbọ nke klaasị CSS maka sistemu grid (dịka, .container , .row , .col-md-1 , wdg). |
$enable-caret |
true (ndabere) ma ọ bụfalse |
Na-enyere aka ilekọta pseudo element na .dropdown-toggle . |
$enable-pointer-cursor-for-buttons |
true (ndabere) ma ọ bụfalse |
Tinye cursor "aka" na ihe bọtịnụ enweghị nkwarụ. |
$enable-print-styles |
true (ndabere) ma ọ bụfalse |
Na-akwado ụdị maka ịkwalite mbipụta. |
$enable-responsive-font-sizes |
true ma ọ bụ false (ndabere) |
Na -akwado nha mkpụrụedemede na-anabata . |
$enable-validation-icons |
true (ndabere) ma ọ bụfalse |
Na-akwado background-image akara ngosi n'ime ntinye ederede yana ụfọdụ ụdị omenala maka steeti nkwado. |
$enable-deprecation-messages |
true (ndabere) ma ọ bụfalse |
Tọọ iji false zoo ịdọ aka ná ntị mgbe ị na-eji ngwakọta na ọrụ ọ bụla emebisịghị emebi nke akwadoro iwepụ na v5 . |
Agba
A na-ewu ọtụtụ ihe dị iche iche na akụrụngwa Bootstrap site n'ụdị agba akọwapụtara na maapụ Sass. Enwere ike ịgbanye maapụ a na Sass ka iwepụta usoro iwu ngwa ngwa.
Agba niile
Agba niile dị na Bootstrap 4, dị ka Sass variables na maapụ Sass na scss/_variables.scss
faịlụ. A ga-agbasawanye nke a na obere mwepụta ndị na-esote iji gbakwunye ndo ndị ọzọ, dị ka palette grayscale anyị gụnyelarị.
Nke a bụ otu ị ga-esi jiri ndị a na Sass gị:
// With variable
.alpha { color: $purple; }
// From the Sass map with our `color()` function
.beta { color: color("purple"); }
klaasị ịba uru dị maka ịtọ color
na background-color
.
Agba isiokwu
Anyị na-eji akụkụ nke agba niile mepụta obere palette agba maka imepụta atụmatụ agba, dịkwa ka Sass variables na maapụ Sass na scss/_variables.scss
faịlụ Bootstrap.
Agba ntụ
Ụdị mgbanwe isi awọ sara mbara yana maapụ Sass scss/_variables.scss
maka ndo isi awọ na-agbanwe agbanwe n'ofe ọrụ gị. Rịba ama na ndị a bụ "ụda isi awọ", nke na-eche ihu ụda na-acha anụnụ anụnụ, kama ịbụ isi awọ na-anọpụ iche.
N'ime scss/_variables.scss
, ị ga-ahụ mgbanwe agba Bootstrap na maapụ Sass. Nke a bụ ọmụmaatụ nke $colors
maapụ Sass:
$colors: (
"blue": $blue,
"indigo": $indigo,
"purple": $purple,
"pink": $pink,
"red": $red,
"orange": $orange,
"yellow": $yellow,
"green": $green,
"teal": $teal,
"cyan": $cyan,
"white": $white,
"gray": $gray-600,
"gray-dark": $gray-800
) !default;
Tinye, wepụ, ma ọ bụ gbanwee ụkpụrụ dị n'ime maapụ ahụ iji melite otu esi eji ha n'ọtụtụ ihe ndị ọzọ. Ọ dị nwute na oge a, ọ bụghị akụrụngwa ọ bụla na-eji maapụ Sass a. Mmelite ga-eme n'ọdịnihu ga-agbalịsi ike imeziwanye nke a. Ruo mgbe ahụ, mee atụmatụ iji ${color}
mgbanwe ndị a na maapụ Sass a.
Ngwa
@each
Ejiri loops rụọ ọtụtụ akụrụngwa na akụrụngwa Bootstrap na- aga n'ihu maapụ Sass. Nke a na-enye aka karịsịa maka ịmepụta ụdị dị iche iche nke akụrụngwa site na anyị $theme-colors
yana ịmepụta ụdị dị iche iche na-anabata maka ebe nkwụsịtụ ọ bụla. Ka ị na-ahazi maapụ Sass ndị a wee na-achịkọta, ị ga-ahụ mgbanwe gị ozugbo na loops ndị a.
Ndị na-eme mgbanwe
A na-ewu ọtụtụ ihe ndị Bootstrap site na usoro klaasị-mgbanwe. Nke a pụtara na ọtụtụ styling dị n'ime klaasị (dịka ọmụmaatụ .btn
) ebe ụdịdị ndịiche dị na klaasị mgbanwe (dịka ọmụmaatụ, .btn-danger
). Ewubere klaasị mgbanwe ndị a site na $theme-colors
eserese ngosi iji hazie nọmba na aha klaasị mgbanwe anyị.
Nke a bụ ihe atụ abụọ nke otu anyị si elegharị $theme-colors
maapụ ahụ iji mepụta ihe ngbanwe na akụrụngwa yana akụrụngwa ndabere .alert
anyị niile ..bg-*
// Generate alert modifier classes
@each $color, $value in $theme-colors {
.alert-#{$color} {
@include alert-variant(theme-color-level($color, -10), theme-color-level($color, -9), theme-color-level($color, 6));
}
}
// Generate `.bg-*` color utilities
@each $color, $value in $theme-colors {
@include bg-variant('.bg-#{$color}', $value);
}
Na-anabata
Loops Sass ndị a ejedebeghị na maapụ agba, ma. Ị nwekwara ike iwepụta mgbanwe na-anabata nke akụrụngwa ma ọ bụ akụrụngwa gị. Were dịka ọmụmaatụ akụrụngwa nhazi ederede anyị na-anabata ebe anyị na-agwakọta @each
loop maka $grid-breakpoints
maapụ Sass yana ajụjụ mgbasa ozi gụnyere.
@each $breakpoint in map-keys($grid-breakpoints) {
@include media-breakpoint-up($breakpoint) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
.text#{$infix}-left { text-align: left !important; }
.text#{$infix}-right { text-align: right !important; }
.text#{$infix}-center { text-align: center !important; }
}
}
Ọ bụrụ na ịchọrọ ịgbanwe aha gị $grid-breakpoints
, mgbanwe gị ga-emetụta loops niile na-atụgharị n'elu maapụ ahụ.
Ụdị mgbanwe CSS
Bootstrap 4 gụnyere ihe dị ka ihe dị iche iche omenala CSS iri na abụọ na CSS achịkọtara. Ndị a na-enye ohere dị mfe iji nweta ụkpụrụ ndị a na-ejikarị eme ihe dị ka agba isiokwu anyị, ebe nkwụsịtụ, na nchịkọta mkpụrụedemede bụ isi mgbe ị na-arụ ọrụ na Inspector ihe nchọgharị gị, igbe igbe koodu, ma ọ bụ ihe ngosi izugbe.
Ụdị mgbanwe dị
Nke a bụ mgbanwe ndị anyị gụnyere (rịba ama na :root
achọrọ). Ha dị na _root.scss
faịlụ anyị.
:root {
--blue: #007bff;
--indigo: #6610f2;
--purple: #6f42c1;
--pink: #e83e8c;
--red: #dc3545;
--orange: #fd7e14;
--yellow: #ffc107;
--green: #28a745;
--teal: #20c997;
--cyan: #17a2b8;
--white: #fff;
--gray: #6c757d;
--gray-dark: #343a40;
--primary: #007bff;
--secondary: #6c757d;
--success: #28a745;
--info: #17a2b8;
--warning: #ffc107;
--danger: #dc3545;
--light: #f8f9fa;
--dark: #343a40;
--breakpoint-xs: 0;
--breakpoint-sm: 576px;
--breakpoint-md: 768px;
--breakpoint-lg: 992px;
--breakpoint-xl: 1200px;
--font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
--font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
Ihe atụ
Ụdị mgbanwe CSS na-enye ụdị mgbanwe mgbanwe nke Sass, mana na-enweghị mkpa nke nchịkọta tupu e zigara ya na ihe nchọgharị ahụ. Dịka ọmụmaatụ, ebe a, anyị na-emegharị font ibe anyị yana njikọ na mgbanwe CSS.
body {
font: 1rem/1.5 var(--font-family-sans-serif);
}
a {
color: var(--blue);
}
Ngbanwe ntụpọ
Ọ bụ ezie na anyị na mbụ tinyere ntụpọ nkwụsịtụ na mgbanwe CSS anyị (dịka, --breakpoint-md
), anaghị akwado ndị a na ajụjụ mgbasa ozi , mana enwere ike iji ha n'ime usoro iwu na ajụjụ mgbasa ozi. Ngbanwe ntụpọ ndị a na-anọgide na CSS achịkọtara maka ndakọrịta azụ n'ihi na Javascript nwere ike iji ya mee ihe. Mụtakwuo na nkọwapụta .
Nke a bụ ọmụmaatụ nke ihe anaghị akwado:
@media (min-width: var(--breakpoint-sm)) {
...
}
Ma ebe a bụ ọmụmaatụ nke ihe akwadoro:
@media (min-width: 768px) {
.custom-element {
color: var(--primary);
}
}