Skip to di men tin dɛn we de insay Skip to doks nevigishɔn
in English

Sass bin de

Yuz wi sɔs Sass fayl dɛn fɔ tek advantej pan vɛriɔbul dɛn, map dɛn, miksin dɛn, ɛn fɛnshɔn dɛn fɔ ɛp yu fɔ bil fast ɛn kɔstɔmayz yu prɔjek.

Yuz wi sɔs Sass fayl dɛn fɔ tek advantej pan vɛriɔbul dɛn, map dɛn, miksin dɛn, ɛn mɔ.

Fayl strɔkchɔ

Ɛnitɛm we i pɔsibul, nɔ chenj di kɔr fayl dɛn na Bootstrap. Fɔ Sass, dat min se yu fɔ mek yu yon staylshit we de import Bootstrap so dat yu go ebul fɔ chenj ɛn ɛkstɛnd am. If yu tek am se yu de yuz pakej manija lɛk npm, yu go gɛt fayl strɔkchɔ we tan lɛk dis:

your-project/
├── scss
│   └── custom.scss
└── node_modules/
    └── bootstrap
        ├── js
        └── scss

If yu dɔn dawnlod wi sɔs fayl dɛn ɛn yu nɔ de yuz pakej manija, yu go want fɔ sɛtup sɔntin we fiba da strɔkchɔ de wit yu an, ɛn kip Bootstrap in sɔs fayl dɛn separet frɔm yu yon.

your-project/
├── scss
│   └── custom.scss
└── bootstrap/
    ├── js
    └── scss

Impɔtant tin dɛn

Insay yu custom.scss, yu go import Bootstrap in sɔs Sass fayl dɛn. Yu gɛt tu opshɔn dɛn: put ɔl di Bootstrap, ɔ pik di pat dɛn we yu nid. Wi de ɛnkɔrej di las wan dɛn, pan ɔl we wi fɔ no se sɔm tin dɛn de we wi nid ɛn dipɛnshɔn dɛn de akɔdin to wi kɔmpɔnɛnt dɛn. Yu go nid bak fɔ put sɔm JavaSkript fɔ wi plɔgin dɛn.

// 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/root";
@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";

// 5. Add additional custom code here

Wit dat setup in ples, yu kin bigin fɔ chenj ɛni wan pan di Sass vɛriɔbul ɛn map dɛn na yu custom.scss. Yu kin bigin bak fɔ ad sɔm pat dɛn na Bootstrap ɔnda di // Optionalsɛkshɔn as nid de. Wi de advays yu fɔ yuz di ful import stak frɔm wi bootstrap.scssfayl as yu statin pɔynt.

Difɔlt dɛn we de chenj chenj

Ɛvri Sass vɛriɔbul na Bootstrap inklud di !defaultflag we de alaw yu fɔ ɔvalayz di vɛriɔbul in difɔlt valyu na yu yon Sass we yu nɔ chenj Bootstrap in sɔs kɔd. Kɔpi ɛn pas di vɛriɔbul dɛn as nid de, chenj dɛn valyu dɛn, ɛn pul di !defaultflag. If dɛn dɔn ɔlrɛdi asaynd wan vɛriɔbul, den i nɔ go ri-asayn bay di difɔlt valyu dɛn na Bootstrap.

Yu go fɛn di kɔmplit list fɔ di vayriɔbul dɛn we Bootstrap gɛt na scss/_variables.scss. Sɔm vɛriɔbul dɛn de sɛt to null, dɛn vɛriɔbul ya nɔ de autput di prɔpati pas dɛn ɔvarayd dɛn na yu kɔnfigyushɔn.

Variable ovarayd fɔ kam afta wi fɛnshɔn dɛn dɔn import, bɔt bifo di ɔda import dɛn.

Na wan ɛgzampul we de chenj di background-colorɛn colorfɔ di <body>we yu de import ɛn kɔmpilayt Bootstrap via npm:

// 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";

// Optional Bootstrap components here
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc

Ripit as nid de fɔ ɛni vɛriɔbul na Bootstrap, inklud di global opshɔn dɛn we de dɔŋ ya.

Get stat wit Bootstrap via npm wit wi stata projek! Hed to di twbs/bootstrap-npm-starter tɛmplat ripɔsitɔri fɔ si aw fɔ bil ɛn kɔstɔmayz Bootstrap na yu yon npm projɛkt. Inklud Sass kɔmpayla, Ɔtoprɛfiksa, Stayllint, PurgeCSS, ɛn Bootstrap Aykɔn dɛn.

Map ɛn lɔp dɛn

Bootstrap inklud wan hanful Sass map, ki valyu pe we de mek am izi fɔ jenarayz famili dɛn we gɛt rilayt CSS. Wi de yuz Sass map fɔ wi kɔlɔ dɛn, grid brekpoint dɛn, ɛn ɔda tin dɛn. Jɔs lɛk Sass vɛriɔbul dɛn, ɔl di Sass map dɛn gɛt di !defaultflag ɛn dɛn kin ɔvarayd ɛn ɛkstɛnd dɛn.

Sɔm pan wi Sass map dɛn kin jɔyn to ɛmti wan dɛn bay difɔlt. Dis kin bi fɔ alaw fɔ mek dɛn ebul fɔ mek wan Sass map izi wan, bɔt i kin kam wit di kɔst fɔ mek i nɔ izi fɔ pul tin dɛn na map smɔl.

Modify di map fɔ di map

Ɔl di vɛriɔbul dɛn na di $theme-colorsmap dɛn de difayn dɛn as standalɔn vɛriɔbul dɛn. Fɔ chenj wan kɔlɔ we dɔn de na wi $theme-colorsmap, ad dɛn tin ya to yu kɔstɔm Sass fayl:

$primary: #0074d9;
$danger: #ff4136;

Leta, dɛn kin sɛt dɛn vɛriɔbul ya na Bootstrap in $theme-colorsmap:

$theme-colors: (
  "primary": $primary,
  "danger": $danger
);

Ad pan di map

Ad nyu kɔlɔ dɛn to $theme-colors, ɔ ɛni ɔda map, bay we yu mek nyu Sass map wit yu kɔstɔm valyu dɛn ɛn jɔyn am wit di ɔrijinal map. If na so i bi, wi go mek wan nyu $custom-colorsmap ɛn jɔyn am wit $theme-colors.

// Create your own map
$custom-colors: (
  "custom-color": #900
);

// Merge the maps
$theme-colors: map-merge($theme-colors, $custom-colors);

Rimov am na di map

Fɔ pul di kɔlɔ dɛn na $theme-colors, ɔ ɛni ɔda map, yuz map-remove. Yu fɔ no se yu fɔ put am bitwin wi rikwaymɛnt ɛn opshɔn dɛn:

// 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";
// etc

Ki dɛn we dɛn nid

Bootstrap de tek di prezɛns fɔ sɔm spɛshal ki dɛn insay Sass map dɛn as wi de yuz ɛn ɛkstɛnd dɛn wan ya wisɛf. As yu de kɔstɔmayz di map dɛn we de insay, yu kin gɛt mistek usay dɛn de yuz wan patikyula Sass map in ki.

Fɔ ɛgzampul, wi kin yuz di primary, success, ɛn dangerki dɛn frɔm $theme-colorsfɔ link dɛn, bɔtin dɛn, ɛn fɔm stet dɛn. Fɔ riples di valyu dɛn fɔ dɛn ki dɛn ya nɔ fɔ prɛzɛnt ɛni prɔblɛm, bɔt fɔ pul dɛn kin mek Sass kɔmpilayshɔn prɔblɛm dɛn. Insay dɛn instans ya, yu go nid fɔ chenj di Sass kɔd we de mek yus to dɛn valyu dɛn de.

Di wok dɛn we dɛn de du

Kɔlɔ dɛn

Neks to di Sass map dɛn we wi gɛt, dɛn kin yuz tim kɔlɔ dɛn bak as standalɔn vɛriɔbul dɛn, lɛk $primary.

.custom-element {
  color: $gray-100;
  background-color: $dark;
}

Yu kin layt ɔ dak kɔlɔ dɛn wit Bootstrap’s tint-color()ɛn shade-color()fɛnshɔn dɛn. Dɛn fɛnshɔn ya go miks kɔlɔ wit blak ɔ wayt, nɔ lɛk Sass in nativ lighten()ɛn darken()fɛnshɔn dɛn we go chenj di laytnɛs bay wan fiks amɔnt, we bɔku tɛm nɔ kin mek di ifɛkt we yu want.

// 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));
}

In prɔsis, yu go kɔl di fɛnshɔn ɛn pas di kɔlɔ ɛn wet paramita dɛn.

.custom-element {
  color: tint-color($primary, 10%);
}

.custom-element-2 {
  color: shade-color($danger, 30%);
}

Difrɛn frɔm di kɔlɔ dɛn

Fɔ mek dɛn ebul fɔ mit di WCAG 2.0 aksesibiliti standad fɔ kɔlɔ kɔntrast , di wan dɛn we rayt di buk gi kɔntrast rɛtɛshɔn we at le 4.5:1 , wit rili smɔl ɛksɛpshɔn dɛn.

Wan ɔda wok we wi put insay Bootstrap na di kɔlɔ kɔntrast fɛnshɔn, color-contrast. I de yuz di WCAG 2.0 algɔrithm fɔ kɔlkul kɔntrast trɛshɔld dɛn bays pan rilitiv luminans insay wan sRGBkɔlɔspɛs fɔ ɔtomɛtik ritɔn wan layt ( #fff), dak ( #212529) ɔ blak ( #000) kɔntrast kɔlɔ bays pan di spɛsifikɛd bays kɔlɔ. Dis funkshɔn de yus mɔ fɔ miksin ɔ lɔp usay yu de jenarayz bɔku klas dɛn.

Fɔ ɛgzampul, fɔ mek dɛn gɛt kɔlɔ swatch dɛn frɔm wi $theme-colorsmap:

@each $color, $value in $theme-colors {
  .swatch-#{$color} {
    color: color-contrast($value);
  }
}

Yu kin yuz am bak fɔ wan-wan kɔntrast nid dɛn:

.custom-element {
  color: color-contrast(#000); // returns `color: #fff`
}

Yu kin sho bak wan bays kɔlɔ wit wi kɔlɔ map fɛnshɔn dɛn:

.custom-element {
  color: color-contrast($dark); // returns `color: #fff`
}

Escape fɔ SVG

Wi de yuz di escape-svgfɛnshɔn fɔ rɔnawe pan di <, >ɛn #aks fɔ SVG bakgrɔn pikchɔ dɛn. We yu de yuz di escape-svgfɛnshɔn, dɛn fɔ kot di data URI dɛn.

Add ɛn Subtract di fɛnshɔn dɛn

Wi de yuz di addɛn subtractfɛnshɔn dɛn fɔ rap di CSS calcfɛnshɔn. Di men rizin fɔ dɛn fɛnshɔn ya na fɔ avɔyd mistek we dɛn pas wan valyu we “yunit nɔ gɛt” 0insay wan calcɛksprɛshɔn. Ɛksprɛshɔn dɛn lɛk calc(10px - 0)go gi bak wan mistek na ɔl di brɔuza dɛn, pan ɔl we i kɔrɛkt pan matematiks.

Ɛgzampul usay di kalk valid:

$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);
}

Example usay di kalc nɔ valid:

$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);
}

Mixin dɛn

Wi scss/mixins/dairekta gɛt wan tɔn miksin dɛn we de pawa pat dɛn na Bootstrap ɛn yu kin yuz am bak akɔdin to yu yon prɔjek.

Kɔlɔ skim dɛn

Wan shɔthand mixin fɔ di prefers-color-schememidia kwɛstyɔn de wit sɔpɔt fɔ light, dark, ɛn kɔstɔm kɔlɔ skim dɛn.

@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
  }
}