Theming Bootstrap
Yenza ngokwezifiso i-Bootstrap yesi-4 ngezinto zethu ezintsha ezakhelwe ngaphakathi ze-Sass zokhetho lwesitayile sehlabathi ukulungiselela umxholo olula kunye notshintsho lwamacandelo.
Kwi-Bootstrap 3, i-theming yayiqhutywa ubukhulu becala kukudlula okuguquguqukayo kwi-LESS, i-CSS yesiko, kunye necwecwe lesitayile somxholo owahlukileyo esiwufake dist
kwiifayile zethu. Ngomzamo othile, umntu unokuphinda ahlengahlengise ngokupheleleyo inkangeleko yeBootstrap 3 ngaphandle kokuchukumisa iifayile ezingundoqo. I-Bootstrap 4 ibonelela ngendlela eqhelekileyo, kodwa eyahlukileyo kancinane.
Ngoku, umxholo ufezekiswa ngoguquguquko lweSass, iimephu zeSass, kunye neCSS yesiko. Akusekho cwecwe lesimbo lomxholo ozinikeleyo; endaweni yoko, unokwenza umxholo owakhelwe-ngaphakathi ukongeza i-gradients, izithunzi, kunye nokunye.
Sebenzisa umthombo wethu weefayile zeSass ukuthatha ithuba lokuguquguquka, iimephu, imixube, kunye nokunye.
Nanini na kunokwenzeka, kunqanda ukulungisa iifayile ezingundoqo zeBootstrap. KwiSass, oko kuthetha ukwenza elakho iphepha lesimbo elingenisa ngaphandle iBootstrap ukuze ukwazi ukuyilungisa kwaye uyandise. Ucinga ukuba usebenzisa umphathi wephakheji njenge-npm, uya kuba nesakhiwo sefayile esijongeka ngolu hlobo:
Ukuba ukhuphele iifayile zethu zemvelaphi kwaye awusebenzisi umphathi wepakethe, uya kufuna ukuseta ngesandla into efana neso sakhiwo, ugcine iifayile zemvelaphi yeBootstrap zahlukile kweyakho.
Kweyakho custom.scss
, uya kungenisa iifayile zeSass zomthombo weBootstrap. Uneendlela ezimbini ongakhetha kuzo: bandakanya yonke iBootstrap, okanye khetha iindawo ozifunayo. Siyayikhuthaza le yokugqibela, nangona sisazi ukuba kukho iimfuno kunye nokuxhomekeka kumacandelo ethu. Kananjalo kuya kufuneka ubandakanye iJavaScript yeeplagi zethu.
Ngolu seto lusendaweni, ungaqala ukulungisa naziphi na izinto eziguquguqukayo zeSass kunye neemephu kweyakho custom.scss
. Ungaqalisa kwakhona ukongeza iinxalenye zeBootstrap phantsi // Optional
kwecandelo njengoko kufuneka. Sicebisa ukuba usebenzise isitaki esipheleleyo sokungenisa bootstrap.scss
kwifayile yethu njengendawo yakho yokuqala.
Yonke into eguquguqukayo ye-Sass kwi-Bootstrap 4 ibandakanya !default
iflegi ekuvumela ukuba ubhale ngaphezulu ixabiso elimiselweyo le-Sass yakho ngaphandle kokuguqula ikhowudi yemvelaphi ye-Bootstrap. Khuphela kwaye uncamathisele izinto eziguquguqukayo njengoko kufuneka, uguqule amaxabiso azo, kwaye ususe !default
iflegi. Ukuba uguqulo sele lwabelwe, ngoko aluzukwabelwa kwakhona ngamaxabiso angagqibekanga kwiBootstrap.
Ukugqithisa okuguquguqukayo ngaphakathi kwefayile ye-Sass efanayo kunokuza ngaphambi okanye emva kokuguquguquka okungagqibekanga. Nangona kunjalo, xa ubeka ngaphezulu kwiifayile zeSass, izinto zakho ezingaphezulu kufuneka zize phambi kokuba ungenise iifayile zeSass zeBootstrap.
Nanku umzekelo otshintsha background-color
kunye color
neyokurhweba <body>
ngaphandle kunye nokuqulunqa i-Bootstrap nge-npm:
Phinda njengoko kuyimfuneko kuyo nayiphi na inguqu kwiBootstrap, kuquka iinketho zehlabathi ezingezantsi.
I-Bootstrap 4 ibandakanya iimephu ze-Sass ezimbalwa, izibini zexabiso eziphambili ezenza kube lula ukuvelisa iintsapho ze-CSS ehambelanayo. Sisebenzisa iimephu zakwaSass kwimibala yethu, iindawo zokuqhawula igridi, nokunye. Kanye njengezinto eziguquguqukayo ze-Sass, zonke iimephu ze-Sass zibandakanya !default
iflegi kwaye zinokubhalwa ngaphezulu kwaye zandiswe.
Ezinye zeemephu zethu zakwaSass zidityaniswa zibe zingenanto ngokungagqibekanga. Oku kwenzelwa ukuvumela ukwandiswa lula kwemephu ye-Sass enikiweyo, kodwa kuza ngexabiso lokwenza ukususa izinto kwimephu kube nzima ngakumbi.
Ukuguqula umbala osele ukhona $theme-colors
kwimephu yethu, yongeza oku kulandelayo kwifayile yakho yesiko leSass:
Ukongeza umbala omtsha $theme-colors
, yongeza iqhosha elitsha kunye nexabiso:
Ukususa imibala kwi- $theme-colors
, okanye nayiphi na enye imephu, sebenzisa map-remove
:
I-Bootstrap ithatha ubukho bezitshixo ezithile ngaphakathi kweemephu ze-Sass njengoko besizisebenzisa kwaye sizandise ngokwethu. Njengoko ulungiselela iimephu ezibandakanyiweyo, ungadibana neempazamo apho kusetyenziswa isitshixo semephu ye-Sass ethile.
Umzekelo, sisebenzisa i- primary
, success
kunye danger
nezitshixo ezisuka $theme-colors
kumakhonkco, amaqhosha, kunye nefom yesimo. Ukutshintsha amaxabiso ezi zitshixo kufuneka kungabikho miba, kodwa ukuwasusa kunokubangela imiba yokuhlanganiswa kwe-Sass. Kwezi meko, kuya kufuneka uguqule ikhowudi ye-Sass esebenzisa loo maxabiso.
I-Bootstrap isebenzisa imisebenzi emininzi ye-Sass, kodwa iseti encinci kuphela esebenzayo kumxholo jikelele. Sifake imisebenzi emithathu yokufumana ixabiso kwiimephu zemibala:
Oku kukuvumela ukuba ukhethe umbala omnye kwimephu ye-Sass ngendlela onokuthi usebenzise ngayo ukuguquguquka kombala ukusuka kwi-v3.
Kananjalo sinomnye umsebenzi wokufumana umgangatho othile wombala kwimephu $theme-colors
. Amaxabiso amanqanaba angalunganga aya kuwenza lula umbala, ngelixa amanqanaba aphezulu aya kuba mnyama.
Ngokwenza, ungabiza umsebenzi kwaye ugqithise kwiiparamitha ezimbini: igama lombala ukusuka $theme-colors
(umzekelo, iprimary okanye ingozi) kunye nenqanaba lamanani.
Imisebenzi eyongezelelweyo inokongezwa kwixesha elizayo okanye i-Sass yakho yesiko ukwenza imisebenzi yenqanaba leemephu ze-Sass ezongezelelweyo, okanye enye yegeneric ukuba ufuna ukuba ne-verbose ngakumbi.
Omnye umsebenzi owongezelelweyo esiwufakayo kwiBootstrap ngumsebenzi wokuchasa umbala, color-yiq
. Isebenzisa isithuba sombala we-YIQ ukubuyisela ngokuzenzekelayo ukukhanya ( #fff
) okanye mnyama ( #111
) umahluko wombala osekelwe kumbala wesiseko oxeliweyo. Lo msebenzi uluncedo kakhulu kwimixube okanye iilophu apho uvelisa iiklasi ezininzi.
Umzekelo, ukuvelisa iiwotshi zemibala $theme-colors
kwimephu yethu:
Ingasetyenziselwa iimfuno zokuthelekisa enye:
Ungaphinda uchaze umbala osisiseko ngemisebenzi yethu yemephu yombala:
Yenza ngokwezifiso i-Bootstrap 4 kunye nefayile yethu eyakhelwe-ngaphakathi eguquguqukayo kwaye uguqule ngokulula izinto ezikhethwa yi-CSS yehlabathi ngezinto ezintsha $enable-*
zeSass. Bhala ngaphezulu ixabiso loguquko kwaye uphinde udibanise npm run test
njengoko kufuneka.
Ungafumana kwaye wenze ezi ziguquguqukayo kwiinketho eziphambili zehlabathi _variables.scss
kwifayile yethu.
Iyaguquguquka | Imilinganiselo | Inkcazo |
---|---|---|
$spacer |
1rem (ehlala ikho), okanye naliphi na ixabiso > 0 |
Ixela ixabiso elihlala lihleli le spacer ukuvelisa ngokwenkqubo izixhobo zethu ze spacer . |
$enable-rounded |
true (ehlala ikho) okanyefalse |
Yenza border-radius izimbo ezichazwe kwangaphambili kumacandelo awohlukeneyo. |
$enable-shadows |
true okanye false (okuhlala kukho) |
Yenza box-shadow izimbo ezichazwe kwangaphambili kumacandelo awohlukeneyo. |
$enable-gradients |
true okanye false (okuhlala kukho) |
Yenza iigradient ezichazwe kwangaphambili kusetyenziswa background-image izimbo kwizinto ezahlukeneyo. |
$enable-transitions |
true (ehlala ikho) okanyefalse |
Yenza okuchazwe transition kwangaphambili kumacandelo awohlukeneyo. |
$enable-hover-media-query |
true okanye false (okuhlala kukho) |
Irhoxisiwe |
$enable-grid-classes |
true (ehlala ikho) okanyefalse |
Yenza ukuveliswa kweeklasi zeCSS zesixokelelwano segridi (umzekelo, .container , .row , , .col-md-1 , njl.). |
$enable-caret |
true (ehlala ikho) okanyefalse |
Yenza ipseudo element ikhathalelwe .dropdown-toggle . |
$enable-print-styles |
true (ehlala ikho) okanyefalse |
Yenza izimbo zokulungisa ushicilelo. |
Uninzi lwezinto ezahlukeneyo zeBootstrap kunye nezinto eziluncedo zakhiwe ngothotho lwemibala echazwe kwimephu ye-Sass. Le mephu inokulotywa kwi-Sass ukuvelisa ngokukhawuleza uthotho lweesethi zemithetho.
Yonke imibala ekhoyo kwi-Bootstrap 4, iyafumaneka njengeenguqu ze-Sass kunye nemephu ye-Sass scss/_variables.scss
kwifayile yethu. Oku kuya kwandiswa kukhupho oluncinci olulandelayo ukongeza i-shades eyongezelelweyo, efana ne- grayscale palette esele siyibandakanyile.
Nantsi indlela onokuzisebenzisa ngayo ezi kwiSass yakho:
Iiklasi eziluncedo zombala zikwakhona ngokusetwa color
kunye background-color
.
Kwixesha elizayo, siza kujolisa ekuboneleleni ngeemephu ze-Sass kunye nezinto eziguquguqukayo zemithunzi yombala ngamnye njengoko senzile ngemibala engwevu engezantsi.
Sisebenzisa i-subset yayo yonke imibala ukwenza i-palette yombala omncinci wokuvelisa izikimu zombala, ezifumanekayo njengeenguqu ze-Sass kunye nemephu ye-Sass scss/_variables.scss
kwifayile yethu.
Iseti ebanzi yezinto ezingwevu kunye nemephu ye-Sass scss/_variables.scss
kwimithunzi engwevu engaguqukiyo kwiprojekthi yakho yonke.
Ngaphakathi _variables.scss
, uya kufumana iinguqu zethu zemibala kunye nemephu ye-Sass. Nanku umzekelo $colors
wemephu ye-Sass:
Yongeza, susa, okanye ulungise amaxabiso kwimaphu ukuhlaziya indlela asetyenziswa ngayo kwezinye izinto ezininzi. Ngelishwa ngeli xesha, asingawo onke amacandelo asebenzisa le mephu ye-Sass. Uhlaziyo lwexesha elizayo luya kuzama ukuphucula oku. Kude kube ngoko, cwangcisa ukusetyenziswa kwezinto ${color}
eziguquguqukayo kunye nale mephu ye-Sass.
Uninzi lwamacandelo kunye nezinto eziluncedo zeBootstrap zakhiwe @each
ngeelophu eziphindaphindayo kwimephu ye-Sass. Oku kuluncedo kakhulu ekuveliseni ukwahluka kwecandelo ngokwethu $theme-colors
kunye nokudala iiyantlukwano eziphendulayo kwindawo nganye yoqhawulo. Njengoko usenza ngokwezifiso ezi mephu ze-Sass kwaye uphinda uhlanganise, uya kubona ngokuzenzekelayo utshintsho lwakho lubonakaliswe kwezi lophu.
Uninzi lwamacandelo e-Bootstrap akhiwe ngendlela yeklasi ye-modifier. Oku kuthetha ukuba ubuninzi besimbo buqulethwe kudidi olusisiseko (umzekelo, .btn
) ngelixa ulwahlulo lwesimbo luvalelwe kwiiklasi zesilungisi (umzekelo, .btn-danger
). Ezi klasi zesilungisi zakhiwe $theme-colors
kwimephu ukwenza inani kunye negama leeklasi zethu zesilungisi.
Nantsi imizekelo emibini yendlela esijika ngayo phezu $theme-colors
kwemephu ukwenza izilungisi .alert
zecandelo kunye nazo zonke izinto zethu .bg-*
ezingasemva eziluncedo.
Ezi lophu zeSass aziphelelanga kwiimephu zemibala, nazo. Unokwenza ukwahluka okuphendulayo kwamacandelo akho okanye izinto eziluncedo. Thatha umzekelo izixhobo zethu zolungelelwaniso lombhalo ophendulayo apho sixuba i- @each
loop $grid-breakpoints
yemephu ye-Sass kunye nombuzo wemidiya uquka.
Ukuba ufuna ukulungisa eyakho $grid-breakpoints
, utshintsho lwakho luya kusebenza kuzo zonke iilophu eziphinda-phindayo phezu kwaloo mephu.
I-Bootstrap 4 ibandakanya malunga neshumi elinesibini leempawu zesiko le-CSS (izinto eziguquguqukayo) kwiCSS ehlanganisiweyo. Ezi zibonelela ngofikelelo olulula kumaxabiso aqhele ukusetyenziswa njengemibala yomxholo wethu, iindawo zokuqhawuka, kunye nokupakishwa kwefonti esisiseko xa usebenza kuMhloli wesikhangeli sakho, ikhowudi yebhokisi yesanti, okanye iprototyping jikelele.
Nazi izinto eziguquguqukayo esizibandakanyayo (qaphela ukuba :root
iyafuneka). Zifumaneka _root.scss
kwifayile yethu.
Izinto eziguquguqukayo zeCSS zibonelela ngokuguquguquka okufanayo kwizinto eziguquguqukayo zeSass, kodwa ngaphandle kwesidingo sokuhlanganiswa ngaphambi kokuba zinikezelwe kwisikhangeli. Umzekelo, apha siseta ngokutsha ifonti yephepha lethu kunye nezimbo zekhonkco ezineenguqu zeCSS.
Unokusebenzisa iinguqu zethu zebreakpoint kwimibuzo yakho yemidiya: