Source

Theming Bootstrap

Customize Bootstrap 4 nrog peb cov tshiab Sass variables rau lub ntiaj teb no style nyiam rau yooj yim theming thiab tivthaiv hloov.

Taw qhia

Hauv Bootstrap 3, theming feem ntau tau tsav los ntawm qhov sib txawv ntawm qhov tsis sib xws hauv LESS, kev cai CSS, thiab cov ntsiab lus cais uas peb suav nrog hauv peb distcov ntaub ntawv. Nrog rau qee qhov kev siv zog, ib tus tuaj yeem rov tsim kho qhov zoo ntawm Bootstrap 3 yam tsis tau kov cov ntaub ntawv tseem ceeb. Bootstrap 4 muab kev paub, tab sis sib txawv me ntsis.

Tam sim no, theming tau ua tiav los ntawm Sass variables, Sass maps, thiab kev cai CSS. Tsis muaj ntau lub ntsiab lus tshwj xeeb stylesheet; Hloov chaw, koj tuaj yeem ua kom lub ntsiab lus tsim los ntxiv gradients, duab ntxoov ntxoo, thiab ntau dua.

Sass

Siv peb cov ntaub ntawv Sass los ua kom zoo dua ntawm kev sib txawv, daim duab qhia chaw, mixins, thiab lwm yam. Hauv peb qhov tsim peb tau nce qhov Sass rounding precision mus rau 6 (los ntawm lub neej ntawd nws yog 5) los tiv thaiv teeb meem nrog browser rounding.

Cov ntaub ntawv qauv

Thaum twg ua tau, zam kev hloov kho Bootstrap cov ntaub ntawv tseem ceeb. Rau Sass, uas txhais tau tias tsim koj tus kheej stylesheet uas imports Bootstrap yog li koj tuaj yeem hloov kho thiab txuas ntxiv mus. Piv txwv tias koj siv tus thawj tswj pob xws li npm, koj yuav muaj cov qauv ntaub ntawv zoo li no:

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

Yog tias koj tau rub tawm peb cov ntaub ntawv los ntawm cov ntaub ntawv thiab tsis siv tus thawj tswj pob, koj yuav xav teeb tsa ib yam dab tsi zoo ib yam li cov qauv ntawd, khaws Bootstrap cov ntaub ntawv cais tawm ntawm koj tus kheej.

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

Kev xa tawm

Hauv koj custom.scss, koj yuav import Bootstrap qhov chaw Sass cov ntaub ntawv. Koj muaj ob txoj kev xaiv: suav nrog tag nrho cov Bootstrap, lossis xaiv qhov koj xav tau. Peb txhawb cov tom kawg, txawm tias yuav tsum paub tias muaj qee qhov kev xav tau thiab kev vam khom nyob thoob plaws peb cov khoom. Koj tseem yuav tau suav qee qhov JavaScript rau peb cov plugins.

// Custom.scss
// Option A: Include all of Bootstrap

@import "../node_modules/bootstrap/scss/bootstrap";
// Custom.scss
// Option B: Include parts of Bootstrap

// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

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

Nrog rau qhov kev teeb tsa no, koj tuaj yeem pib hloov kho ib qho ntawm Sass variables thiab maps hauv koj custom.scss. Koj tuaj yeem pib ntxiv qhov seem ntawm Bootstrap hauv qab // Optionalntu raws li xav tau. Peb xav kom siv tag nrho cov ntshuam pawg los ntawm peb cov bootstrap.scssntaub ntawv raws li koj qhov chaw pib.

Hloov pauv tsis tau

Txhua qhov sib txawv Sass hauv Bootstrap 4 suav nrog tus !defaultchij tso cai rau koj hla dhau qhov sib txawv ntawm tus nqi qub hauv koj tus kheej Sass yam tsis tau hloov kho Bootstrap qhov chaws. Luam thiab muab tshuaj txhuam cov hloov pauv raws li xav tau, hloov kho lawv cov txiaj ntsig, thiab tshem tawm tus !defaultchij. Yog tias qhov hloov pauv tau raug xa mus, ces nws yuav tsis rov muab dua los ntawm qhov tseem ceeb hauv Bootstrap.

Koj yuav pom cov npe tag nrho ntawm Bootstrap qhov hloov pauv hauv scss/_variables.scss.

Kev hloov pauv tsis sib xws nyob rau hauv tib cov ntaub ntawv Sass tuaj yeem tuaj ua ntej lossis tom qab lub neej hloov pauv. Txawm li cas los xij, thaum overriding hla Sass cov ntaub ntawv, koj cov overrides yuav tsum tuaj ua ntej koj import Bootstrap's Sass cov ntaub ntawv.

Nov yog ib qho piv txwv uas hloov qhov background-colorthiab colorrau <body>thaum importing thiab compiling Bootstrap ntawm npm:

// Your variable overrides
$body-bg: #000;
$body-color: #111;

// Bootstrap and its default variables
@import "../node_modules/bootstrap/scss/bootstrap";

Rov ua dua li qhov tsim nyog rau txhua qhov hloov pauv hauv Bootstrap, suav nrog cov kev xaiv thoob ntiaj teb hauv qab no.

Maps thiab loops

Bootstrap 4 suav nrog ib tug puv tes ntawm Sass maps, tus nqi tseem ceeb uas ua rau nws yooj yim dua los tsim cov tsev neeg ntawm CSS muaj feem xyuam. Peb siv Sass maps rau peb cov xim, daim phiaj tawg, thiab lwm yam. Ib yam li Sass variables, tag nrho Sass maps suav nrog tus !defaultchij thiab tuaj yeem hla dhau thiab txuas ntxiv.

Qee qhov ntawm peb daim ntawv qhia Sass tau muab tso rau hauv qhov khoob los ntawm lub neej ntawd. Qhov no yog ua kom yooj yim nthuav dav ntawm daim ntawv qhia Sass, tab sis los ntawm tus nqi ntawm kev tshem tawm cov khoom los ntawm daim ntawv qhia me ntsis nyuaj dua.

Hloov daim ntawv qhia

Txhawm rau hloov cov xim uas twb muaj lawm hauv peb $theme-colorsdaim ntawv qhia, ntxiv cov hauv qab no rau koj cov ntaub ntawv Sass kev cai:

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

Ntxiv rau daim ntawv qhia

Txhawm rau ntxiv cov xim tshiab rau $theme-colors, ntxiv tus yuam sij tshiab thiab tus nqi:

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

Tshem tawm ntawm daim ntawv qhia

Txhawm rau tshem tawm cov xim ntawm $theme-colors, lossis lwm daim ntawv qhia, siv map-remove. Nco ntsoov tias koj yuav tsum ntxig nws ntawm peb cov kev xav tau thiab cov kev xaiv:

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

Yuav tsum tau cov yuam sij

Bootstrap xav tias muaj qee cov yuam sij tshwj xeeb hauv Sass maps thaum peb siv thiab txuas ntxiv rau peb tus kheej. Raws li koj hloov kho cov duab qhia chaw, koj yuav ntsib teeb meem qhov twg ib qho Sass daim ntawv qhia tus yuam sij raug siv.

Piv txwv li, peb siv cov primary, success, thiab cov dangeryuam sij los ntawm $theme-colorskev sib txuas, nyees khawm, thiab daim ntawv xeev. Hloov qhov tseem ceeb ntawm cov yuam sij no yuav tsum tsis muaj teeb meem, tab sis tshem tawm lawv yuav ua rau Sass compilation teeb meem. Hauv cov xwm txheej no, koj yuav tsum tau hloov kho Sass code uas ua rau siv cov txiaj ntsig ntawd.

Muaj nuj nqi

Bootstrap siv ntau lub luag haujlwm Sass, tab sis tsuas yog ib qho subset muaj feem xyuam rau cov ntsiab lus dav dav. Peb tau suav nrog peb txoj haujlwm kom tau txais txiaj ntsig los ntawm cov duab kos duab:

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

Cov no tso cai rau koj xaiv ib qho xim los ntawm Sass daim ntawv qhia ntau npaum li cas koj yuav siv cov xim txawv ntawm v3.

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

Peb kuj tseem muaj lwm txoj haujlwm kom tau txais cov xim tshwj xeeb los ntawm $theme-colorsdaim ntawv qhia. Cov qib tsis zoo yuav ua rau xim xim, thaum qib siab dua yuav tsaus.

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

Hauv kev xyaum, koj yuav hu rau qhov muaj nuj nqi thiab dhau ntawm ob qhov tsis muaj: lub npe ntawm cov xim los ntawm $theme-colors(xws li, thawj lossis txaus ntshai) thiab qib lej.

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

Cov haujlwm ntxiv tuaj yeem raug ntxiv rau yav tom ntej lossis koj tus kheej Sass kev cai los tsim cov haujlwm qib rau Sass maps ntxiv, lossis txawm tias ib qho kev ua haujlwm yog tias koj xav ua ntau yam lus.

Xim sib txawv

Ib qho haujlwm ntxiv uas peb suav nrog hauv Bootstrap yog cov xim sib txawv, color-yiq. Nws siv YIQ xim qhov chaw kom rov qab tau lub teeb ( #fff) lossis tsaus nti ( #111) sib txawv xim raws li cov xim hauv paus. Qhov kev ua haujlwm no tshwj xeeb tshaj yog muaj txiaj ntsig zoo rau mixins lossis loops uas koj tab tom tsim ntau chav kawm.

Piv txwv li, los tsim cov xim swatches los ntawm peb $theme-colorsdaim ntawv qhia:

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

Nws kuj tseem siv tau rau ib qho kev sib piv xav tau:

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

Koj tuaj yeem hais qhia lub hauv paus xim nrog peb daim ntawv qhia xim ua haujlwm:

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

Sass xaiv

Customize Bootstrap 4 nrog peb cov ntaub ntawv sib txawv ua ke thiab yooj yim toggle thoob ntiaj teb CSS nyiam nrog $enable-*Sass hloov tshiab. Override ib tug sib txawv tus nqi thiab recompile nrog npm run testraws li xav tau.

Koj tuaj yeem nrhiav thiab kho qhov hloov pauv no rau cov kev xaiv thoob ntiaj teb tseem ceeb hauv Bootstrap cov scss/_variables.scssntaub ntawv.

Hloov pauv Tus nqi Kev piav qhia
$spacer 1rem(default), lossis ib qho nqi> 0 Qhia meej tus nqi spacer default rau programmatically tsim peb spacer utilities .
$enable-rounded true(default) los yogfalse Ua kom cov border-radiusqauv uas tau hais ua ntej ntawm ntau yam khoom.
$enable-shadows truelos yog false(default) Ua kom cov box-shadowqauv uas tau hais ua ntej ntawm ntau yam khoom.
$enable-gradients truelos yog false(default) Ua kom cov gradients predefined los ntawm background-imagecov qauv ntawm ntau yam khoom.
$enable-transitions true(default) los yogfalse Enables predefined transitions ntawm ntau yam Cheebtsam.
$enable-prefers-reduced-motion-media-query true(default) los yogfalse Tso cai rau prefers-reduced-motioncov lus nug xov xwm , uas txwv tsis pub qee cov animations / kev hloov pauv raws li cov neeg siv browser / kev ua haujlwm nyiam.
$enable-hover-media-query truelos yog false(default) Deprecated
$enable-grid-classes true(default) los yogfalse Ua kom muaj cov chav kawm CSS rau cov kab sib chaws (xws li, .container, .row, .col-md-1, thiab lwm yam).
$enable-caret true(default) los yogfalse Enables pseudo element caret rau .dropdown-toggle.
$enable-print-styles true(default) los yogfalse Ua kom cov qauv rau optimizing luam ntawv.
$enable-validation-icons true(default) los yogfalse Ua kom background-imagecov cim nyob rau hauv cov ntawv nyeem thiab qee cov ntaub ntawv kev cai rau kev siv tau.

Xim

Ntau yam ntawm Bootstrap ntau yam khoom siv thiab cov khoom siv tau tsim los ntawm cov xim sib txawv hauv Sass daim ntawv qhia. Daim ntawv qhia no tuaj yeem hla dhau hauv Sass kom sai sai ua kom muaj ntau txoj cai.

Txhua xim

Txhua xim muaj nyob rau hauv Bootstrap 4, muaj raws li Sass variables thiab Sass daim ntawv qhia hauv scss/_variables.scsscov ntaub ntawv. Qhov no yuav nthuav dav ntxiv rau hauv cov kev tso tawm me me ntxiv ntxiv rau qhov ntxoov ntxoo ntxiv, zoo li cov xim grey palette peb twb suav nrog.

Xiav
Indigo
Ntshav
Paj yeeb
Liab
Txiv kab ntxwv
Daj
Ntsuab
Teal
Cyan

Nov yog qhov koj tuaj yeem siv cov no hauv koj Sass:

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

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

Cov chav kawm siv xim xim kuj muaj rau kev teeb tsa colorthiab background-color.

Nyob rau hauv lub neej yav tom ntej, peb mam li npaj muab Sass maps thiab variables rau ntxoov ntawm txhua xim raws li peb tau ua nrog cov greyscale xim hauv qab no.

Xim xim

Peb siv subset ntawm tag nrho cov xim los tsim ib tug me me xim palette rau tsim xim schemes, kuj muaj xws li Sass variables thiab ib daim ntawv qhia Sass nyob rau hauv Bootstraps cov scss/_variables.scssntaub ntawv.

Thawj
Secondary
Kev vam meej
Kev phom sij
Ceeb toom
Cov ntaub ntawv
Teeb
Tsaus

Grey

Ib qho kev nthuav dav ntawm grey variables thiab Sass daim ntawv qhia nyob rau hauv scss/_variables.scsskom zoo ib yam ntxoov grey thoob plaws koj qhov project. Nco ntsoov tias cov no yog "txias greys", uas nyiam mus rau lub suab xiav maj mam, tsis yog nruab nrab greys.

100
200
300
400
500
600
700
800
900

Tsis pub dhau scss/_variables.scss, koj yuav pom Bootstrap cov xim sib txawv thiab Sass daim ntawv qhia. Nov yog ib qho piv txwv ntawm $colorsSass map:

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

Ntxiv, tshem tawm, lossis hloov kho qhov tseem ceeb hauv daim duab qhia kev hloov kho lawv siv li cas hauv ntau lwm yam. Hmoov tsis zoo nyob rau lub sijhawm no, tsis yog txhua yam khoom siv siv daim ntawv qhia Sass no. Cov kev hloov tshiab yav tom ntej yuav siv zog los txhim kho qhov no. Txog thaum ntawd, npaj rau kev siv cov ${color}hloov pauv thiab daim ntawv qhia Sass no.

Cheebtsam

Ntau yam ntawm Bootstrap cov khoom thiab cov khoom siv hluav taws xob tau tsim nrog cov @eachvoj voog uas rov ua dua ib daim ntawv qhia Sass. Qhov no yog qhov tshwj xeeb tshaj yog pab rau kev tsim cov kev sib txawv ntawm cov khoom siv los ntawm peb $theme-colorsthiab tsim cov kev hloov pauv rau txhua qhov kev tawg. Thaum koj kho cov Sass maps thiab rov ua dua, koj yuav pom koj cov kev hloov pauv tshwm sim hauv cov voj voog no.

Hloov kho

Ntau yam ntawm Bootstrap cov khoom yog tsim nrog lub hauv paus-modifier class mus kom ze. Qhov no txhais tau hais tias feem ntau ntawm cov styling muaj nyob rau hauv cov chav kawm (piv txwv li, .btn) thaum style variations raug txwv rau cov chav kawm hloov kho (xws li, .btn-danger). Cov chav kawm hloov kho no yog tsim los ntawm $theme-colorsdaim ntawv qhia kom kho tus lej thiab lub npe ntawm peb cov chav kawm hloov kho.

Nov yog ob qho piv txwv ntawm yuav ua li cas peb voj hla $theme-colorsdaim ntawv qhia los tsim cov kev hloov pauv rau cov .alertkhoom siv thiab tag nrho peb .bg-*cov khoom siv tom qab.

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

Teb

Cov Sass loops no tsis txwv rau cov duab kos duab, ib yam nkaus. Koj tseem tuaj yeem tsim cov kev hloov pauv ntawm koj cov khoom lossis cov khoom siv. Ua piv txwv peb cov lus teb cov khoom siv sib txuas lus uas peb sib xyaw ua ke @eachrau $grid-breakpointsSass daim ntawv qhia nrog cov lus nug hauv xov xwm suav nrog.

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

Yog tias koj xav tau hloov kho koj $grid-breakpoints, koj cov kev hloov pauv yuav siv rau txhua lub voj voog rov ua dua daim ntawv qhia ntawd.

CSS variables

Bootstrap 4 suav nrog ib ncig ntawm ob lub kaum os CSS kev cai cov khoom (kuj hloov pauv) hauv nws cov CSS suav. Cov no muab kev nkag tau yooj yim rau cov txiaj ntsig zoo ib yam li peb cov ntsiab lus xim, cov ntsiab lus tawg, thiab cov font loj loj thaum ua haujlwm hauv koj tus browser tus neeg soj ntsuam, code sandbox, lossis kev tsim qauv.

Muaj variables

Nov yog cov kev hloov pauv uas peb suav nrog (nco ntsoov tias qhov :rootyuav tsum tau ua). Lawv nyob hauv peb cov _root.scssntaub ntawv.

: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, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  --font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

Piv txwv

CSS variables muaj qhov hloov pauv zoo sib xws rau Sass's variables, tab sis tsis tas yuav tsum tau muab tso ua ke ua ntej muab rau browser. Piv txwv li, ntawm no peb tab tom rov pib dua peb nplooj ntawv font thiab txuas cov qauv nrog CSS sib txawv.

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

Breakpoint variables

Thaum peb xub suav nrog cov ntsiab lus sib cais hauv peb cov CSS hloov pauv (xws li, --breakpoint-md), cov no tsis txaus siab hauv cov lus nug xov xwm , tab sis lawv tseem tuaj yeem siv tau hauv cov cai hauv cov lus nug hauv xov xwm. Cov kev hloov pauv no tseem nyob hauv CSS suav nrog rau kev rov qab sib raug zoo muab lawv tuaj yeem siv los ntawm JavaScript. Kawm ntxiv hauv spec .

Nov yog ib qho piv txwv ntawm qhov tsis txaus siab:

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

Thiab ntawm no yog ib qho piv txwv ntawm qhov kev txhawb nqa:

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