网页 综合 更新时间 2026-11-13 15:36:33

CSS variables · Bootstrap v6.0

自动蜘蛛 getbootstrap.com 正文约 3561 字
CSS variables · Bootstrap v6.0
Use Bootstrap’s CSS custom properties for fast and forward-looking design and development.
Bootstrap includes many CSS custom properties (variables) in its compiled CSS for real-time customization without the need to recompile Sass. These provide easy access to commonly used values like our theme colors, breakpoints, and primary font stacks when working in your browser’s inspector, a code sandbox, or general prototyping.
All our custom properties are prefixed with bs- to avoid conflicts with third party CSS.
Here are the variables we include (note that the :root, :host selector is required) that can be accessed anywhere Bootstrap’s CSS is loaded. They’re located in our _root.scss file and included in our compiled dist files. Many of these resolve their light and dark values automatically via the CSS light-dark() function, so a single :root, :host declaration covers both color modes.
Bootstrap makes use of custom properties as local variables for various components. This way we reduce our compiled CSS, ensure styles aren’t inherited in places like nested tables, and allow some basic restyling and extending of Bootstrap components after Sass compilation.
Nearly every component exposes its own set of CSS variables, scoped to the component’s base class. For example, the alert component reads a handful of --bs-alert-* variables that you can override in your own CSS—no Sass compilation required:
Have a look at our table documentation for some insight into how we’re using CSS variables. Our navbars also use CSS variables, and we use CSS variables across our grids—primarily for gutters and the opt-in CSS grid.
Whenever possible, we’ll assign CSS variables at the base component level (e.g., .navbar for navbar and its sub-components). This reduces guessing on where and how to customize, and allows for easy modifications by our team in future updates.
Most CSS variables use a prefix to avoid collisions with your own codebase. This prefix is in addition to the -- that’s required on every CSS variable.
The bs- prefix is added automatically via PostCSS using the postcss-prefix-custom-properties plugin during our build process. This means you write unprefixed custom properties in the source Sass (e.g., --border-radius) and they become prefixed in the compiled CSS (e.g., --bs-border-radius).
If you’re building your own custom version of Bootstrap’s CSS and want to change the prefix, update the prefix option in your PostCSS configuration:
CSS variables offer similar flexibility to Sass’s variables, but without the need for compilation before being served to the browser. For example, here we’re resetting our page’s font and link styles with CSS variables.
Bootstrap provides custom :focus styles using a combination of Sass and CSS variables that can be optionally added to specific components and elements. We do not yet globally override all :focus styles.
In our Sass, we set default values that can be customized before compiling.
Those variables are then reassigned to :root level CSS variables that can be customized in real-time, including with options for x and y offsets (which default to their fallback value of 0).
While we include our grid breakpoints as CSS variables (except for xs), be aware that CSS variables do not work in media queries. This is by design in the CSS spec for variables, but may change in coming years with support for env() variables. Check out this Stack Overflow answer for some helpful links. In the meantime, you can use these variables in other CSS situations, as well as in your JavaScript.
访问原文 返回搜索 https://getbootstrap.com/docs/6.0/getting-started/css-variables
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。