Lumpat menyang isi utama Lumpat menyang pandhu arah docs

Gunakake file Sass sumber kita kanggo njupuk kauntungan saka variabel, peta, campuran, lan fungsi kanggo mbantu mbangun luwih cepet lan ngatur proyek sampeyan.

Gunakake file Sass sumber kita kanggo njupuk kauntungan saka variabel, peta, mixin, lan liya-liyane.

Struktur berkas

Yen bisa, aja ngowahi file inti Bootstrap. Kanggo Sass, tegese nggawe stylesheet dhewe sing ngimpor Bootstrap supaya sampeyan bisa ngowahi lan ngluwihi. Yen sampeyan nggunakake manajer paket kaya npm, sampeyan bakal duwe struktur file sing katon kaya iki:

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

Yen sampeyan wis ndownload file sumber lan ora nggunakake manajer paket, sampeyan pengin nyetel kanthi manual kaya struktur kasebut, supaya file sumber Bootstrap kapisah saka sampeyan dhewe.

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

Ngimpor

Ing custom.scss, sampeyan bakal ngimpor file Sass sumber Bootstrap. Sampeyan duwe rong pilihan: kalebu kabeh Bootstrap, utawa pilih bagean sing dibutuhake. Kita nyengkuyung sing terakhir, sanajan ngerti ana sawetara syarat lan dependensi ing komponen kita. Sampeyan uga kudu nyakup sawetara JavaScript kanggo plugin kita.

// 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

Kanthi persiyapan kasebut, sampeyan bisa miwiti ngowahi variabel lan peta Sass ing custom.scss. Sampeyan uga bisa miwiti nambah bagean Bootstrap ing // Optionalbagean sing dibutuhake. Disaranake nggunakake tumpukan impor lengkap saka bootstrap.scssfile kita minangka titik wiwitan.

Default variabel

Saben variabel Sass ing Bootstrap kalebu !defaultgendera sing ngidini sampeyan ngganti nilai standar variabel kasebut ing Sass sampeyan dhewe tanpa ngowahi kode sumber Bootstrap. Salin lan tempel variabel yen perlu, ngowahi nilai, lan mbusak !defaultgendera. Yen variabel wis ditugasake, mula ora bakal ditugasake maneh dening nilai standar ing Bootstrap.

Sampeyan bakal nemokake dhaptar lengkap variabel Bootstrap ing scss/_variables.scss. Sawetara variabel disetel menyang null, variabel kasebut ora ngasilake properti kasebut kajaba ditimpa ing konfigurasi sampeyan.

Variabel overrides kudu teka sawise fungsi kita diimpor, nanging sadurunge liyane saka impor.

Mangkene conto sing ngganti background-colorlan colornalika <body>ngimpor lan nyusun Bootstrap liwat 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

Baleni yen perlu kanggo variabel apa wae ing Bootstrap, kalebu pilihan global ing ngisor iki.

Miwiti Bootstrap liwat npm karo proyek wiwitan kita! Pindhah menyang repositori template twbs/bootstrap-npm-starter kanggo ndeleng carane mbangun lan ngatur Bootstrap ing proyek npm sampeyan dhewe. Kalebu kompiler Sass, Autoprefixer, Stylelint, PurgeCSS, lan Ikon Bootstrap.

Peta lan puteran

Bootstrap kalebu sawetara peta Sass, pasangan nilai kunci sing luwih gampang ngasilake kulawarga CSS sing gegandhengan. Kita nggunakake peta Sass kanggo werna, breakpoints kothak, lan liyane. Kaya variabel Sass, kabeh peta Sass kalebu !defaultgendera lan bisa diganti lan ditambahi.

Sawetara peta Sass kita digabung dadi peta kosong minangka standar. Iki wis rampung kanggo ngidini expansion gampang saka map Sass diwenehi, nanging rawuh ing biaya njabut item saka peta rada luwih angel.

Ngowahi peta

Kabeh variabel ing $theme-colorspeta ditetepake minangka variabel mandiri. Kanggo ngowahi warna sing ana ing $theme-colorspeta kita, tambahake ing ngisor iki menyang file Sass khusus sampeyan:

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

Sabanjure, variabel kasebut disetel ing $theme-colorspeta Bootstrap:

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

Tambah menyang peta

Tambah warna anyar menyang $theme-colors, utawa peta liyane, kanthi nggawe peta Sass anyar nganggo nilai khusus sampeyan lan gabung karo peta asli. Ing kasus iki, kita bakal nggawe $custom-colorspeta anyar lan nggabung karo $theme-colors.

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

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

Mbusak saka peta

Kanggo mbusak werna saka $theme-colors, utawa peta liyane, gunakake map-remove. Elinga yen sampeyan kudu nglebokake ing antarane syarat lan pilihan:

// 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

Tombol sing dibutuhake

Bootstrap nganggep ana sawetara tombol tartamtu ing peta Sass nalika kita nggunakake lan ngluwihi iki dhewe. Nalika sampeyan ngatur peta sing kalebu, sampeyan bisa nemoni kesalahan ing ngendi kunci peta Sass tartamtu digunakake.

Contone, kita nggunakake primary, success, lan dangertombol saka $theme-colorskanggo pranala, tombol, lan wangun negara. Ngganti nilai kunci kasebut ora ana masalah, nanging mbusak bisa nyebabake masalah kompilasi Sass. Ing kasus iki, sampeyan kudu ngowahi kode Sass sing nggunakake nilai kasebut.

Fungsi

werna-werna

Ing jejere peta Sass , werna tema uga bisa digunakake minangka variabel mandiri, kayata $primary.

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

Sampeyan bisa madhangi utawa peteng werna nganggo Bootstrap tint-color()lan shade-color()fungsi. Fungsi kasebut bakal nyampur warna karo ireng utawa putih, ora kaya asli lighten()lan darken()fungsi Sass sing bakal ngganti cahya kanthi jumlah tetep, sing asring ora nyebabake efek sing dikarepake.

// 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));
}

Ing laku, sampeyan bakal nelpon fungsi lan pass ing werna lan bobot paramèter.

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

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

Kontras warna

Supaya bisa nyukupi standar aksesibilitas WCAG 2.0 kanggo kontras warna , panulis kudu menehi rasio kontras paling sethithik 4,5:1 , kanthi sawetara pangecualian.

Fungsi tambahan sing kalebu ing Bootstrap yaiku fungsi kontras warna color-contrast,. Iki nggunakake algoritma WCAG 2.0 kanggo ngitung ambang kontras adhedhasar luminance relatif ing ruang warna sRGBkanggo ngasilake warna kontras cahya ( #fff), peteng ( #212529) utawa ireng ( #000) kanthi otomatis adhedhasar warna dhasar sing ditemtokake. Fungsi iki utamané migunani kanggo mixin utawa puteran ing ngendi sampeyan ngasilake pirang-pirang kelas.

Contone, kanggo ngasilake swatch warna saka $theme-colorspeta kita:

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

Bisa uga digunakake kanggo kabutuhan kontras siji-siji:

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

Sampeyan uga bisa nemtokake warna dhasar kanthi fungsi peta warna:

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

Uwal SVG

Kita nggunakake escape-svgfungsi kanggo uwal saka <, >lan #karakter kanggo gambar latar mburi SVG. Nalika nggunakake escape-svgfungsi kasebut, URI data kudu dipetik.

Fungsi Tambah lan Kurangi

Kita nggunakake addlan subtractfungsi kanggo mbungkus calcfungsi CSS. Tujuan utama fungsi kasebut yaiku kanggo ngindhari kesalahan nalika nilai "unitless" diterusake 0menyang calcekspresi. Ekspresi kaya calc(10px - 0)bakal ngasilake kesalahan ing kabeh browser, sanajan bener kanthi matematis.

Conto ing endi calc sah:

$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);
}

Conto ing ngendi calc ora valid:

$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);
}

Campuran

Direktori kita scss/mixins/duwe akeh mixin sing nguwasani bagean Bootstrap lan uga bisa digunakake ing proyek sampeyan dhewe.

Skema warna

Campuran singkatan kanggo pitakon prefers-color-schememedia kasedhiya kanthi dhukungan kanggo light, dark, lan skema warna khusus.

@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
  }
}