Sass
利用我们的源 Sass 文件来利用变量、映射、混合和函数来帮助你更快地构建和自定义项目。
利用我们的源 Sass 文件来利用变量、映射、混合等。
🌐 Utilize our source Sass files to take advantage of variables, maps, mixins, and more.
在使用最新版本的 Dart Sass 编译源 Sass 文件时,会显示 Sass 弃用警告。这不会阻止编译或使用 Bootstrap。我们正在努力寻找长期解决方案,但在此期间,这些弃用通知可以忽略。
文件结构
🌐 File structure
尽可能避免修改 Bootstrap 的核心文件。对于 Sass,这意味着创建你自己的样式表来导入 Bootstrap,以便你可以修改和扩展它。假设你正在使用像 npm 这样的包管理器,你将会有如下的文件结构:
🌐 Whenever possible, avoid modifying Bootstrap’s core files. For Sass, that means creating your own stylesheet that imports Bootstrap so you can modify and extend it. Assuming you’re using a package manager like npm, you’ll have a file structure that looks like this:
your-project/
├── scss/
│ └── custom.scss
└── node_modules/
│ └── bootstrap/
│ ├── js/
│ └── scss/
└── index.html
如果你下载了我们的源文件,但没有使用包管理器,则需要手动创建类似结构的文件,并将 Bootstrap 的源文件与你自己的源文件分开。
🌐 If you’ve downloaded our source files and aren’t using a package manager, you’ll want to manually create something similar to that structure, keeping Bootstrap’s source files separate from your own.
your-project/
├── scss/
│ └── custom.scss
├── bootstrap/
│ ├── js/
│ └── scss/
└── index.html
输入
🌐 Importing
在你的 custom.scss 中,你将导入 Bootstrap 的源 Sass 文件。你有两个选择:包含所有的 Bootstrap,或者选择你需要的部分。我们鼓励后一种做法,但要注意我们的一些组件之间存在一些要求和依赖。你还需要为我们的插件包含一些 JavaScript。
🌐 In your custom.scss, you’ll import Bootstrap’s source Sass files. You have two options: include all of Bootstrap, or pick the parts you need. We encourage the latter, though be aware there are some requirements and dependencies across our components. You also will need to include some JavaScript for our plugins.
// 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 (including any separate color mode stylesheets)
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/variables-dark";
// 4. Include any default map overrides here
// 5. Include remainder of required parts
@import "../node_modules/bootstrap/scss/maps";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
// 6. Include any other optional stylesheet partials as desired; list below is not inclusive of all available stylesheets
@import "../node_modules/bootstrap/scss/utilities";
@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";
@import "../node_modules/bootstrap/scss/helpers";
// ...
// 7. Optionally include utilities API last to generate classes based on the Sass map in `_utilities.scss`
@import "../node_modules/bootstrap/scss/utilities/api";
// 8. Add additional custom code here
有了这个设置,你可以开始修改 custom.scss 中的任何 Sass 变量和映射。你还可以根据需要在 // Optional 部分添加 Bootstrap 的部分内容。我们建议使用我们 bootstrap.scss 文件中的完整导入堆栈作为起点。
🌐 With that setup in place, you can begin to modify any of the Sass variables and maps in your custom.scss. You can also start to add parts of Bootstrap under the // Optional section as needed. We suggest using the full import stack from our bootstrap.scss file as your starting point.
编译
🌐 Compiling
为了在浏览器中将你的自定义 Sass 代码用作 CSS,你需要一个 Sass 编译器。Sass 以 CLI 包的形式提供,但你也可以使用其他构建工具如 Gulp 或 Webpack 来编译,或者使用 GUI 应用。一些 IDE 也内置了 Sass 编译器,或者可以作为可下载的扩展使用。
🌐 In order to use your custom Sass code as CSS in the browser, you need a Sass compiler. Sass ships as a CLI package, but you can also compile it with other build tools like Gulp or Webpack, or with GUI applications. Some IDEs also have Sass compilers built in or as downloadable extensions.
我们喜欢使用命令行接口(CLI)来编译我们的 Sass,但你可以使用你喜欢的任何方法。从命令行运行以下命令:
🌐 We like to use the CLI to compile our Sass, but you can use whichever method you prefer. From the command line, run the following:
# Install Sass globally
npm install -g sass
# Watch your custom Sass for changes and compile it to CSS
sass --watch ./scss/custom.scss ./css/custom.css
在 sass-lang.com/install 和 使用 VS Code 编译 上了解更多关于你的选项。
🌐 Learn more about your options at sass-lang.com/install and compiling with VS Code.
使用 Bootstrap 搭配其他构建工具? 可以考虑阅读我们关于使用 Webpack、Parcel 或 Vite 编译的指南。我们在 GitHub 的示例仓库 中也提供了适合生产环境的演示。
包括
🌐 Including
一旦你的 CSS 被编译,你就可以将其包含在你的 HTML 文件中。在你的 index.html 内,你需要包含已编译的 CSS 文件。如果你更改了已编译 CSS 文件的路径,请确保更新路径。
🌐 Once your CSS is compiled, you can include it in your HTML files. Inside your index.html you’ll want to include your compiled CSS file. Be sure to update the path to your compiled CSS file if you’ve changed it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Custom Bootstrap</title>
<link href="/css/custom.css" rel="stylesheet">
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
变量默认值
🌐 Variable defaults
Bootstrap 中的每个 Sass 变量都包含 !default 标志,允许你在不修改 Bootstrap 源代码的情况下在自己的 Sass 中覆盖变量的默认值。根据需要复制和粘贴变量,修改其值,并移除 !default 标志。如果一个变量已经被赋值,那么它将不会被 Bootstrap 的默认值重新赋值。
🌐 Every Sass variable in Bootstrap includes the !default flag allowing you to override the variable’s default value in your own Sass without modifying Bootstrap’s source code. Copy and paste variables as needed, modify their values, and remove the !default flag. If a variable has already been assigned, then it won’t be re-assigned by the default values in Bootstrap.
你可以在 scss/_variables.scss 中找到 Bootstrap 变量的完整列表。某些变量被设置为 null,这些变量不会输出属性,除非在你的配置中被覆盖。
🌐 You will find the complete list of Bootstrap’s variables in scss/_variables.scss. Some variables are set to null, these variables don’t output the property unless they are overridden in your configuration.
变量覆盖必须在函数导入之后、其余导入之前进行。
🌐 Variable overrides must come after our functions are imported, but before the rest of the imports.
这是一个示例,当通过 npm 导入和编译 Bootstrap 时,会更改 <body> 的 background-color 和 color:
🌐 Here’s an example that changes the background-color and color for the <body> when importing and compiling Bootstrap via 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/variables-dark";
@import "../node_modules/bootstrap/scss/maps";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
// Optional Bootstrap components here
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc
根据需要对 Bootstrap 中的任何变量重复此操作,包括下面的全局选项。
🌐 Repeat as necessary for any variable in Bootstrap, including the global options below.
Get started with Bootstrap via npm with our starter project! Head to the Sass & JS example template repository to see how to build and customize Bootstrap in your own npm project. Includes Sass compiler, Autoprefixer, Stylelint, PurgeCSS, and Bootstrap Icons.
映射和循环
🌐 Maps and loops
Bootstrap 包含了一些 Sass 映射,即键值对,这使得生成相关 CSS 系列更容易。我们使用 Sass 映射来管理颜色、网格断点等。就像 Sass 变量一样,所有 Sass 映射都包含 !default 标志,并且可以被覆盖和扩展。
🌐 Bootstrap includes a handful of Sass maps, key value pairs that make it easier to generate families of related CSS. We use Sass maps for our colors, grid breakpoints, and more. Just like Sass variables, all Sass maps include the !default flag and can be overridden and extended.
我们的一些 Sass 映射默认会合并到空映射中。这样做是为了方便扩展给定的 Sass 映射,但代价是从映射中_删除_项目会稍微困难一些。
🌐 Some of our Sass maps are merged into empty ones by default. This is done to allow easy expansion of a given Sass map, but comes at the cost of making removing items from a map slightly more difficult.
修改映射
🌐 Modify map
$theme-colors 映射中的所有变量都定义为独立变量。要修改我们 $theme-colors 映射中的现有颜色,请将以下内容添加到你的自定义 Sass 文件中:
🌐 All variables in the $theme-colors map are defined as standalone variables. To modify an existing color in our $theme-colors map, add the following to your custom Sass file:
$primary: #0074d9;
$danger: #ff4136;
之后,这些变量会在 Bootstrap 的 $theme-colors 映射中设置:
🌐 Later on, these variables are set in Bootstrap’s $theme-colors map:
$theme-colors: (
"primary": $primary,
"danger": $danger
);
添加到映射
🌐 Add to map
通过创建一个包含自定义值的新 Sass 映射并将其与原始映射合并,可以为 $theme-colors 或任何其他映射添加新颜色。在这种情况下,我们将创建一个新的 $custom-colors 映射并将其与 $theme-colors 合并。
🌐 Add new colors to $theme-colors, or any other map, by creating a new Sass map with your custom values and merging it with the original map. In this case, we'll create a new $custom-colors map and merge it with $theme-colors.
// Create your own map
$custom-colors: (
"custom-color": #900
);
// Merge the maps
$theme-colors: map-merge($theme-colors, $custom-colors);
从映射中删除
🌐 Remove from map
要从 $theme-colors 或任何其他地图中移除颜色,请使用 map-remove。请注意,你必须在 variables 中定义后,并在 maps 中使用前,将 $theme-colors 插入我们的要求之间:
🌐 To remove colors from $theme-colors, or any other map, use map-remove. Be aware you must insert $theme-colors between our requirements just after its definition in variables and before its usage in maps:
// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/variables-dark";
$theme-colors: map-remove($theme-colors, "info", "light", "dark");
@import "../node_modules/bootstrap/scss/maps";
@import "../node_modules/bootstrap/scss/mixins";
@import "../node_modules/bootstrap/scss/root";
// Optional
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
// etc
所需按键
🌐 Required keys
Bootstrap 假设 Sass 映射中存在一些特定的键,因为我们自己使用并扩展了这些键。在自定义包含的映射时,你可能会遇到使用特定 Sass 映射键的错误。
🌐 Bootstrap assumes the presence of some specific keys within Sass maps as we used and extend these ourselves. As you customize the included maps, you may encounter errors where a specific Sass map’s key is being used.
例如,我们使用 $theme-colors 中的 primary、success 和 danger 键来处理链接、按钮和表单状态。替换这些键的值通常不会有问题,但删除它们可能会导致 Sass 编译问题。在这些情况下,你需要修改使用这些值的 Sass 代码。
🌐 For example, we use the primary, success, and danger keys from $theme-colors for links, buttons, and form states. Replacing the values of these keys should present no issues, but removing them may cause Sass compilation issues. In these instances, you’ll need to modify the Sass code that makes use of those values.
函数
🌐 Functions
颜色
🌐 Colors
除了我们拥有的Sass 映射之外,主题颜色也可以作为独立变量使用,例如 $primary。
🌐 Next to the Sass maps we have, theme colors can also be used as standalone variables, like $primary.
.custom-element {
color: $gray-100;
background-color: $dark;
}
你可以使用 Bootstrap 的 tint-color() 和 shade-color() 函数来调亮或调暗颜色。这些函数会将颜色与黑色或白色混合,不同于 Sass 原生的 lighten() 和 darken() 函数,它们会以固定量改变明度,这通常不会达到想要的效果。
🌐 You can lighten or darken colors with Bootstrap’s tint-color() and shade-color() functions. These functions will mix colors with black or white, unlike Sass’ native lighten() and darken() functions which will change the lightness by a fixed amount, which often doesn’t lead to the desired effect.
shift-color() 通过在权重为正时为颜色加阴影、权重为负时为颜色加色调,将这两种功能结合在一起。
// 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));
}
实际操作中,你可以调用该函数并传入颜色和权重参数。
🌐 In practice, you’d call the function and pass in the color and weight parameters.
.custom-element {
color: tint-color($primary, 10%);
}
.custom-element-2 {
color: shade-color($danger, 30%);
}
.custom-element-3 {
color: shift-color($success, 40%);
background-color: shift-color($success, -60%);
}
色彩对比
🌐 Color contrast
为了满足网页内容可访问性指南(WCAG)的对比度要求,作者必须提供至少文本颜色对比度为4.5:1和非文本颜色对比度为3:1,几乎没有例外。
🌐 In order to meet the Web Content Accessibility Guidelines (WCAG) contrast requirements, authors must provide a minimum text color contrast of 4.5:1 and a minimum non-text color contrast of 3:1, with very few exceptions.
为了帮助实现这一点,我们在 Bootstrap 中包含了 color-contrast 函数。它使用 WCAG 对比度比率算法 基于 sRGB 颜色空间中的相对亮度计算对比度阈值,以自动返回基于指定基色的浅色 (#fff)、深色 (#212529) 或黑色 (#000) 对比色。这个函数对于在生成多个类的 mixin 或循环中特别有用。
🌐 To help with this, we included the color-contrast function in Bootstrap. It uses the WCAG contrast ratio algorithm for calculating contrast thresholds based on relative luminance in an sRGB color space to automatically return a light (#fff), dark (#212529) or black (#000) contrast color based on the specified base color. This function is especially useful for mixins or loops where you’re generating multiple classes.
例如,要从我们的 $theme-colors 地图生成颜色样本:
🌐 For example, to generate color swatches from our $theme-colors map:
@each $color, $value in $theme-colors {
.swatch-#{$color} {
color: color-contrast($value);
}
}
它还可以用于一次性对比需求:
🌐 It can also be used for one-off contrast needs:
.custom-element {
color: color-contrast(#000); // returns `color: #fff`
}
你还可以使用我们的颜色图函数指定基色:
🌐 You can also specify a base color with our color map functions:
.custom-element {
color: color-contrast($dark); // returns `color: #fff`
}
转义 SVG
🌐 Escape SVG
我们使用 escape-svg 函数来转义 SVG 背景图片的 <、> 和 # 字符。使用 escape-svg 函数时,数据 URI 必须加引号。
🌐 We use the escape-svg function to escape the <, > and # characters for SVG background images. When using the escape-svg function, data URIs must be quoted.
加法和减法函数
🌐 Add and Subtract functions
我们使用 add 和 subtract 函数来封装 CSS calc 函数。这些函数的主要目的是在将“无单位”的 0 值传入 calc 表达式时避免错误。像 calc(10px - 0) 这样的表达式在所有浏览器中都会返回错误,尽管在数学上是正确的。
🌐 We use the add and subtract functions to wrap the CSS calc function. The primary purpose of these functions is to avoid errors when a “unitless” 0 value is passed into a calc expression. Expressions like calc(10px - 0) will return an error in all browsers, despite being mathematically correct.
计算有效的示例:
🌐 Example where the calc is valid:
$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);
}
计算无效的示例:
🌐 Example where the calc is invalid:
$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);
}
混入
🌐 Mixins
我们的 scss/mixins/ 目录中有大量的 mixin,它们驱动了 Bootstrap 的部分功能,也可以在你自己的项目中使用。
🌐 Our scss/mixins/ directory has a ton of mixins that power parts of Bootstrap and can also be used across your own project.
配色方案
🌐 Color schemes
prefers-color-scheme 媒体查询的速记 mixin 可用,并支持 light 和 dark 配色方案。有关我们的配色模式 mixin 的信息,请参阅配色模式文档。
🌐 A shorthand mixin for the prefers-color-scheme media query is available with support for light and dark color schemes. See the color modes documentation for information on our color mode mixin.
@mixin color-scheme($name) {
@media (prefers-color-scheme: #{$name}) {
@content;
}
}
.custom-element {
@include color-scheme(light) {
// Insert light mode styles here
}
@include color-scheme(dark) {
// Insert dark mode styles here
}
}