in English

Theming Bootstrap

Gadzirisa Bootstrap 4 neyedu itsva yakavakirwa-muSass machinjiro epasirese masitaera aunofarira kuti zvive nyore theming uye chikamu chekuchinja.

Nhanganyaya

MuBootstrap 3, theming yainyanya kufambiswa nekusiyana-siyana muLESS, tsika CSS, uye yakaparadzana theme stylesheet yatakaisa distmumafaira edu. Nekumwe kuedza, munhu anogona kugadzirisa zvachose chitarisiko cheBootstrap 3 pasina kubata mafaera epakati. Bootstrap 4 inopa yakajairika, asi yakati siyanei maitiro.

Ikozvino, theming inopedzwa neSass akasiyana, Sass mepu, uye tsika CSS. Hapasisina yakatsaurirwa theme stylesheet; pachinzvimbo, unogona kugonesa yakavakirwa-mukati theme kuti uwedzere gradients, mimvuri, uye nezvimwe.

Sass

Shandisa yedu sosi mafaera eSass kutora mukana wezvakasiyana, mepu, musanganiswa, uye nezvimwe paunenge uchigadzira Sass uchishandisa yako wega pombi.

Chimiro chefaira

Pese pazvinogoneka, dzivirira kugadzirisa mafaira eBootstrap. Kune Sass, zvinoreva kugadzira yako wega stylesheet inopinza Bootstrap kuti ugone kuigadzirisa nekuiwedzera. Kufungidzira kuti uri kushandisa pasuru maneja senge npm, iwe unenge uine faira chimiro chinotaridzika seizvi:

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

Kana iwe wakadhawunirodha yedu sosi mafaera uye usingashandise pasuru maneja, iwe unenge uchida kumisikidza nemaoko chimwe chinhu chakafanana neiyo chimiro, uchichengeta Bootstrap's sosi mafaera akasiyana kubva kune ako.

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

Importing

Mune yako custom.scss, iwe unopinza Bootstrap's source Sass mafaera. Iwe une mbiri sarudzo: sanganisira ese eBootstrap, kana tora zvikamu zvaunoda. Tinokurudzira vekupedzisira, asi ziva kuti pane zvimwe zvinodiwa uye zvinotsamira pazvikamu zvedu. Iwe zvakare unozofanirwa kusanganisira imwe JavaScript yemapulagi edu.

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

Nekuseta ikoko munzvimbo, unogona kutanga kugadzirisa chero maSass akasiyana uye mepu mune yako custom.scss. Iwe unogona zvakare kutanga kuwedzera zvikamu zveBootstrap pasi // Optionalpechikamu sezvinodiwa. Isu tinopa zano kushandisa iyo yakazara yekupinza stack kubva bootstrap.scsskufaira redu senzvimbo yako yekutanga.

Variable defaults

Yese Sass inosiyana muBootstrap inosanganisira iyo !defaultmureza inokubvumidza kuti uwedzere kukosha kwekusiyana kweiyo Sass yako pasina kugadzirisa Bootstrap's source code. Kopa uye unamate zvinosiyana sezvinodiwa, shandura maitiro avo, uye bvisa !defaultmureza. Kana shanduko yakatopihwa, saka haizopihwazve neiyo default tsika muBootstrap.

Iwe uchawana iyo yakazara runyorwa rweBootstrap's akasiyana mu scss/_variables.scss. Mamwe mavhezheni akaiswa ku null, aya akasiyana haabudise chivakwa kunze kwekunge akadarika mukumisikidzwa kwako.

Zvinosiyana-siyana zvinofanirwa kuuya mushure mekunge mabasa edu, zvinosiyana, uye musanganiswa zvaunzwa kunze kwenyika, asi pamberi pezvimwe zvekunze.

Heino muenzaniso unoshandura iyo background-coloruye coloryeiyo <body>kana uchipinza uye kunyora Bootstrap kuburikidza 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

Dzokorora sezvinodiwa kune chero shanduko muBootstrap, kusanganisira sarudzo dzepasirese pazasi.

Tanga neBootstrap kuburikidza nenpm neyekutanga chirongwa chedu! Musoro kune twbs/bootstrap-npm-starter template repository kuti uone maitiro ekuvaka uye kugadzirisa Bootstrap mune yako wega npm purojekiti. Inosanganisira Sass compiler, Autoprefixer, Stylelint, PurgeCSS, uye Bootstrap Icons.

Mepu uye zvishwe

Bootstrap 4 inosanganisira mashoma emamepu eSass, akakosha mapeya anoita kuti zvive nyore kugadzira mhuri dzeCSS ine hukama. Isu tinoshandisa mamepu eSass kune edu mavara, grid breakpoints, nezvimwe. Sezvakangoita maSass akasiyana, ese maSass mepu anosanganisira !defaultmureza uye anogona kudhindwa nekuwedzerwa.

Mamwe emamepu edu eSass anobatanidzwa kuita asina chinhu nekukasira. Izvi zvinoitirwa kubvumira kuwedzera kuri nyore kwemepu yeSass yakapihwa, asi zvinouya nemubhadharo wekugadzira kubvisa zvinhu kubva pamepu kuoma zvishoma.

Shandura mepu

Kuti ugadzirise ruvara rwuripo $theme-colorspamepu yedu, wedzera zvinotevera kune yako tsika Sass faira:

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

Wedzera kumepu

Kuti uwedzere ruvara rutsva ku $theme-colors, wedzera kiyi nyowani uye kukosha:

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

Bvisa kubva pamepu

Kubvisa mavara kubva $theme-colors, kana chero imwe mepu, shandisa map-remove. Ziva kuti unofanirwa kuiisa pakati pezvatinoda uye sarudzo:

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

Makiyi anodiwa

Bootstrap inotora kuvapo kwemamwe makiyi mukati meSass mepu sezvataishandisa uye kuwedzera iwo pachedu. Sezvo iwe uchigadzirisa mamepu anosanganisirwa, unogona kusangana nezvikanganiso apo chaiyo Sass mepu kiyi iri kushandiswa.

Semuenzaniso, isu tinoshandisa iyo primary, success, uye dangermakiyi kubva $theme-colorskune zvinongedzo, mabhatani, uye fomu nyika. Kutsiva kukosha kwemakiyi aya hakufanire kuratidza nyaya, asi kubvisa kunogona kukonzera Sass kuunganidza nyaya. Muzviitiko izvi, iwe unozofanirwa kugadzirisa iyo Sass kodhi inoshandisa iwo maitiro.

Mabasa

Bootstrap inoshandisa akati wandei maSass mabasa, asi chete subset inoshanda kune general theming. Isu takabatanidza mabasa matatu ekutora kukosha kubva kumamepu emavara:

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

Izvi zvinokutendera kuti usarudze rumwe ruvara kubva pamepu yeSass zvakanyanya semashandisiro aungaita ruvara rwakasiyana kubva kuv3.

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

Isu tine rimwe basa rekuwana imwe nhanho yeruvara kubva $theme-colorspamepu. Negative level values ​​inorerutsa ruvara, ukuwo mazinga epamusoro achisviba.

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

Mukuita, iwe waizodaidza basa racho uye wopfuura muzvikamu zviviri: zita reruvara kubva $theme-colors(semuenzaniso, chekutanga kana njodzi) uye nhamba yenhamba.

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

Mamwe mabasa anogona kuwedzerwa mune ramangwana kana yako tsika Sass kugadzira nhanho mashandiro emamwe mamepu eSass, kana generic kana iwe uchida kuve wakanyanya verbose.

Kusiyana kwemavara

Rimwe basa ratinosanganisa muBootstrap ndiro basa rekusiyanisa ruvara, color-yiq. Inoshandisa iyo YIQ nzvimbo yemavara kuti idzorere otomatiki mwenje ( #fff) kana yakasviba ( #111) musiyano weruvara unoenderana neyakatsanangurwa base color. Iri basa rinonyanya kubatsira kumamikisi kana zvishwe pauri kugadzira akawanda makirasi.

Semuenzaniso, kugadzira mavara emavara kubva $theme-colorspamepu yedu:

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

Inogona zvakare kushandiswa kune imwe-off mutsauko zvinodiwa:

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

Iwe unogona zvakare kutsanangura ruvara rwepasi nemepu yedu yemavara mabasa:

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

Escape SVG

Isu tinoshandisa escape-svgbasa kutiza iyo <, >uye #mavara eSVG kumashure mifananidzo. Aya mavara anofanirwa kupukunyuka kuti ape nemazvo mifananidzo yekumashure muIE. Paunenge uchishandisa escape-svgbasa, data URIs inofanirwa kutorwa.

Wedzera uye Bvisa mabasa

Isu tinoshandisa iyo adduye subtractmabasa kuputira iyo CSS calcbasa. Chinangwa chikuru chemabasa aya ndechekudzivirira kukanganisa kana 0kukosha kwe "unitless" kuchiiswa muchirevo calc. Matauriro akadai calc(10px - 0)anodzosa chikanganiso mumabhurawuza ese, kunyangwe ari masvomhu chaiwo.

Muenzaniso apo calc inoshanda:

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

Muenzaniso apo calc isiri kushanda:

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

Sass sarudzo

Gadzirisa Bootstrap 4 neyedu yakavakirwa-mukati dhizaini faira uye shandura zviri nyore zvepasirese CSS zvido nemhando itsva $enable-*dzeSass. Bvisa kukosha kweiyo vhezheni uye wobatanidza nayo npm run testsezvinodiwa.

Iwe unogona kuwana uye kugadzirisa aya machinjiro kune akakosha epasi rose sarudzo muBootstrap's scss/_variables.scssfaira.

Variable Values Tsanangudzo
$spacer 1rem(default), kana chero kukosha> 0 Inodoma iyo default spacer kukosha kuti igadzire yedu spacer utilities .
$enable-rounded true(default) kanafalse Inogonesa border-radiuszvitaera zvakafanotsanangurwa pazvikamu zvakasiyana.
$enable-shadows truekana false(default) Inogonesa zvakafanotsanangurwa zvekushongedza box-shadowzvitaera pane zvakasiyana zvikamu. Haina kukanganisa box-shadows inoshandiswa kune inotarisisa nyika.
$enable-gradients truekana false(default) Inogonesa kufanotsanangurwa gradients kuburikidza background-imagenezvitaera pane akasiyana mazvikamu.
$enable-transitions true(default) kanafalse Inogonesa predefined transitions pazvikamu zvakasiyana.
$enable-prefers-reduced-motion-media-query true(default) kanafalse Inogonesa prefers-reduced-motionkubvunza kwenhau , iyo inodzvanya mamwe maanimation/shanduko zvichibva pane zvevashandisi'browser/operating system zvido.
$enable-hover-media-query truekana false(default) Yakarambwa
$enable-grid-classes true(default) kanafalse Inogonesa kugadzirwa kweCSS makirasi egidhi system (semuenzaniso, .container, .row, .col-md-1, nezvimwewo).
$enable-caret true(default) kanafalse Inogonesa pseudo element caret pa .dropdown-toggle.
$enable-pointer-cursor-for-buttons true(default) kanafalse Wedzera "ruoko" chitubu kune zvisina kuremara bhatani zvinhu.
$enable-print-styles true(default) kanafalse Inogonesa masitaera ekugonesa kudhinda.
$enable-responsive-font-sizes truekana false(default) Inobvumira mafonti saizi anopindura .
$enable-validation-icons true(default) kanafalse Inogonesa ma background-imageicons mukati memavara ekuisa uye mamwe mafomu ega ega enzvimbo dzekusimbisa.
$enable-deprecation-messages true(default) kanafalse Gadzirira falsekuviga yambiro kana uchishandisa chero yakarasika misanganiswa uye mabasa akarongwa kubviswa mu v5.

Color

Zvizhinji zveBootstrap zvakasiyana-siyana zvikamu uye zvishandiso zvinovakwa kuburikidza neakatevedzana emavara anotsanangurwa mumepu yeSass. Iyi mepu inogona kuvharirwa muSass kuti ikurumidze kugadzira nhevedzano yemitemo.

Mavara ose

Ese mavara anowanikwa muBootstrap 4, anowanikwa seSass akasiyana uye mepu yeSass scss/_variables.scssmufaira. Izvi zvichawedzerwa pane anotevera madiki ekuburitswa kuti awedzere mamwe mimvuri, senge greyscale palette yatinotobatanidza .

$blue #007bff
$indigo #6610f2
$pepuru #6f42c1
$pingi #e83e8c
$tsvuku #dc3545
$Orenji #fd7e14
$yellow #ffc107
$girini #28a745
$teal #20c997
$cyan #17a2b8

Heano maitiro aungashandisa aya muSass yako:

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

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

Makirasi ekushandisa mavara anowanikwawo pakuseta coloruye background-color.

Mune ramangwana, isu tichavavarira kupa maSass mamepu uye akasiyana emhando dzemavara ega ega sezvatakaita ne grayscale mavara pazasi.

Theme mavara

Isu tinoshandisa subset yemavara ese kugadzira diki pendi yeruvara kugadzira marongero emavara, inowanikwawo semaSass akasiyana uye mepu yeSass muBootstrap's scss/_variables.scssfile.

$primary #007bff
$secondary #6c757d
$ kubudirira #28a745
$ngozi #dc3545
$yambiro #ffc107
$info #17a2b8
$ chiedza #f8f9fa
$rima #343a40

Grays

Iyo yakawedzera seti yegrey akasiyana uye mepu yeSass mune scss/_variables.scssinowirirana mimvuri yegrey pane yako purojekiti. Ziva kuti aya ndiwo "greys anotonhorera", ayo anotarisa kune yakasarudzika yebhuruu toni, pane kusarerekera grey.

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

Mukati me scss/_variables.scss, iwe unowana Bootstrap's mavara akasiyana uye Sass mepu. Heino muenzaniso $colorsweSass mepu:

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

Wedzera, bvisa, kana shandura kukosha mukati memepu kuti uvandudze mashandisirwo aanoitwa mune zvimwe zvikamu zvakawanda. Nehurombo panguva ino, hazvisi zvese zvikamu zvinoshandisa iyi mepu yeSass. Zvidzoreso zveramangwana zvichaedza kuvandudza pane izvi. Kusvika panguva iyoyo, ronga pakushandisa ${color}zvinosiyana uye iyi mepu yeSass.

Zvikamu

Zvizhinji zvezvinhu zveBootstrap uye zvinoshandiswa zvinovakwa @eachnezvishwe zvinodzokorora pamusoro pemepu yeSass. Izvi zvinonyanya kubatsira pakugadzira mutsauko wechikamu neyedu $theme-colorsuye kugadzira anopindura akasiyana ega ega breakpoint. Sezvo iwe uchigadzirisa aya mamepu eSass uye kudzoreredza, iwe unozoona otomatiki shanduko dzako dzichiratidzwa mune izvi zvishwe.

Modifiers

Zvizhinji zvezvinhu zveBootstrap zvakavakwa ne base-modifier kirasi maitiro. Izvi zvinoreva kuti huwandu hwemataera huri mukirasi yepasi (semuenzaniso, .btn) nepo masitaera akasiyana achingogumira kumakirasi ekugadzirisa (semuenzaniso, .btn-danger). Aya makirasi ekugadzirisa akavakwa kubva $theme-colorspamepu kuti agadzirise nhamba uye zita remakirasi edu ekugadzirisa.

Heino mienzaniso miviri yemabatiro atinoita pamusoro $theme-colorspemepu kuti tigadzire ma modifiers kuchikamu .alertuye zvese zvedu .bg-*zvemashure zvinoshandiswa.

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

Anoteerera

Aya maSass loops haana kugumira kumamepu emavara, kana. Iwe unogona zvakare kugadzira inopindura misiyano yezvikamu zvako kana zvishandiso. Tora semuenzaniso yedu inopindura mameseji ekugadzirisa mameseji kwatinosanganisa @eachloop $grid-breakpointsyemepu yeSass nemubvunzo wenhau unosanganisira.

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

Kana iwe uchifanira kugadzirisa yako $grid-breakpoints, shanduko dzako dzinoshanda kune ese zvishwe zvinodzokorora pamusoro pemepu iyoyo.

CSS zvakasiyana

Bootstrap 4 inosanganisira anenge gumi nemaviri eCSS tsika zvivakwa (zvinosiyana) mune yayo yakanyorwa CSS. Izvi zvinopa nyore kuwana kune anowanzo shandiswa kukosha senge yedu theme mavara, mabreakpoints, uye ekutanga font stacks paunenge uchishanda mubrowser's Inspector, kodhi sandbox, kana general prototyping.

Available variables

Heano mavara atinosanganisa (ona kuti izvo :rootzvinodiwa). Anowanikwa _root.scssmufaira redu.

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

Mienzaniso

CSS zvinosiyana zvinopa kuchinjika kwakafanana kune maSass akasiyana, asi pasina kudikanwa kwekubatanidza isati yapihwa kune browser. Semuenzaniso, pano tiri kusetazve mafonti epeji redu uye masitaera ekubatanidza ane CSS zvinosiyana.

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

Breakpoint variables

Nepo isu pakutanga takabatanidza mabreakpoints mune yedu CSS akasiyana (eg, --breakpoint-md), aya haatsigirwe mumibvunzo midhiya , asi anogona achiri kushandiswa mukati memitemo mumibvunzo yenhau. Aya mabreakpoint akasiyana anoramba ari muCSS yakaunganidzwa yekudzokera kumashure inopihwa iyo inogona kushandiswa neJavaScript. Dzidza zvakawanda mune spec .

Heino muenzaniso wezvisina kutsigirwa:

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

Uye heino muenzaniso wezvinotsigirwa:

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