Tlula eka nhlokomhaka-nkulu Tlula eka docs navigation

Tirhisa tifayela ta hina ta Sass ta xihlovo ku tirhisa swilo leswi cinca-cincaka, mimepe, mixins, na mintirho ku ku pfuna ku aka hi ku hatlisa na ku lulamisa phurojeke ya wena.

Tirhisa tifayela ta hina ta Sass ta xihlovo ku tirhisa swilo leswi cinca-cincaka, mimepe, mixins, na swin’wana.

Xivumbeko xa fayili

Loko swi koteka, papalata ku cinca tifayela ta nkoka ta Bootstrap. Eka Sass, sweswo swi vula ku endla stylesheet ya wena leyi nghenisaka Bootstrap leswaku u ta kota ku yi cinca no yi andlala. Loko hi ehleketa leswaku u tirhisa mufambisi wa phasela ku fana na npm, u ta va na xivumbeko xa fayili lexi langutekaka hi ndlela leyi:

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

Loko u dawunilode tifayela ta hina ta xihlovo naswona u nga tirhisi mufambisi wa tiphasela, u ta lava ku veka hi voko nchumu lowu fanaka ni xivumbeko xexo, u hlayisa tifayela ta xihlovo ta Bootstrap ti hambanile ni ta wena.

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

Ku nghenisa swilo etikweni

Eka custom.scss, u ta nghenisa tifayela ta Sass ta xihlovo xa Bootstrap. U na tindlela timbirhi: katsa hinkwaswo swa Bootstrap, kutani u hlawula swiphemu leswi u swi lavaka. Hi khutaza leswi swo hetelela, hambi leswi tivaka leswaku ku na swilaveko swo karhi na ku titshega eka swiphemu swa hina hinkwaswo. U ta tlhela u lava ku katsa JavaScript yin’wana ya ti-plugin ta hina.

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

// Include any default variable overrides here (though functions won't be available)

@import "../node_modules/bootstrap/scss/bootstrap";

// Then add additional custom code here
// Custom.scss
// Option B: Include parts of Bootstrap

// 1. Include functions first (so you can manipulate colors, SVGs, calc, etc)
@import "../node_modules/bootstrap/scss/functions";

// 2. Include any default variable overrides here

// 3. Include remainder of required Bootstrap stylesheets
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

// 4. Include any optional Bootstrap components as you like
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/containers";
@import "../node_modules/bootstrap/scss/grid";

// 5. Add additional custom code here

Loko setup yoleyo yi ri kona, u nga sungula ku cinca xin’wana ni xin’wana xa swilo leswi cinca-cincaka swa Sass ni mimepe eka custom.scss. U nga ha tlhela u sungula ku engetela swiphemu swa Bootstrap ehansi ka // Optionalxiyenge tanihilaha swi lavekaka hakona. Hi ringanyeta ku tirhisa xithaki xo nghenisa hi ku helela ku suka eka bootstrap.scssfayili ya hina tanihi masungulo ya wena.

Swihlawulekisi leswi cinca-cincaka

Xihlawulekisi xin’wana na xin’wana xa Sass eka Bootstrap xi katsa !defaultmujeko lowu ku pfumelelaka ku tlula nkoka wa xiviri wa xihlawulekisi eka Sass ya wena handle ko cinca khodi ya xihlovo ya Bootstrap. Kopa u tlhela u namarheta swilo leswi cinca-cincaka hilaha swi lavekaka hakona, cinca mimpimo ya swona, ivi u susa !defaultmujeko. Loko xihlawulekisi se xi averiwe, kutani a xi nge he averiwi nakambe hi mimpimo ya ntolovelo eka Bootstrap.

U ta kuma nxaxamelo lowu heleleke wa swilo leswi cinca-cincaka swa Bootstrap eka scss/_variables.scss. Swilo swin’wana leswi cinca-cincaka swi vekiwile eka null, swilo leswi swi nga humesi nhundzu handle ka loko swi tluriwile eka xivumbeko xa wena.

Ku tlula loku cinca-cincaka ku fanele ku ta endzhaku ka loko mintirho ya hina yi nghenisiwile, kambe emahlweni ka ku nghenisiwa hinkwako.

Hi lexi xikombiso lexi cincaka background-colorna colorxa <body>loko u nghenisa na ku hlengeleta Bootstrap hi ku tirhisa npm:

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

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

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

// Optional Bootstrap components here
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc

Phindha tanihilaha swi lavekaka hakona eka xihlawulekisi xihi na xihi eka Bootstrap, ku katsa na swihlawulekisi swa misava hinkwayo laha hansi.

Sungula hi Bootstrap hi ku tirhisa npm hi phurojeke ya hina yo sungula! Khoma ndlela eka vuhlayiselo bya xifaniso xa twbs/bootstrap-npm-starter ku vona ndlela yo aka na ku lulamisa Bootstrap eka phurojeke ya wena ya npm. Ku katsa na muhlengeleti wa Sass, Autoprefixer, Stylelint, PurgeCSS, na Swifaniso swa Bootstrap.

Mimepe na swirhendzevutana

Bootstrap yi katsa mimepe yitsongo ya Sass, mimpatswa ya nkoka ya nkoka leyi endlaka leswaku swi olova ku tumbuluxa mindyangu ya CSS leyi fambelanaka. Hi tirhisa mimepe ya Sass eka mihlovo ya hina, tindhawu to tshoveka ta gridi ni swin’wana. Ku fana na swilo leswi cinca-cincaka swa Sass, mimepe hinkwayo ya Sass yi katsa !defaultmujeko naswona yi nga ha tluriwa no andlariwa.

Yin’wana ya mimepe ya hina ya Sass yi hlanganisiwa yi va leyi nga riki na nchumu hi ku tiyimisela. Leswi swi endliwa ku pfumelela ku ndlandlamuxiwa ko olova ka mepe wa Sass lowu nyikiweke, kambe swi ta hi ntsengo wo endla leswaku ku susa swilo eka mepe swi tika nyana.

Cinca mepe

Swilo hinkwaswo leswi cinca-cincaka $theme-colorsemepeni swi hlamuseriwa tanihi swilo leswi cinca-cincaka leswi tiyimeleke swoxe. Ku cinca muvala lowu nga kona eka $theme-colorsmepe wa hina, engetela leswi landzelaka eka fayili ya wena ya Sass ya ntolovelo:

$primary: #0074d9;
$danger: #ff4136;

Endzhaku ka nkarhi, swilo leswi swi cinca-cincaka swi vekiwa eka $theme-colorsmepe wa Bootstrap:

$theme-colors: (
  "primary": $primary,
  "danger": $danger
);

Engetela eka mepe

Engetela mihlovo leyintshwa eka $theme-colors, kumbe mepe wun’wana ni wun’wana, hi ku endla mepe lowuntshwa wa Sass hi mimpimo ya wena ya ntolovelo ivi u wu hlanganisa ni mepe wo sungula. Eka xiyimo lexi, hi ta endla $custom-colorsmepe lowuntshwa ivi hi wu hlanganisa na $theme-colors.

// Create your own map
$custom-colors: (
  "custom-color": #900
);

// Merge the maps
$theme-colors: map-merge($theme-colors, $custom-colors);

Susa eka mepe

Ku susa mihlovo eka $theme-colors, kumbe mepe wun’wana ni wun’wana, tirhisa map-remove. Tiva leswaku u fanele ku yi nghenisa exikarhi ka swilaveko swa hina na swihlawulekisi:

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

Swilotlelo leswi lavekaka

Bootstrap yi teka vukona bya swilotlelo swin’wana swo karhi endzeni ka mimepe ya Sass tani hi leswi hi tirhiseke na ku andlala leswi hi hexe. Loko u ri karhi u lulamisa mimepe leyi katsekaka, u nga ha hlangana ni swihoxo laha ku tirhisiwaka xilotlelo xo karhi xa mepe wa Sass.

Hi xikombiso, hi tirhisa primary, success, na dangerswilotlelo leswi humaka $theme-colorseka swihlanganisi, swikomba-nkarhi ni swiyimo swa fomo. Ku cinca mimpimo ya swilotlelo leswi a swi fanelanga ku humesa timhaka, kambe ku swi susa swi nga vanga swiphiqo swa nhlengeleto wa Sass. Eka swiyimo leswi, u ta lava ku cinca khodi ya Sass leyi tirhisaka mimpimo yoleyo.

Mintirho

Mihlovo

Etlhelo ka mimepe ya Sass leyi hi nga na yona, mihlovo ya nhlokomhaka yi nga tlhela yi tirhisiwa tanihi swilo leswi cinca-cincaka leswi tiyimeleke, ku fana na $primary.

.custom-element {
  color: $gray-100;
  background-color: $dark;
}

U nga voninga kumbe ku endla mihlovo ya ntima hi Bootstrap's tint-color()na shade-color()mintirho. Mintirho leyi yi ta hlanganisa mihlovo na ya ntima kumbe yo basa, ku hambana na Sass’ native lighten()na darken()mintirho leyi nga ta cinca ku olova hi mpimo lowu vekiweke, leswi talaka ku nga yisi eka mbuyelo lowu lavekaka.

// Tint a color: mix a color with white
@function tint-color($color, $weight) {
  @return mix(white, $color, $weight);
}

// Shade a color: mix a color with black
@function shade-color($color, $weight) {
  @return mix(black, $color, $weight);
}

// Shade the color if the weight is positive, else tint it
@function shift-color($color, $weight) {
  @return if($weight > 0, shade-color($color, $weight), tint-color($color, -$weight));
}

Hi ku tirhisa, u ta vitana ntirho ivi u hundza eka tipharamitha ta muvala ni ntiko.

.custom-element {
  color: tint-color($primary, 10%);
}

.custom-element-2 {
  color: shade-color($danger, 30%);
}

Ku hambana ka mihlovo

Leswaku ku fikeleriwa swipimelo swa ku fikelela swa WCAG 2.0 swa ku hambana ka mihlovo , vatsari va fanele ku nyika mpimo wa ku hambana wa kwalomu ka 4.5:1 , handle ka swin’wana switsongo swinene.

Ntirho wun’wana lowu hi wu katsaka eka Bootstrap i ntirho wa ku hambana ka mihlovo, color-contrast. Yi tirhisa algorithm ya WCAG 2.0 yo hlayela swipimelo swa ku hambana leswi sekeriweke eka ku vonakala loku ringanaka eka ndhawu ya sRGBmihlovo ku vuyisa hi ku tisungulela muvala wo hambana wo vonakala ( #fff), wa munyama ( #212529) kumbe wa ntima ( #000) lowu sekeriweke eka muvala wa le hansi lowu boxiweke. Ntirho lowu wu pfuna ngopfu eka ti mixins kumbe ti loops laha u humesaka ti class to tala.

Hi xikombiso, ku endla swifaniso swa mihlovo ku suka eka $theme-colorsmepe wa hina:

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

Yi nga ha tlhela yi tirhisiwa eka swilaveko swa ku hambana ka nkarhi wun’we:

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

U nga ha tlhela u boxa muvala wa le hansi hi mintirho ya hina ya mepe wa mihlovo:

.custom-element {
  color: color-contrast($dark); // returns `color: #fff`
}

Balekela SVG

Hi tirhisa escape-svgntirho ku balekela <, >na #swihlawulekisi swa swifaniso swa le ndzhaku swa SVG. Loko u tirhisa escape-svgntirho, ti-URI ta data ti fanele ku tshahiwa.

Mintirho yo Engetela na ku Susa

Hi tirhisa mintirho ya addna subtractku phutsela calcntirho wa CSS. Xikongomelonkulu xa mintirho leyi i ku papalata swihoxo loko ntikelo lowu “nga riki na yuniti” 0wu hundziseriwa eka calcxivulavulelo. Swivulavulelo swo fana na leswi calc(10px - 0)swi ta vuyisa xihoxo eka swihlamusela-marito hinkwaswo, hambileswi swi nga ntiyiso hi tlhelo ra tinhlayo.

Xikombiso laha calc yi tirhaka kona:

$border-radius: .25rem;
$border-width: 1px;

.element {
  // Output calc(.25rem - 1px) is valid
  border-radius: calc($border-radius - $border-width);
}

.element {
  // Output the same calc(.25rem - 1px) as above
  border-radius: subtract($border-radius, $border-width);
}

Xikombiso laha calc yi nga tirhiki:

$border-radius: .25rem;
$border-width: 0;

.element {
  // Output calc(.25rem - 0) is invalid
  border-radius: calc($border-radius - $border-width);
}

.element {
  // Output .25rem
  border-radius: subtract($border-radius, $border-width);
}

Ti- mixins

Directory ya hina scss/mixins/yi na thani ya mixins leyi nyikaka matimba eka swiphemu swa Bootstrap naswona yi nga tlhela yi tirhisiwa ku tsemakanya phurojeke ya wena.

Swikimi swa mihlovo

Mixin ya xitlhokovetselo xa prefers-color-schemexivutiso xa midiya ya kumeka hi nseketelo wa light, dark, na swikimi swa mihlovo ya ntolovelo.

@mixin color-scheme($name) {
  @media (prefers-color-scheme: #{$name}) {
    @content;
  }
}
.custom-element {
  @include color-scheme(dark) {
    // Insert dark mode styles here
  }

  @include color-scheme(custom-named-scheme) {
    // Insert custom color scheme styles here
  }
}