Theming Bootstrap
Enza ngendlela oyifisayo i-Bootstrap 4 ngezinto zethu ezintsha ezakhelwe ngaphakathi ze-Sass ezithandwayo zesitayela somhlaba wonke ukuze uthole itimu elula nezinguquko zengxenye.
Isingeniso
Ku-Bootstrap 3, itimu yayiqhutshwa kakhulu ukweqa okuguquguqukayo kokuthi LESS, i-CSS yangokwezifiso, neshidi lesitayela letimu elihlukile esilifake dist
kumafayela ethu. Ngomzamo othile, umuntu angakwazi ukuklama kabusha ngokuphelele ukubukeka kwe-Bootstrap 3 ngaphandle kokuthinta amafayela awumongo. I-Bootstrap 4 inikeza indlela ejwayelekile, kodwa ehluke kancane.
Manje, itimu ifezwa okuguquguqukayo kwe-Sass, amamephu we-Sass, ne-CSS yangokwezifiso. Alisekho ishidi lesitayela letimu elizinikele; esikhundleni salokho, ungakwazi ukunika amandla itimu eyakhelwe ngaphakathi ukuze ungeze ama-gradient, izithunzi, nokuningi.
Sass
Sebenzisa umthombo wethu wamafayela e-Sass ukuze uthathe ithuba lokuhlukahluka, amamephu, imiksi, nokuningi. Ekwakheni kwethu sikhuphule ukunemba kokusondeza kwe-Sass kwaba ku-6 (ngokuzenzakalelayo kungu-5) ukuvimbela izinkinga ngokufinyezwa kwesiphequluli.
Isakhiwo sefayela
Noma nini lapho kunokwenzeka, gwema ukulungisa amafayela ayinhloko e-Bootstrap. Kwa-Sass, lokho kusho ukudala eyakho ishidi lesitayela elingenisa i-Bootstrap ukuze ukwazi ukuyilungisa futhi uyinwebe. Uma ucabanga ukuthi usebenzisa umphathi wephakheji njenge-npm, uzoba nesakhiwo sefayela esibukeka kanje:
Uma ulande amafayela ethu omthombo futhi ungasebenzisi umphathi wephakheji, uzofuna ukumisa mathupha into efana naleso sakhiwo, ugcine amafayela omthombo we-Bootstrap ehlukene kwewakho.
Iyangenisa
Kweyakho custom.scss
, uzongenisa amafayela we-Sass womthombo we-Bootstrap. Unezinketho ezimbili: faka yonke i-Bootstrap, noma khetha izingxenye ozidingayo. Sikhuthaza lokhu okwakamuva, nakuba sazi ukuthi kunezimfuneko ezithile nokuncika kuzo zonke izingxenye zethu. Uzodinga futhi ukufaka i-JavaScript yama-plugin ethu.
Uma lokho kusetha kusendaweni, ungaqala ukulungisa noma yikuphi okuguquguqukayo kwe-Sass namamephu kufayela lakho le- custom.scss
. Ungaqala futhi ukwengeza izingxenye ze-Bootstrap ngaphansi // Optional
kwesigaba njengoba kudingeka. Siphakamisa ukuthi usebenzise isitaki sokungenisa esigcwele esivela bootstrap.scss
kufayela lethu njengendawo yakho yokuqala.
Okuzenzakalelayo okuguquguqukayo
Konke okuguquguqukayo kwe-Sass ku-Bootstrap 4 kufaka phakathi !default
ifulegi elikuvumela ukuthi ubhale ngaphezulu inani elizenzakalelayo lokuguquguquka ku-Sass yakho ngaphandle kokulungisa ikhodi yomthombo ye-Bootstrap. Kopisha futhi unamathisele okuguquguqukayo njengoba kudingeka, lungisa amanani azo, futhi ususe !default
ifulegi. Uma okuguquguqukayo sekuvele kwabelwe, ngakho-ke ngeke kuphinde kunikezwe amanani azenzakalelayo ku-Bootstrap.
Uzothola uhlu oluphelele lweziguquguquko ze-Bootstrap ku- scss/_variables.scss
. Ezinye eziguquguqukayo zisethelwe kokuthi null
, lezi ziguquko azikhiphi isici ngaphandle kokuthi zikhishwe ekucushweni kwakho.
Ukukhishwa okuguquguqukayo ngaphakathi kwefayela elifanayo le-Sass kungase kufike ngaphambi noma ngemva kokuguquguquka okuzenzakalelayo. Nokho, uma ukhipha kuwo wonke amafayela e-Sass, ukukhipha kwakho kufanele kuze ngaphambi kokuthi ungenise amafayela e-Sass e-Bootstrap.
Nasi isibonelo esishintsha i- kanye background-color
neyokuthi lapho ungenisa futhi uhlanganisa i-Bootstrap nge-npm:color
<body>
Phinda njengoba kudingekile kunoma yikuphi okuguquguqukayo ku-Bootstrap, kuhlanganise nezinketho zomhlaba jikelele ngezansi.
Amamephu namaluphu
I-Bootstrap 4 ihlanganisa idlanzana lamamephu e-Sass, amapheya enani angukhiye akwenza kube lula ukukhiqiza imindeni ye-CSS ehlobene. Sisebenzisa amamephu e-Sass ngemibala yethu, izindawo zokunqamuka kwegridi, nokuningi. Njengezinto eziguquguqukayo ze-Sass, wonke amamephu akwa-Sass afaka !default
ifulegi futhi angabhalwa phansi futhi andwe.
Amanye amamephu ethu e-Sass ahlanganiswa abe angenalutho ngokuzenzakalelayo. Lokhu kwenzelwa ukuvumela ukunwetshwa okulula kwemephu ye-Sass enikeziwe, kodwa kuza ngezindleko zokwenza ukususa izinto kumephu kube nzima kakhulu.
Lungisa imephu
Ukuze ulungise umbala okhona $theme-colors
kumephu yethu, engeza okulandelayo kufayela lakho langokwezifiso le-Sass:
Engeza kumephu
Ukwengeza umbala omusha ku- $theme-colors
, engeza ukhiye omusha nevelu:
Susa kumephu
Ukuze ususe imibala ku- $theme-colors
, nanoma iyiphi enye imephu, sebenzisa map-remove
. Qaphela ukuthi kufanele uyifake phakathi kwezimfuneko zethu nezinketho:
Okhiye abadingekayo
I-Bootstrap ithatha ubukhona bokhiye abathile ngaphakathi kwamamephu e-Sass njengoba siwasebenzisile futhi sizinweba thina ngokwethu. Njengoba wenza ngendlela oyifisayo amamephu afakiwe, ungase uhlangabezane namaphutha lapho kusetshenziswa khona ukhiye othile wemephu ye-Sass.
Isibonelo, sisebenzisa i- primary
, success
, kanye danger
nokhiye abavela kuzo $theme-colors
ukuze uthole izixhumanisi, izinkinobho, nezimo zefomu. Ukushintsha amanani alaba khiye akumele kuveze izinkinga, kodwa ukuwasusa kungase kubangele izinkinga zokuhlanganisa i-Sass. Kulezi zimo, uzodinga ukushintsha ikhodi ye-Sass esebenzisa lawo manani.
Imisebenzi
I-Bootstrap isebenzisa imisebenzi eminingana ye-Sass, kodwa isethi encane kuphela esebenza kungqikithi evamile. Sifake imisebenzi emithathu yokuthola amanani kumamephu ombala:
Lokhu kukuvumela ukuthi ukhethe umbala owodwa kumephu ye-Sass njengokuthi ungasebenzisa kanjani umbala ohlukile ku-v3.
Siphinde sibe nomunye umsebenzi wokuthola izinga elithile lombala $theme-colors
kumephu. Amanani amaleveli angalungile azokhanyisa umbala, kuyilapho amazinga aphezulu ezoba mnyama.
Empeleni, ungabiza umsebenzi bese udlula ngamapharamitha amabili: igama lombala ukusuka $theme-colors
(isb, okuyinhloko noma ingozi) kanye nezinga lezinombolo.
Imisebenzi eyengeziwe ingase yengezwe ngokuzayo noma eyakho i-Sass yangokwezifiso ukuze udale imisebenzi yeleveli yamamephu engeziwe e-Sass, noma ngisho nelinye elijwayelekile uma ufuna ukusho amazwi amaningi.
Ukungafani kombala
Umsebenzi owengeziwe esiwufaka ku-Bootstrap umsebenzi wokugqama kombala, color-yiq
. Isebenzisa isikhala sombala we-YIQ ukubuyisela ngokuzenzakalelayo ukukhanya ( #fff
) noma okumnyama ( #111
) umbala wokugqama osuselwe kumbala wesisekelo oshiwo. Lo msebenzi uwusizo ikakhulukazi kumamiksi noma amaluphu lapho ukhiqiza khona amakilasi amaningi.
Isibonelo, ukwenza ama-swatches ombala $theme-colors
kumephu yethu:
Ingase futhi isetshenziselwe izidingo zokuqhathanisa okukodwa:
Ungaphinda ucacise umbala oyisisekelo ngemisebenzi yethu yemephu yombala:
Escape SVG
Sisebenzisa escape-svg
umsebenzi ukubalekela <
, >
nezinhlamvu #
zezithombe zangemuva ze-SVG. Lezi zinhlamvu zidinga ukweqiwa ukuze kunikezwe kahle izithombe ezingemuva nge-IE.
Engeza futhi Ususe imisebenzi
Sisebenzisa add
kanye subtract
nemisebenzi ukusonga umsebenzi we-CSS calc
. Injongo eyinhloko yale misebenzi iwukugwema amaphutha lapho inani elithi “unitless” 0
lidluliselwa calc
enkulumweni. Izinkulumo ezifana nalezi calc(10px - 0)
zizobuyisela iphutha kuzo zonke iziphequluli, ngaphandle kokuthi zilungile ngokwezibalo.
Isibonelo lapho i-calc isebenza khona:
Isibonelo lapho i-calc ingavumelekile:
Izinketho ze-Sass
Enza ngendlela oyifisayo i-Bootstrap 4 ngefayela lethu eliguquguqukayo elakhelwe ngaphakathi futhi uguqule kalula okuthandwayo kwe-CSS yomhlaba wonke ngezinto ezintsha ze- $enable-*
Sass. Khipha inani lokuguquguquka bese uhlanganisa futhi npm run test
njengoba kudingeka.
Ungathola futhi wenze ngendlela oyifisayo lokhu okuguquguqukayo ukuze uthole izinketho ezibalulekile zomhlaba wonke scss/_variables.scss
kufayela le-Bootstrap.
Iyaguquguquka | Amanani | Incazelo |
---|---|---|
$spacer |
1rem (okuzenzakalelayo), nanoma yiliphi inani > 0 |
Icacisa inani elizenzakalelayo le-spacer ukukhiqiza ngokuhlelekile izinsiza zethu ze-spacer . |
$enable-rounded |
true (okuzenzakalelayo) nomafalse |
Inika amandla border-radius izitayela ezichazwe ngaphambilini ezingxenyeni ezihlukahlukene. |
$enable-shadows |
true noma false (okuzenzakalelayo) |
Inika amandla box-shadow izitayela ezichazwe ngaphambilini ezingxenyeni ezihlukahlukene. |
$enable-gradients |
true noma false (okuzenzakalelayo) |
Inika amandla ama-gradient achazwe ngaphambilini background-image ngezitayela ezingxenyeni ezihlukahlukene. |
$enable-transitions |
true (okuzenzakalelayo) nomafalse |
Inika amandla transition ama-s achazwe ngaphambilini ezingxenyeni ezihlukahlukene. |
$enable-prefers-reduced-motion-media-query |
true (okuzenzakalelayo) nomafalse |
Inika amandla prefers-reduced-motion umbuzo wemidiya , ocindezela ukugqwayiza/ushintsho oluthile olususelwe kuzintandokazi zesiphequluli/isistimu yokusebenza yabasebenzisi. |
$enable-hover-media-query |
true noma false (okuzenzakalelayo) |
Kwehlisiwe |
$enable-grid-classes |
true (okuzenzakalelayo) nomafalse |
Inika amandla ukukhiqizwa kwamakilasi e-CSS kusistimu yegridi (isb, .container , .row , .col-md-1 , njll.). |
$enable-caret |
true (okuzenzakalelayo) nomafalse |
Inika amandla i-pseudo element caret ku- .dropdown-toggle . |
$enable-pointer-cursor-for-buttons |
true (okuzenzakalelayo) nomafalse |
Engeza ikhesa “yesandla” kuma-elementi enkinobho engacishiwe. |
$enable-print-styles |
true (okuzenzakalelayo) nomafalse |
Inika amandla izitayela zokuthuthukisa ukuphrinta. |
$enable-responsive-font-sizes |
true noma false (okuzenzakalelayo) |
Inika amandla osayizi befonti abaphendulayo . |
$enable-validation-icons |
true (okuzenzakalelayo) nomafalse |
Inika amandla background-image izithonjana ngaphakathi kokufakwayo kombhalo kanye namafomu athile angokwezifiso ezimo zokuqinisekisa. |
$enable-deprecation-messages |
true noma false (okuzenzakalelayo) |
Misa ukuze true ubonise izexwayiso uma usebenzisa noma yimiphi imiksi ehoxisiwe nemisebenzi ehlelelwe ukususwa ku- v5 . |
Umbala
Izingxenye eziningi nezinsiza ezihlukahlukene ze-Bootstrap zakhiwe ngochungechunge lwemibala echazwe kumephu ye-Sass. Le mephu ingafakwa ku-Sass ukuze kukhiqizwe ngokushesha uchungechunge lwamasethi emithetho.
Yonke imibala
Yonke imibala etholakala ku-Bootstrap 4, iyatholakala njengezinto eziguquguqukayo ze-Sass kanye nemephu ye-Sass scss/_variables.scss
efayeleni. Lokhu kuzonwetshwa ekukhishweni okuncane okulandelayo ukuze kwengezwe ama-shades engeziwe, afana nephalethi ye-grayscale esesivele siyifakile.
Nansi indlela ongazisebenzisa ngayo lezi ku-Sass yakho:
Amakilasi okusetshenziswa kwemibala nawo ayatholakala ukuze amiswe color
futhi background-color
.
Ngokuzayo, sizohlose ukuhlinzeka ngamamephu we-Sass nokuguquguquka kwemibala yombala ngamunye njengoba senzile ngemibala empunga engezansi.
Imibala yetimu
Sisebenzisa isethi engaphansi yayo yonke imibala ukuze sakhe iphalethi yombala emincane ukuze sikhiqize izikimu zombala, futhi ezitholakala njengeziguquguqukayo ze-Sass kanye nemephu ye-Sass scss/_variables.scss
kufayela le-Bootstrap.
Izimpunga
Isethi enwetshiwe yokuhlukahluka okumpunga kanye nemephu ye-Sass scss/_variables.scss
ukuze uthole imithunzi engaguquki yokumpunga kuyo yonke iphrojekthi yakho. Qaphela ukuthi lawa “angamagrey apholile”, ajwayele ukuya ethoni ecashile eluhlaza okwesibhakabhaka, esikhundleni sokumpunga okuphakathi.
Ngaphakathi scss/_variables.scss
, uzothola okuguquguqukayo kombala kwe-Bootstrap kanye nemephu ye-Sass. Nasi isibonelo $colors
semephu ye-Sass:
Engeza, susa, noma lungisa amanani ngaphakathi kwemephu ukuze ubuyekeze indlela asetshenziswa ngayo kwezinye izingxenye eziningi. Ngeshwa ngalesi sikhathi, akuzona zonke izingxenye ezisebenzisa le mephu ye-Sass. Izibuyekezo zesikhathi esizayo zizolwela ukuthuthukisa kulokhu. Kuze kube yileso sikhathi, hlela ukusebenzisa ${color}
okuguquguqukayo kanye nale mephu ye-Sass.
Izingxenye
Izingxenye eziningi nezinsiza ze-Bootstrap zakhiwe @each
ngamalophu aphindaphindayo phezu kwemephu ye-Sass. Lokhu kusiza kakhulu ekukhiqizeni okuhlukile kwengxenye yethu $theme-colors
nokudala okuhlukile okusabelayo kwephuzu ngalinye lokuhlukana. Njengoba wenza ngendlela oyifisayo lawa mamephu we-Sass futhi uhlanganisa futhi, uzobona ngokuzenzakalela izinguquko zakho ziboniswa kulawa maluphu.
Izilungisi
Izingxenye eziningi ze-Bootstrap zakhiwe ngendlela yekilasi le-base-modifier. Lokhu kusho ukuthi inqwaba yesitayela iqukethwe kusigaba sesisekelo (isb, .btn
) kuyilapho ukuhluka kwesitayela kuphelele kumakilasi okulungisa (isb, .btn-danger
). Lezi zigaba zokuguqula zakhiwe kusukela $theme-colors
kumephu ukuze kwenziwe ngendlela oyifisayo inombolo negama lamakilasi ethu okulungisa.
Nazi izibonelo ezimbili zokuthi singena kanjani phezu $theme-colors
kwemephu ukuze sikhiqize izilungisi .alert
zengxenye kanye nazo zonke .bg-*
izinsiza zethu ezingemuva.
Iyasabela
Lawa ma-loops akwa-Sass awakhawulelwe kumamephu anemibala, nawo. Ungakwazi futhi ukukhiqiza ukuhlukahluka okusabelayo kwezingxenye zakho noma izinsiza. Thatha isibonelo izinsiza zethu zokuqondanisa umbhalo ophendulayo lapho sixuba @each
iluphu $grid-breakpoints
yemephu ye-Sass kanye nombuzo wemidiya.
Uma kwenzeka udinga ukulungisa $grid-breakpoints
, izinguquko zakho zizosebenza kuwo wonke ama-loops aphindaphindayo kuleyo mephu.
Izinguquko ze-CSS
I-Bootstrap 4 ihlanganisa cishe izingxenye ezimbili nambili zangokwezifiso ze-CSS (okuguquguqukayo) ku-CSS yayo ehlanganisiwe. Lokhu kunikeza ukufinyelela okulula kumanani asetshenziswa kakhulu njengemibala yethu yetimu, ama-breakpoint, nezitaki zefonti eziyinhloko lapho usebenza Kumhloli wesiphequluli sakho, ikhodi yesihlabathi, noma i-prototyping evamile.
Okuguquguqukayo okutholakalayo
Nazi izinto eziguquguqukayo esizihlanganisayo (qaphela ukuthi :root
kuyadingeka). Atholakala _root.scss
kufayela lethu.
Izibonelo
Okuguquguqukayo kwe-CSS kunikeza ukuguquguquka okufanayo kokuguquguqukayo kwe-Sass, kodwa ngaphandle kwesidingo sokuhlanganisa ngaphambi kokuthi kunikezwe isiphequluli. Isibonelo, lapha sisetha kabusha ifonti yekhasi lethu nezitayela zokuxhumanisa neziguquguqukayo ze-CSS.
Okuguquguqukayo kwe-Breakpoint
Nakuba ekuqaleni sifake ama-breakpoint ezinguqukweni zethu ze-CSS (isb, --breakpoint-md
), lezi azisekelwe emibuzweni yemidiya , kodwa zingasetshenziswa ngaphakathi kwezimiso zemithetho emibuzweni yemidiya. Lezi zinguquko ze-breakpoint zihlala ku-CSS ehlanganisiwe ukuze zihambisane nemuva uma zingasetshenziswa yi-JavaScript. Funda kabanzi ku-spec .
Nasi isibonelo salokho okungasekelwe:
Futhi nasi isibonelo salokho okusekelwayo: