What are Bootstrap CSS variables and how to use them?
Bootstrap CSS variables are custom properties defined by Bootstrap, which are used to directly customize the appearance of the project through CSS and control color, spacing, fonts, borders, etc. They are based on native CSS custom properties and can be easily rewritten and reused. Bootstrap 5 makes extensive use of CSS variables to implement dynamic styles. These variables usually act on root or specific components and support global or local modifications. For example, --bs-primary defines the main color, --bs-font-sans-serif sets the sans serif font, and --bs-border-radius sets the rounded corner size. Users can reference these variables in their own CSS: for example, .my-button uses var(--bs-primary) as the background color. To customize Bootstrap styles, redefine variables through Sass before importing, or override the :root value with normal CSS after loading. When using SCSS, first set $primary: #ff6b6b; and then import Bootstrap to generate CSS based on the new value. Reusing Bootstrap variables in custom components can maintain design consistency. For example, .alert-custom uses variables such as --bs-danger-text and --bs-border-radius-lg to match the built-in danger prompt style. Some variables have component or responsive scope, such as --bs-btn-padding-y and --bs-btn-font-size of buttons, and extended styles such as .btn-large-custom can be created through local definition. In short, Bootstrap CSS variables simplify theme adjustment and component fine-tuning, and provide a modern and easy-to-maintain design system access method.

Bootstrap CSS variables are custom properties defined by Bootstrap that allow you to customize the look and feel of your project directly through CSS. These variables control aspects like colors, spacing, fonts, borders, and more. They're built into Bootstrap using native CSS custom properties, making them easy to override and reuse.
How Bootstrap Uses CSS Variables
Bootstrap 5 relies heavily on CSS variables (also known as custom properties) to enable dynamic styling. These are scoped to specific elements—usually :root or components—so you can change styles globally or locally.
For example, Bootstrap defines primary color, font size, border radius, and other design tokens as variables:
:root { --bs-primary: #0d6efd; --bs-secondary: #6c757d; --bs-font-sans-serif: 'Helvetica Neue', Helvetica, Arial, sans-serif; --bs-border-radius: 0.375rem; }You can use these values anywhere in your CSS:
.my-button { background-color: var(--bs-primary); border-radius: var(--bs-border-radius); }Customizing Bootstrap Variables
To change Bootstrap's default appearance, redefine its variables before importing Bootstrap. This works best when using Sass, but you can also override them in regular CSS if you load your styles after Bootstrap.
- If using Sass/SCSS: set variables before importing Bootstrap
- If using plain CSS: redefine :root variables after Bootstrap's stylesheet
Example with SCSS:
// Override variables $primary: #ff6b6b; $border-radius: 0.5rem; // Then import Bootstrap @import "bootstrap/scss/bootstrap";In this case, Bootstrap will generate CSS using your updated values.
Using CSS Variables in Your Own Components
You can build consistent UIs by reusing Bootstrap's variables in your custom classes. This keeps your design aligned with Bootstrap's system.
For instance:
.alert-custom { padding: var(--bs-gap-3); color: var(--bs-danger-text); background-color: var(--bs-danger-bg-subtle); border: 1px solid var(--bs-danger-border-subtle); border-radius: var(--bs-border-radius-lg); }This ensures your alert matches Bootstrap's subtle danger style without duplicating values.
Responsive and Component-Specific Variables
Some variables are scoped to components or responsive contexts. For example, buttons use --bs-btn-padding-y and --bs-btn-font-size . You can modify these for specific instances:
.btn-large-custom { --bs-btn-padding-y: 0.75rem; --bs-btn-font-size: 1.25rem; }This creates a larger button while still using Bootstrap's button base styles.
Basically, Bootstrap CSS variables make customization easier and more maintainable. Whether adjusting global themes or tweaking individual components, they provide a clean, modern way to work with Bootstrap's design system.
The above is the detailed content of What are Bootstrap CSS variables and how to use them?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
How do CSS variables work? How to use inline CSS variables for layout?
Jun 14, 2022 am 10:46 AM
This article will take you through CSS variables, talk about how CSS variables work, and introduce how to use inline CSS variables to improve the efficiency of smart layout. I hope it will be helpful to everyone!
How to use CSS variables for theming?
Jul 15, 2025 am 01:22 AM
The core of using CSS variables to achieve topic switching is to define the basic variables and organize the topic structure, and dynamically switch through class names or attributes. The steps are as follows: 1. Define basic variables such as colors, fonts, etc. in root; 2. Create classes that cover variables for different themes (such as dark and light colors); 3. Call variables using var() in CSS rules; 4. Switch class names or attributes through JavaScript to achieve theme changes; 5. Extend variables to font size, rounded corners, shadows and other style attributes. This clear structure and easy maintenance lies in reasonable naming and scope control.
What is the purpose of CSS variables scope?
Jul 18, 2025 am 03:35 AM
The scope of CSS variables controls their access scope and behavior, and implements style control through global and local scopes. By default, CSS variables are valid in declared elements and their children, but the scope can be adjusted by defining the location; global variables are defined in: root pseudo-class and can be accessed by the entire document; local variables are defined in a specific selector, only for use by this component or region; reasonable scope improves maintainability and avoids naming conflicts; common patterns are global variables used for topics (such as colors, fonts), and local variables are used for component layout settings; variables can be overwritten at any level of the DOM tree, providing finer style control; fallback values can be specified when using variables to enhance robustness; appropriate scope makes the style neat, predictable, and easy to expand.
What are CSS Custom Properties (variables), and how do they improve maintainability and theming?
Jun 19, 2025 am 12:48 AM
CSS variables improve style maintenance and theme management by defining reusable values. Its core advantages include: 1. Reduce duplication and improve consistency by centrally defining style values; 2. Support dynamic coverage to achieve simple and efficient multi-theme switching; 3. Avoid common traps such as inconsistent naming, overuse, etc. Developers should define global variables in:root, and use scope and fallback values to enhance flexibility and readability, while dynamic adjustments are combined with JavaScript to improve user experience.
What are CSS custom properties (variables) and how to use them?
Jul 21, 2025 am 02:34 AM
CSS custom attributes (i.e. CSS variables) improve development efficiency by storing and reusing style values. The core usage methods include: 1. Use the --variable name syntax to define in:root or specific selector; 2. Reference variables through the var (--variable name, default value) function; 3. Can dynamically update the variable values, support theme switching, responsive design and other scenarios. Their inheritance and cascade rules are consistent with ordinary CSS properties and can be modified through JavaScript runtime to achieve user preferences or dynamic style adjustments.
How to use CSS variables
Aug 22, 2025 am 07:49 AM
CSS variables improve maintainability of styles by defining and multiplexing values. 1. Use the --prefix to define global variables in:root, such as --primary-color; 2. Use the var() function to refer to variables, such as var(--primary-color); 3. You can define local variables in a specific selector to achieve context override; 4. var() supports setting alternative values, such as var(--text-color,#333); 5. Dynamically update variables through JavaScript, such as setProperty('--primary-color','#ff5733'); 6. Follow best practices: name meaningful, group related variables, avoid
The Power of CSS Custom Properties (Variables)
Aug 04, 2025 pm 12:02 PM
CSScustompropertiesenabledynamic,maintainablestylingbyallowingvariablestobedefinedandupdatedatruntime.1.Definereusabledesigntokensin:rootforconsistentcolors,fonts,andspacing.2.UpdatevariableswithJavaScriptforreal-timethemeswitching,liketogglingdarkmo
How to use CSS variables (custom properties)?
Sep 11, 2025 am 11:48 AM
To define CSS variables, you need to use the --prefix and usually declare in:root, such as --primary-color:#007bff; 2. Reference variables through the var() function, supporting setting of alternate values, such as var(--text-color, black); 3. You can update variables dynamically through JavaScript or media queries to achieve topic switching or responsive adjustments; 4. It is recommended to use semantic naming, unify specifications, and avoid duplicate definitions to improve maintenance and readability.





