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.
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 dist
cov 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.
Siv peb cov ntaub ntawv Sass los ua kom zoo dua ntawm kev sib txawv, daim duab qhia chaw, mixins, thiab lwm yam.
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:
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.
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.
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 // Optional
ntu raws li xav tau. Peb xav kom siv tag nrho cov ntshuam pawg los ntawm peb cov bootstrap.scss
ntaub ntawv raws li koj qhov chaw pib.
Txhua qhov sib txawv Sass hauv Bootstrap 4 suav nrog tus !default
chij 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 !default
chij. 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 hauv tib cov ntaub ntawv Sass tuaj yeem tuaj ua ntej lossis tom qab qhov hloov pauv tsis zoo. 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-color
thiab color
rau <body>
thaum importing thiab compiling Bootstrap ntawm npm:
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.
Bootstrap 4 suav nrog ib qho 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 !default
chij 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.
Txhawm rau hloov cov xim uas twb muaj lawm hauv peb $theme-colors
daim ntawv qhia, ntxiv cov hauv qab no rau koj cov ntaub ntawv Sass kev cai:
Txhawm rau ntxiv xim tshiab rau $theme-colors
, ntxiv tus yuam sij tshiab thiab tus nqi:
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:
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 kho cov duab qhia chaw, koj tuaj yeem ntsib qhov yuam kev uas siv Sass daim ntawv qhia tus yuam sij.
Piv txwv li, peb siv cov primary
, success
, thiab cov danger
yuam sij los ntawm $theme-colors
kev 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.
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:
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.
Peb kuj tseem muaj lwm txoj haujlwm kom tau txais cov xim tshwj xeeb los ntawm $theme-colors
daim ntawv qhia. Cov qib tsis zoo yuav ua rau xim xim, thaum qib siab dua yuav tsaus.
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.
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.
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 no muaj nuj nqi tshwj xeeb tshaj yog pab tau rau mixins lossis loops uas koj tab tom tsim ntau chav kawm.
Piv txwv li, los tsim cov xim swatches los ntawm peb $theme-colors
daim ntawv qhia:
Nws kuj tseem siv tau rau ib qho kev sib piv xav tau:
Koj tuaj yeem hais qhia lub hauv paus xim nrog peb daim ntawv qhia xim ua haujlwm:
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 test
raws 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.scss
ntaub 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-radius qauv uas tau hais ua ntej ntawm ntau yam khoom. |
$enable-shadows |
true los yog false (default) |
Ua kom cov box-shadow qauv uas tau hais ua ntej ntawm ntau yam khoom. |
$enable-gradients |
true los yog false (default) |
Ua kom cov gradients predefined los ntawm background-image cov qauv ntawm ntau yam khoom. |
$enable-transitions |
true (default) los yogfalse |
Enables predefined transition s ntawm ntau yam Cheebtsam. |
$enable-hover-media-query |
true los 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. |
Ntau yam ntawm Bootstrap ntau yam khoom 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 muaj nyob rau hauv Bootstrap 4, muaj xws li Sass variables thiab Sass daim ntawv qhia hauv scss/_variables.scss
cov ntaub ntawv. Qhov no yuav nthuav dav ntxiv nyob 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.
Nov yog qhov koj tuaj yeem siv cov no hauv koj Sass:
Cov chav kawm siv xim xim kuj muaj rau kev teeb tsa color
thiab 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.
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.scss
ntaub ntawv.
Ib qho kev nthuav dav ntawm grey variables thiab Sass daim ntawv qhia nyob rau hauv scss/_variables.scss
kom zoo ib yam ntxoov grey thoob plaws koj qhov project.
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 $colors
Sass map:
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.
Ntau ntawm Bootstrap cov khoom thiab cov khoom siv tau tsim nrog cov @each
voj voog uas rov ua dua ib daim ntawv qhia Sass. Qhov no yog qhov tshwj xeeb tshaj yog pab tau rau kev tsim cov kev sib txawv ntawm cov khoom siv los ntawm peb $theme-colors
thiab 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.
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 ib 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-colors
daim ntawv qhia kom kho tus lej thiab lub npe ntawm peb cov chav hloov kho.
Nov yog ob qho piv txwv ntawm yuav ua li cas peb voj hla $theme-colors
daim ntawv qhia los tsim cov kev hloov kho rau cov .alert
khoom siv thiab tag nrho peb .bg-*
cov khoom siv tom qab.
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 @each
rau $grid-breakpoints
Sass daim ntawv qhia nrog cov lus nug hauv xov xwm suav nrog.
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.
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.
Nov yog cov kev hloov pauv uas peb suav nrog (nco ntsoov tias qhov :root
yuav tsum tau ua). Lawv nyob hauv peb cov _root.scss
ntaub ntawv.
CSS variables muaj qhov hloov pauv zoo sib xws rau Sass qhov hloov pauv, 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.
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:
Thiab ntawm no yog ib qho piv txwv ntawm qhov kev txhawb nqa: