in English

Bootstrap

Hindura Bootstrap 4 hamwe nuburyo bushya bwubatswe muri Sass ihindagurika kumiterere yisi yose ukunda kubintu byoroshye nibihinduka.

Intangiriro

Muri Bootstrap 3, insanganyamatsiko ahanini yatwarwaga nimpinduka zirenga muri LESS, CSS yihariye, hamwe nurupapuro rwihariye rwimiterere twashizemo muri distdosiye zacu. Hamwe nimbaraga, umuntu yashoboraga guhindura rwose isura ya Bootstrap 3 idakora kuri dosiye yibanze. Bootstrap 4 itanga uburyo bumenyerewe, ariko butandukanye gato.

Noneho, insanganyamatsiko irangizwa nimpinduka za Sass, ikarita ya Sass, hamwe na CSS yihariye. Nta yindi nsanganyamatsiko yihariye; ahubwo, urashobora gukora insanganyamatsiko yubatswe kugirango wongere gradients, igicucu, nibindi byinshi.

Sass

Koresha isoko yacu ya Sass kugirango ukoreshe impinduka, amakarita, ivanga, nibindi byinshi mugihe utegura Sass ukoresheje umuyoboro wawe bwite.

Imiterere ya dosiye

Igihe cyose bishoboka, irinde guhindura dosiye yibanze ya Bootstrap. Kuri Sass, bivuze gukora urupapuro rwawe bwite rutumiza Bootstrap kugirango ubashe guhindura no kuyagura. Dufate ko ukoresha paketi umuyobozi nka npm, uzagira imiterere ya dosiye isa nkiyi:

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

Niba warakuyeho amadosiye yinkomoko kandi ukaba udakoresha pake yumuyobozi, uzakenera gushiraho intoki ikintu gisa nkiyo miterere, ukagumana dosiye yinkomoko ya Bootstrap itandukanye niyanyu.

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

Kuzana ibicuruzwa

Muriwe custom.scss, uzatumiza inkomoko ya Bootstrap ya Sass dosiye. Ufite amahitamo abiri: shyiramo Bootstrap yose, cyangwa hitamo ibice ukeneye. Turashishikariza aba nyuma, nubwo menya ko hari ibyo dusabwa hamwe nubwishingizi mubice byacu. Uzakenera kandi gushyiramo JavaScript ya plugins zacu.

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

Hamwe nuburyo bwashyizweho, urashobora gutangira guhindura icyaricyo cyose cya Sass hamwe namakarita yawe custom.scss. Urashobora kandi gutangira kongeramo ibice bya Bootstrap munsi yicyiciro // Optionalnkuko bikenewe. Turasaba gukoresha ibicuruzwa byuzuye biva muri bootstrap.scssdosiye yacu aho utangirira.

Impinduka zisanzwe

Buri Sass ihindagurika muri Bootstrap ikubiyemo !defaultibendera ryemerera kurenga agaciro gasanzwe kahinduwe muri Sass yawe bwite udahinduye code ya Bootstrap. Gukoporora no gukata impinduka nkuko bikenewe, uhindure indangagaciro, kandi ukureho !defaultibendera. Niba impinduka zimaze gutangwa, ntabwo izongera kugenwa nagaciro gasanzwe muri Bootstrap.

Uzasangamo urutonde rwuzuye rwibihinduka bya Bootstrap muri scss/_variables.scss. Impinduka zimwe zashyizweho null, izi mpinduka ntizisohora umutungo keretse iyo zirengeje urugero muburyo bwawe.

Impinduka zirenga zigomba kuza nyuma yimikorere yacu, ibihinduka, hamwe nuruvange bitumizwa mu mahanga, ariko mbere y’ibindi byatumijwe hanze.

Dore urugero ruhindura i background-colorna colorkuri <body>iyo mugihe cyo gutumiza no gukusanya Bootstrap ukoresheje 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

Subiramo nkibikenewe kubihinduka byose muri Bootstrap, harimo amahitamo yisi yose hepfo.

Tangira na Bootstrap ukoresheje npm hamwe numushinga utangira! Kujya kuri twbs / bootstrap-npm-itangira inyandikorugero yububiko kugirango urebe uko wubaka no gutunganya Bootstrap mumushinga wawe bwite wa npm. Harimo icyegeranyo cya Sass, Autoprefixer, Stylelint, PurgeCSS, na Bootstrap Udushushondanga.

Ikarita

Bootstrap 4 ikubiyemo amakarita ya Sass yintoki, ibyingenzi byingenzi byoroha kubyara imiryango ya CSS ifitanye isano. Dukoresha amakarita ya Sass kumabara yacu, grid yacitse, nibindi byinshi. Nka Sass ihinduka, amakarita yose ya Sass arimo !defaultibendera kandi arashobora kurengerwa no kwagurwa.

Amwe mu makarita yacu ya Sass yahujwe nubusa kubusa. Ibi bikorwa kugirango yemere kwaguka byoroshye ikarita ya Sass yatanzwe, ariko iza ku kiguzi cyo gukora ibintu ku ikarita bigoye cyane.

Hindura ikarita

Guhindura ibara risanzwe kurikarita yacu $theme-colors, ongeraho ibikurikira muri dosiye yawe ya Sass:

$theme-colors: (
  "primary": #0074d9,
  "danger": #ff4136
);

Ongera ku ikarita

Kongera ibara rishya kuri $theme-colors, ongeramo urufunguzo rushya nagaciro:

$theme-colors: (
  "custom-color": #900
);

Kura ku ikarita

Kuraho amabara $theme-colors, cyangwa indi karita iyo ari yo yose, koresha map-remove. Menya neza ko ugomba kubishyira hagati y'ibyo dusabwa n'amahitamo:

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

Urufunguzo rusabwa

Bootstrap ifata ko hari urufunguzo rwihariye mu ikarita ya Sass nkuko twakoresheje kandi twagura ubwacu. Mugihe uhinduye amakarita arimo, urashobora guhura namakosa aho urufunguzo rwikarita ya Sass rukoreshwa.

Kurugero, dukoresha i primary,, successna dangerurufunguzo kuva kumurongo $theme-colors, buto, na form leta. Gusimbuza indangagaciro zuru rufunguzo ntibigomba kwerekana ibibazo, ariko kubikuraho bishobora gutera ibibazo byo gukusanya Sass. Muri ibi bihe, uzakenera guhindura code ya Sass ikoresha izo ndangagaciro.

Imikorere

Bootstrap ikoresha ibikorwa byinshi bya Sass, ariko igice kimwe gusa kirakoreshwa kumutwe rusange. Twashizemo imikorere itatu yo kubona indangagaciro kuva ku ikarita y'amabara:

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

Ibi biragufasha guhitamo ibara rimwe kurikarita ya Sass nkukuntu wakoresha ibara rihinduka kuva v3.

.custom-element {
  color: gray("100");
  background-color: theme-color("dark");
}

Dufite kandi ikindi gikorwa cyo kubona urwego runaka rwamabara kurikarita $theme-colors. Urwego rubi indangagaciro zizorohereza ibara, mugihe urwego rwo hejuru ruzijimye.

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

Mu myitozo, wahamagara imikorere hanyuma ukanyuza mubice bibiri: izina ryibara kuva $theme-colors(urugero, ibanze cyangwa akaga) nurwego rwumubare.

.custom-element {
  color: theme-color-level(primary, -10);
}

Ibikorwa byinyongera birashobora kongerwaho mugihe kizaza cyangwa kugiti cyawe Sass kugirango ukore ibikorwa byurwego rwikarita yinyongera ya Sass, cyangwa nibisanzwe niba ushaka kuba inshinga nyinshi.

Itandukaniro ryamabara

Igikorwa cyinyongera dushyira muri Bootstrap nigikorwa cyo gutandukanya ibara , color-yiq. Ikoresha umwanya wibara rya YIQ kugirango uhite usubiza urumuri ( #fff) cyangwa umwijima ( #111) rutandukanye rushingiye kumabara yibanze. Iyi mikorere ni ingirakamaro cyane cyane kuvanga cyangwa kuzenguruka aho urimo kubyara ibyiciro byinshi.

Kurugero, kubyara amabara yaturutse ku $theme-colorsikarita yacu:

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

Irashobora kandi gukoreshwa muburyo bumwe butandukanye:

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

Urashobora kandi kwerekana ibara shingiro hamwe nibikorwa byikarita yamabara:

.custom-element {
  color: color-yiq(theme-color("dark")); // returns `color: #fff`
}

Hunga SVG

Dukoresha escape-svgimikorere kugirango duhunge i <, >ninyuguti #za SVG amashusho yinyuma. Izi nyuguti zigomba guhunga kugirango zitange neza amashusho yinyuma muri IE. Iyo ukoresheje escape-svgimikorere, amakuru URIs agomba gusubirwamo.

Ongeraho no Gukuramo imirimo

Dukoresha i addn'imikorere subtractyo gupfunyika calcimikorere ya CSS. Intego yibanze yiyi mikorere nukwirinda amakosa mugihe agaciro "kitagira ubumwe" kanyujijwe 0mumvugo calc. Imvugo nka calc(10px - 0)izasubiza ikosa muri mushakisha zose, nubwo ari imibare.

Urugero aho kubara bifite ishingiro:

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

Urugero aho kubara bitemewe:

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

Amahitamo ya Sass

Hindura Bootstrap 4 hamwe na fayili yacu yubatswe yihariye kandi uhindure byoroshye CSS yisi yose hamwe nibihinduka bishya bya $enable-*Sass. Kurenga agaciro k'ibihinduka hanyuma usubiremo npm run testnkuko bikenewe.

Urashobora kubona no guhitamo ibyo bihinduka kubintu byingenzi byingenzi byisi muri scss/_variables.scssdosiye ya Bootstrap.

Birahinduka Indangagaciro Ibisobanuro
$spacer 1rem(Mburabuzi), cyangwa agaciro ako ari ko kose> 0 Kugaragaza agaciro gasanzwe kamaro kugirango gahunda itange umusaruro wibikoresho byacu .
$enable-rounded true(Mburabuzi) cyangwafalse Gushoboza uburyo bwateganijwe mbere border-radiuskubintu bitandukanye.
$enable-shadows truecyangwa false(Mburabuzi) Gushoboza uburyo bwo gushushanya mbere yuburyo box-shadowbutandukanye. Ntabwo bigira ingaruka box-shadows zikoreshwa kuri leta yibanze.
$enable-gradients truecyangwa false(Mburabuzi) Gushoboza gradients zateganijwe binyuze background-imagemuburyo butandukanye.
$enable-transitions true(Mburabuzi) cyangwafalse Gushoboza transitions byateganijwe mbere kubice bitandukanye.
$enable-prefers-reduced-motion-media-query true(Mburabuzi) cyangwafalse Gushoboza prefers-reduced-motionitangazamakuru kubaza , rihagarika animasiyo / inzibacyuho zishingiye kuri mushakisha y'abakoresha / sisitemu y'imikorere.
$enable-hover-media-query truecyangwa false(Mburabuzi) Gutesha agaciro
$enable-grid-classes true(Mburabuzi) cyangwafalse Gushoboza ibisekuru bya CSS ya sisitemu ya gride (urugero ,,,, .containernibindi .row) .col-md-1.
$enable-caret true(Mburabuzi) cyangwafalse Gushoboza pseudo element caret kuri .dropdown-toggle.
$enable-pointer-cursor-for-buttons true(Mburabuzi) cyangwafalse Ongeramo "ukuboko" indanga kubintu bitari ubumuga buto.
$enable-print-styles true(Mburabuzi) cyangwafalse Gushoboza uburyo bwo guhitamo icapiro.
$enable-responsive-font-sizes truecyangwa false(Mburabuzi) Gushoboza ingano yimyandikire .
$enable-validation-icons true(Mburabuzi) cyangwafalse Gushoboza background-imageamashusho mumyandikire yinyandiko hamwe nuburyo bwihariye bwo kwemeza leta.
$enable-deprecation-messages true(Mburabuzi) cyangwafalse Shiraho falseguhisha umuburo mugihe ukoresheje ikintu icyo ari cyo cyose kivanze n'imikorere iteganijwe gukurwaho v5.

Ibara

Byinshi mubintu bitandukanye bya Bootstrap nibikorwa byubatswe binyuze murukurikirane rwamabara asobanurwa mukarita ya Sass. Ikarita irashobora kuzunguruka muri Sass kugirango byihute kubyara urukurikirane rw'amategeko.

Amabara yose

Amabara yose aboneka muri Bootstrap 4, arahari nkibihinduka bya Sass n'ikarita ya Sass muri scss/_variables.scssdosiye. Ibi bizagurwa mugihe gito gisohotse kugirango hongerwemo igicucu, cyane nka palette palette dusanzwe dushyiramo.

$ ubururu # 007bff
$ indigo # 6610f2
$ ibara ry'umuyugubwe # 6f42c1
$ pink # e83e8c
$ umutuku # dc3545
$ orange # fd7e14
$ umuhondo # ffc107
$ icyatsi # 28a745
$ icyayi # 20c997
$ cyan # 17a2b8

Dore uko ushobora gukoresha ibi muri Sass yawe:

// With variable
.alpha { color: $purple; }

// From the Sass map with our `color()` function
.beta { color: color("purple"); }

Amabara yingirakamaro yamasomo nayo arahari mugushiraho colorna background-color.

Mugihe kizaza, tuzagerageza gutanga amakarita ya Sass nibihinduka kubicucu bya buri bara nkuko twabikoze hamwe namabara ya graycale hepfo.

Amabara yibanze

Dukoresha igice cyamabara yose kugirango dukore ibara rito palette yo kubyara amabara, nayo iraboneka nka Sass variable hamwe nikarita ya Sass muri scss/_variables.scssdosiye ya Bootstrap.

$ ibanze # 007bff
$ kabiri # 6c757d
$ intsinzi # 28a745
$ akaga # dc3545
$ kuburira # ffc107
$ amakuru # 17a2b8
$ urumuri # f8f9fa
$ umwijima # 343a40

Icyatsi

Ikirangantego cyagutse cyimyenda ihindagurika hamwe nikarita ya Sass muburyo scss/_variables.scssbwigicucu kijimye cyumusatsi kumushinga wawe. Menya ko ibyo ari “ibara ryiza”, ryerekeza ku ijwi ryoroshye ry'ubururu, aho kuba ibara ridafite aho ribogamiye.

$ gray-100 # f8f9fa
$ gray-200 # e9ecef
$ gray-300 # dee2e6
$ gray-400 # ced4da
$ gray-500 # adb5bd
$ gray-600 # 6c757d
$ gray-700 # 495057
$ gray-800 # 343a40
$ gray-900 # 212529

Imbere scss/_variables.scss, uzasangamo ibara rya Bootstrap hamwe namakarita ya Sass. Dore urugero $colorsrw'ikarita ya 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;

Ongeraho, ukureho, cyangwa uhindure indangagaciro mukarita kugirango uvugurure uko zikoreshwa mubindi bice byinshi. Kubwamahirwe muriki gihe, ntabwo buri kintu cyose gikoresha ikarita ya Sass. Ibihe bizaza bizaharanira kunoza ibi. Kugeza icyo gihe, teganya gukoresha ${color}impinduka n'iyi karita ya Sass.

Ibigize

Byinshi mubigize Bootstrap nibikoresho byubatswe byubatswe hamwe @eachnibisubirwamo hejuru yikarita ya Sass. Ibi bifasha cyane cyane kubyara ibice bigize ibice byacu $theme-colorsno gukora impinduka zishubije kuri buri cyiciro. Mugihe uhinduye amakarita ya Sass hanyuma ukayasubiramo, uzahita ubona impinduka zawe zigaragara muriyi mirongo.

Abahindura

Byinshi mubice bya Bootstrap byubatswe hamwe na base-modifier class approach. Ibi bivuze ko igice kinini cyimyandikire gikubiye mubyiciro fatizo (urugero, .btn) mugihe imiterere itandukanye igarukira kumasomo yo guhindura (urugero, .btn-danger). Aya masomo yo guhindura yubatswe kuva ku $theme-colorsikarita kugirango uhindure umubare nizina ryibyiciro byacu byo guhindura.

Hano hari ingero ebyiri zuburyo twazengurutse $theme-colorsikarita kugirango tubyare abahindura .alertibice hamwe nibikorwa byacu byose byingenzi .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);
}

Igisubizo

Utuzingo twa Sass ntabwo tugarukira ku ikarita y'amabara, haba. Urashobora kandi kubyara itandukaniro ryibisubizo byibigize cyangwa ibikorwa byawe. Fata nkurugero rwibisubizo byingirakamaro byingirakamaro aho tuvanga ikarita yikarita @eachya $grid-breakpointsSass hamwe nibibazo byitangazamakuru birimo.

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

Ukeneye guhindura ibyawe $grid-breakpoints, impinduka zawe zizakoreshwa kumuzingo wose uzenguruka kuri iyo karita.

Impinduka za CSS

Bootstrap 4 ikubiyemo ibintu bibiri bya CSS byihariye (variable) muri CSS yakozwe. Ibi bitanga uburyo bworoshye bwo kubona indangagaciro zikoreshwa nkamabara yinsanganyamatsiko, gucamo, hamwe nimyandikire yibanze mugihe ukora muri Inspecteur ya mushakisha yawe, agasanduku kode, cyangwa prototyping rusange.

Ibihinduka biboneka

Dore impinduka dushyiramo (menya ko :rootibisabwa). Biri muri _root.scssdosiye yacu.

: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;
}

Ingero

Impinduka za CSS zitanga ihinduka risa nimpinduka za Sass, ariko bidakenewe gukusanya mbere yo guhabwa mushakisha. Kurugero, hano turimo gusubiramo imyandikire yurupapuro rwacu hamwe nuburyo bwo guhuza hamwe na CSS ihinduka.

body {
  font: 1rem/1.5 var(--font-family-sans-serif);
}
a {
  color: var(--blue);
}

Impinduka zinyuranye

Mugihe twabanje gushyiramo ibice byahinduwe muri CSS yacu (urugero, --breakpoint-md), ibyo ntibishyigikiwe mubibazo byitangazamakuru , ariko birashobora gukoreshwa mumategeko mubibazo byitangazamakuru. Izi mpinduka zinyuranye ziguma muri CSS yakozwe kugirango ihuze inyuma bitewe nuko ishobora gukoreshwa na JavaScript. Wige byinshi muri sp .

Dore urugero rwibidashyigikiwe:

@media (min-width: var(--breakpoint-sm)) {
  ...
}

Kandi dore urugero rwibyashyigikiwe:

@media (min-width: 768px) {
  .custom-element {
    color: var(--primary);
  }
}