Theming Bootstrap
Sinthani Mwamakonda Anu Bootstrap 4 ndi zosintha zathu zatsopano za Sass pazokonda zapadziko lonse lapansi kuti mukhale ndi mitu yosavuta komanso kusintha magawo.
Mu Bootstrap 3, mitu idayendetsedwa makamaka ndi kupitilira mu LESS, CSS yodziwika bwino, ndi pepala lamutu losiyana lomwe tidaphatikiza dist
m'mafayilo athu. Ndi khama, wina atha kukonzanso mawonekedwe a Bootstrap 3 osakhudza mafayilo oyambira. Bootstrap 4 imapereka njira yodziwika bwino, koma yosiyana pang'ono.
Tsopano, mitu imakwaniritsidwa ndi mitundu ya Sass, mamapu a Sass, ndi CSS yokhazikika. Palibenso pepala lodzipatulira lamutu; m'malo mwake, mutha kuloleza mutu womwe wamangidwa kuti uwonjezere ma gradients, mithunzi, ndi zina zambiri.
Gwiritsani ntchito mafayilo athu a Sass kuti mutengere mwayi pazosintha, mamapu, zosakaniza, ndi zina zambiri.
Ngati n'kotheka, pewani kusintha mafayilo amtundu wa Bootstrap. Kwa Sass, izi zikutanthauza kupanga zolemba zanu zomwe zimalowetsa Bootstrap kuti mutha kusintha ndikuwonjezera. Pongoganiza kuti mukugwiritsa ntchito phukusi loyang'anira ngati npm, mudzakhala ndi fayilo yomwe imawoneka motere:
Ngati mwatsitsa mafayilo athu oyambira ndipo simukugwiritsa ntchito woyang'anira phukusi, mudzafuna kukhazikitsa pamanja china chofanana ndi mawonekedwewo, kusunga mafayilo amtundu wa Bootstrap kukhala osiyana ndi anu.
M'mafayilo anu custom.scss
, mudzalowetsa mafayilo a Sass a Bootstrap. Muli ndi njira ziwiri: kuphatikiza zonse za Bootstrap, kapena sankhani magawo omwe mukufuna. Timalimbikitsa omalizawa, ngakhale dziwani kuti pali zofunika zina ndi zodalira pazigawo zathu zonse. Muyeneranso kuphatikiza JavaScript yamapulagini athu.
Ndi kukhazikitsa komweko, mutha kuyamba kusintha zosintha zilizonse za Sass ndi mamapu anu custom.scss
. Mutha kuyambanso kuwonjezera magawo a Bootstrap pansi // Optional
pagawo ngati pakufunika. Tikukulangizani kuti mugwiritse ntchito stack yonse kuchokera mufayilo yathu bootstrap.scss
ngati poyambira.
Kusintha kulikonse kwa Sass mu Bootstrap 4 kumaphatikizapo !default
mbendera yomwe imakulolani kuti mupitirire mtengo wosasinthika mu Sass yanu popanda kusintha code source ya Bootstrap. Koperani ndi kumata zosintha momwe zingafunikire, sinthani makonda awo, ndikuchotsa !default
mbendera. Ngati kusintha kwaperekedwa kale, ndiye kuti sikugawidwenso ndi mikhalidwe yokhazikika mu Bootstrap.
Mupeza mndandanda wathunthu wazosintha za Bootstrap mu scss/_variables.scss
.
Zosintha zosinthika mkati mwa fayilo yomweyo ya Sass zitha kubwera zisanachitike kapena zitatha zosinthika. Komabe, mukamawonjezera mafayilo a Sass, zowonjezera zanu ziyenera kubwera musanalowetse mafayilo a Sass a Bootstrap.
Nachi chitsanzo chomwe chimasintha background-color
ndi potumiza color
ndikulemba <body>
Bootstrap kudzera npm:
Bwerezani ngati kuli kofunikira pakusintha kulikonse mu Bootstrap, kuphatikiza zosankha zapadziko lonse lapansi pansipa.
Bootstrap 4 imaphatikizapo mamapu ochepa a Sass, awiriawiri ofunika kwambiri omwe amapangitsa kuti zikhale zosavuta kupanga mabanja a CSS yogwirizana. Timagwiritsa ntchito mamapu a Sass pamitundu yathu, mapulaneti a gridi, ndi zina zambiri. Monga zosinthika za Sass, mamapu onse a Sass amaphatikiza !default
mbendera ndipo amatha kuchotsedwa ndikukulitsidwa.
Ena mwa mamapu athu a Sass amaphatikizidwa kukhala opanda kanthu mwachisawawa. Izi zimachitika kuti zilole kukulitsa kosavuta kwa mapu a Sass, koma zimabwera pamtengo wopangitsa kuchotsa zinthu pamapu kukhala kovuta kwambiri.
Kuti musinthe mtundu womwe ulipo $theme-colors
pamapu athu, onjezani zotsatirazi pafayilo yanu yamtundu wa Sass:
Kuti muwonjezere mtundu watsopano ku $theme-colors
, onjezani kiyi yatsopano ndi mtengo:
Kuti muchotse mitundu pa $theme-colors
, kapena mapu ena aliwonse, gwiritsani ntchito map-remove
. Dziwani kuti muyenera kuyiyika pakati pa zomwe tikufuna ndi zomwe mungasankhe:
Bootstrap imatengera kukhalapo kwa makiyi ena mkati mwa mamapu a Sass momwe timagwiritsira ntchito ndikuwonjezera tokha. Mukakonza mamapu omwe akuphatikizidwa, mutha kukumana ndi zolakwika pomwe kiyi ya mapu a Sass ikugwiritsidwa ntchito.
Mwachitsanzo, timagwiritsa ntchito primary
, success
, ndi danger
makiyi ochokera $theme-colors
ku maulalo, mabatani, ndi mawonekedwe a fomu. Kusintha makiyi a makiyiwa sikuyenera kubweretsa zovuta, koma kuwachotsa kungayambitse nkhani za Sass. Muzochitika izi, muyenera kusintha nambala ya Sass yomwe imagwiritsa ntchito mfundozo.
Bootstrap imagwiritsa ntchito ntchito zingapo za Sass, koma kagawo kakang'ono kokha kamene kamagwira ntchito pamutu wamba. Taphatikiza ntchito zitatu zopezera makonda kuchokera pamapu amitundu:
Izi zimakupatsani mwayi wosankha mtundu umodzi pamapu a Sass monga momwe mungagwiritsire ntchito mtundu wa v3.
Tilinso ndi ntchito ina yopezera mulingo winawake wamtundu pamapu$theme-colors
. Miyezo yolakwika idzachepetsa mtunduwo, pomwe milingo yayikulu idzadetsedwa.
Pochita, mumatcha ntchitoyi ndikudutsa magawo awiri: dzina la mtundu kuchokera $theme-colors
(mwachitsanzo, choyambirira kapena chowopsa) ndi kuchuluka kwa manambala.
Ntchito zowonjezera zitha kuwonjezedwa mtsogolomo kapena makonda anu a Sass kuti mupange magwiridwe antchito a mamapu owonjezera a Sass, kapenanso yachidule ngati mukufuna kukhala olankhula kwambiri.
Ntchito ina yowonjezera yomwe timaphatikizapo mu Bootstrap ndi ntchito yosiyanitsa mitundu, color-yiq
. Imagwiritsa ntchito malo amtundu wa YIQ kuti ibweretse kuwala ( #fff
) kapena mdima ( #111
) wosiyana wa mtundu kutengera mtundu womwe watchulidwa. Izi ndizofunikira makamaka pazosakaniza kapena malupu pomwe mukupanga makalasi angapo.
Mwachitsanzo, kupanga zosintha zamitundu $theme-colors
pamapu athu:
Itha kugwiritsidwanso ntchito pazosowa zosiyanitsa kamodzi:
Mutha kutchulanso mtundu woyambira ndi mapu athu amitundu:
Sinthani Mwamakonda Anu Bootstrap 4 ndi fayilo yathu yosinthira makonda opangidwa ndikusintha mosavuta zokonda zapadziko lonse za CSS ndi $enable-*
mitundu yatsopano ya Sass. Chotsani mtengo wosinthika ndikuwonjezeranso npm run test
ngati pakufunika.
Mutha kupeza ndikusintha masinthidwe awa pazosankha zazikulu zapadziko lonse lapansi mufayilo ya Bootstrap scss/_variables.scss
.
Zosintha | Makhalidwe | Kufotokozera |
---|---|---|
$spacer |
1rem (zosakhazikika), kapena mtengo uliwonse> 0 |
Imatchula mtengo wokhazikika wa spacer kuti apange zida zathu za spacer . |
$enable-rounded |
true (zosasintha) kapenafalse |
Imathandizira border-radius masitayelo ofotokozedweratu pazinthu zosiyanasiyana. |
$enable-shadows |
true kapena false (zosakhazikika) |
Imathandizira box-shadow masitayelo ofotokozedweratu pazinthu zosiyanasiyana. |
$enable-gradients |
true kapena false (zosakhazikika) |
Imathandizira ma gradients ofotokozedwatu kudzera background-image masitayelo pazinthu zosiyanasiyana. |
$enable-transitions |
true (zosasintha) kapenafalse |
Imathandiza predefined transition s pa zigawo zosiyanasiyana. |
$enable-hover-media-query |
true kapena false (zosakhazikika) |
Yasiyidwa |
$enable-grid-classes |
true (zosasintha) kapenafalse |
Imathandizira kupanga makalasi a CSS pagululi (monga, .container , .row , .col-md-1 , etc.). |
$enable-caret |
true (zosasintha) kapenafalse |
Imayatsa pseudo element caret pa .dropdown-toggle . |
$enable-print-styles |
true (zosasintha) kapenafalse |
Imayatsa masitaelo kuti muwongolere kusindikiza. |
Zambiri mwazinthu zosiyanasiyana za Bootstrap ndi zofunikira zimamangidwa kudzera mumitundu ingapo yofotokozedwa pamapu a Sass. Mapuwa atha kuwongoleredwa ku Sass kuti apange madongosolo angapo.
Mitundu yonse yomwe ilipo mu Bootstrap 4, imapezeka ngati mitundu ya Sass ndi mapu a Sass mufayilo scss/_variables.scss
. Izi zidzawonjezedwa m'mabuku ang'onoang'ono otsatirawa kuti muwonjezere mithunzi yowonjezera, mofanana ndi palette ya grayscale yomwe timaphatikizapo kale.
Umu ndi momwe mungagwiritsire ntchito izi mu Sass yanu:
Makalasi opangira utoto amapezekanso kuti akhazikike color
ndi background-color
.
M'tsogolomu, tikhala ndi cholinga chopereka mamapu a Sass ndi zosintha zamitundu yamtundu uliwonse monga momwe tachitira ndi mitundu yotuwa pansipa.
Timagwiritsa ntchito kagawo kakang'ono kamitundu yonse kuti tipange utoto wocheperako popanga masikimu amitundu, omwe amapezekanso ngati mitundu ya Sass ndi mapu a Sass mufayilo ya Bootstraps scss/_variables.scss
.
Mitundu yokulirapo yamitundu yotuwa ndi mapu a Sass scss/_variables.scss
amithunzi yotuwa mosasinthasintha pulojekiti yanu.
Mkati scss/_variables.scss
, mupeza mitundu yamitundu ya Bootstrap ndi mapu a Sass. Nachi chitsanzo cha $colors
mapu a Sass:
Onjezani, chotsani, kapena sinthani zinthu zomwe zili pamapu kuti zisinthe momwe zimagwiritsidwira ntchito m'zigawo zina zambiri. Tsoka ilo panthawiyi, sizinthu zonse zomwe zimagwiritsa ntchito mapu a Sass. Zosintha zamtsogolo zidzayesetsa kukonza izi. Mpaka pamenepo, konzekerani kugwiritsa ntchito ${color}
zosinthika ndi mapu a Sass awa.
Zambiri mwazinthu ndi zofunikira za Bootstrap zimamangidwa ndi @each
malupu omwe amayenda pamapu a Sass. Izi ndizothandiza kwambiri popanga mitundu yosiyanasiyana mwazinthu zathu $theme-colors
ndikupanga mitundu yomvera pagawo lililonse. Mukakonza mamapu awa a Sass ndikuphatikizanso, mudzawona zosintha zanu zikuwonekera mu malupu awa.
Zambiri mwazinthu za Bootstrap zimamangidwa ndi njira yosinthira kalasi. Izi zikutanthauza kuti kuchuluka kwa makongoletsedwe kuli m'gulu loyambira (mwachitsanzo, .btn
) pomwe masitayelo amangochitika m'makalasi osintha (mwachitsanzo, .btn-danger
). Makalasi osinthira awa amapangidwa kuchokera $theme-colors
pamapu kuti asinthe makonda a nambala ndi dzina la makalasi athu osintha.
Nazi zitsanzo ziwiri za momwe timayendera $theme-colors
mapu kuti tipange zosintha pagawo ndi .alert
zida zathu zonse .bg-*
zakumbuyo.
Malupu a Sass awa samangokhala mamapu amitundu, mwina. Mukhozanso kupanga mitundu yosiyanasiyana yamagawo anu kapena zofunikira. Tengani mwachitsanzo zida zathu zoyankhulirana ndi mawu pomwe timaphatikiza @each
kuzungulira kwa $grid-breakpoints
mapu a Sass ndi funso latolankhani.
Ngati mukufunika kusintha $grid-breakpoints
, zosintha zanu zidzagwira ntchito pamalupu onse omwe akupitilira pamapuwo.
Bootstrap 4 imaphatikizapo pafupifupi magawo awiri a CSS makonda (zosinthika) mu CSS yake yopangidwa. Izi zimapereka mwayi wosavuta kuzinthu zomwe zimagwiritsidwa ntchito nthawi zambiri monga mitundu yathu yamutu, zodulira, ndi milu ya zilembo zoyambirira mukamagwira ntchito mu Inspector ya msakatuli wanu, bokosi la sandbox, kapena ma prototyping wamba.
Nazi zosintha zomwe timaphatikiza (zindikirani kuti :root
ndizofunikira). Zili mufayilo yathu _root.scss
.
Zosintha za CSS zimapereka kusinthasintha kofanana ndi zosintha za Sass, koma popanda kufunika kophatikiza musanatumizidwe kwa osatsegula. Mwachitsanzo, apa tikukhazikitsanso mafonti atsamba lathu ndi masitayelo olumikizirana ndi CSS.
Ngakhale tidaphatikizirapo zodulira mumitundu yathu ya CSS (mwachitsanzo, --breakpoint-md
), izi sizimathandizidwa pamafunso atolankhani , koma zitha kugwiritsidwabe ntchito mkati mwamalamulo pamafunso azama TV. Zosintha zosinthika izi zimakhalabe mu CSS yopangidwa kuti zigwirizane ndi kumbuyo chifukwa zitha kugwiritsidwa ntchito ndi JavaScript. Dziwani zambiri mu spec.
Nachi chitsanzo cha zomwe sizikuthandizidwa:
Ndipo nachi chitsanzo cha zomwe zimathandizidwa: