test-2.mdx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ---
  2. author: mdo
  3. date: "2022-05-16T00:00:00Z"
  4. title: Random Blog Post from Bootstrap
  5. description: Bootstrap v5.3.1 is here with bug fixes, documentation improvements, and more follow-up enhancements for color modes. Keep reading for the highlights!
  6. image: tabler-icons-2.30.0@2x.png
  7. keywords:
  8. - bootstrap
  9. - guide
  10. ---
  11. [Bootstrap v5.2.0-beta1](/2022/05/13/bootstrap-5-2-0-beta/) added a slew of CSS custom properties, or CSS variables, across the `:root` level and all our core components. Here's a quick look at how you can utilize them in your projects.
  12. With CSS variables, you can now customize Bootstrap easier than ever, and without the need for a CSS preprocessor. All the power of Sass is still there behind the scenes, but CSS variables adds a ton of power for the future. Use and compose new values, updates styles globally without recompiling, set fallback values, setup new color modes, and more.
  13. Let's dig in.
  14. ## CSS variables?
  15. Their official name is custom properties, but they're often referred to as CSS variables thanks to their most immediate use case for setting specific values. Consider reading [the MDN CSS custom properties article](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties#first_steps_with_custom_properties) or [the CSS Tricks guide](https://css-tricks.com/a-complete-guide-to-custom-properties/) if you need a primer.
  16. In a nutshell, CSS variables allow you to name frequently used values. For example, instead of writing `#6f42c1` everywhere, you can set `--purple: #6f42c1`. Then you can use that variable later on with the `var()` function.
  17. ```css
  18. :root {
  19. --purple: #6f42c1;
  20. }
  21. .custom-element {
  22. color: var(--purple);
  23. }
  24. ```
  25. We use CSS variables in Bootstrap to set many property values globally, across our components, and in some of our utilities.
  26. ## Groups of variables
  27. When we talk about CSS variables in Bootstrap, we're referring to three major groups:
  28. - **Root variables —** Globally scoped variables available on the `:root` element (`<html>` usually) and accessible by any element throughout the DOM.
  29. - **Component variables —** Variables scoped specifically to each component, usually on the component's base class, and their modifier classes and Sass mixins.
  30. - **Utility variables —** Used as modifiers within other utility classes.
  31. Regardless of where they are, all of our CSS variables are prefixed with `--bs-`, so you know where they're coming from and how they might be used across codebases that mix Bootstrap's CSS with additional custom styles. You'll also notice that we don't put all our component variables at the root level. This keeps CSS variables scoped to their intended use cases and prevents polluted variables in the global `:root` scope.
  32. It's also worth mentioning two larger efforts that are still to come around CSS variables:
  33. 1. Adding CSS variables to all our forms
  34. 2. Adding more nuanced global theme variables and support for color modes like [dark mode](https://github.com/twbs/bootstrap/pull/35857).
  35. These are likely coming in v5.3.0 (our next minor release after v5.2.0 stablizes), so in the mean time, check out the GitHub repo to see how things are shaping up.
  36. ## Root variables
  37. ![Root variables in web inspector](/assets/img/2022/05/docs-root-vars.png)
  38. Bootstrap has a ton of [root variables](https://getbootstrap.com/docs/5.2/customize/css-variables/#root-variables) and we'll only be adding more in future updates for the aforementioned color mode support. As of this post, we have the following CSS variables on the `:root` element:
  39. - **Colors —** All named colors, gray colors, and theme colors. This also includes all our `$theme-colors` in their `rgb` format.
  40. - **Body font styles —** Everything from `font-size` to `color` and more, all applied to our `<body>` element.
  41. - **Shared properties —** For property-value pairings that we consider theme specific, like link colors and border styles.
  42. Root CSS variables are used extensively across other parts of Bootstrap to allow you to easily override our default styles at a global level. For example, if you wanted to adjust the default `border-radius` and link color for our components, you could override a couple variables instead of writing new selectors.
  43. ```scss
  44. // custom.css
  45. :root {
  46. --bs-border-radius: .5rem;
  47. --bs-link-color: #333;
  48. }
  49. ```
  50. You can even use other root variables to override those values:
  51. ```scss
  52. // custom.css
  53. :root {
  54. --bs-border-radius: var(--bs-border-radius-lg);
  55. --bs-link-color: var(--bs-gray-800);
  56. }
  57. ```
  58. Without CSS variables, you'd have to use a preprocessor like Sass or write new selectors for every instance of these properties across all components. The former is relatively easy, the latter not so much. CSS variables help solve that.
  59. ## Component variables
  60. On our components, CSS variables get even more power for customizing. Nearly everything under the Components section in our docs sidebar now has CSS variables available to you:
  61. - [Accordion](https://getbootstrap.com/docs/5.2/components/accordion/)
  62. - [Alerts](https://getbootstrap.com/docs/5.2/components/alerts/)
  63. - [Badge](https://getbootstrap.com/docs/5.2/components/badge/)
  64. - [Breadcrumb](https://getbootstrap.com/docs/5.2/components/breadcrumb/)
  65. - [Buttons](https://getbootstrap.com/docs/5.2/components/buttons/)
  66. - [Button group](https://getbootstrap.com/docs/5.2/components/button-group/)
  67. - [Card](https://getbootstrap.com/docs/5.2/components/card/)
  68. - [Carousel](https://getbootstrap.com/docs/5.2/components/carousel/)
  69. - [Collapse](https://getbootstrap.com/docs/5.2/components/collapse/)
  70. - [Dropdowns](https://getbootstrap.com/docs/5.2/components/dropdowns/)
  71. - [List group](https://getbootstrap.com/docs/5.2/components/list-group/)
  72. - [Modal](https://getbootstrap.com/docs/5.2/components/modal/)
  73. - [Navbar](https://getbootstrap.com/docs/5.2/components/navbar/)
  74. - [Navs & tabs](https://getbootstrap.com/docs/5.2/components/navs-tabs/)
  75. - [Offcanvas](https://getbootstrap.com/docs/5.2/components/offcanvas/)
  76. - [Pagination](https://getbootstrap.com/docs/5.2/components/pagination/)
  77. - [Placeholders](https://getbootstrap.com/docs/5.2/components/placeholders/)
  78. - [Popovers](https://getbootstrap.com/docs/5.2/components/popovers/)
  79. - [Progress](https://getbootstrap.com/docs/5.2/components/progress/)
  80. - [Spinners](https://getbootstrap.com/docs/5.2/components/spinners/)
  81. - [Toasts](https://getbootstrap.com/docs/5.2/components/toasts/)
  82. - [Tooltips](https://getbootstrap.com/docs/5.2/components/tooltips/)
  83. _Scrollspy and close button have no relevant CSS variables, so they're excluded here._
  84. Throughout our documentation you'll find examples of customizing our default components by overriding their CSS variables. One great example comes from our own docs where we write our own button styles to [create a purple button](https://getbootstrap.com/docs/5.2/components/buttons/#variables).
  85. ```css
  86. .btn-bd-primary {
  87. --bs-btn-font-weight: 600;
  88. --bs-btn-color: var(--bs-white);
  89. --bs-btn-bg: var(--bd-violet);
  90. --bs-btn-border-color: var(--bd-violet);
  91. --bs-btn-border-radius: .5rem;
  92. --bs-btn-hover-color: var(--bs-white);
  93. --bs-btn-hover-bg: #{shade-color($bd-violet, 10%)};
  94. --bs-btn-hover-border-color: #{shade-color($bd-violet, 10%)};
  95. --bs-btn-focus-shadow-rgb: var(--bd-violet-rgb);
  96. --bs-btn-active-color: var(--bs-btn-hover-color);
  97. --bs-btn-active-bg: #{shade-color($bd-violet, 20%)};
  98. --bs-btn-active-border-color: #{shade-color($bd-violet, 20%)};
  99. }
  100. ```
  101. Which looks like this:
  102. ![Custom Bootstrap docs button](/assets/img/2022/05/docs-custom-button.png)
  103. Another great example is from [our tooltips](https://getbootstrap.com/docs/5.2/components/tooltips/#custom-tooltips). You can add custom classes to tooltips and popovers in Bootstrap with `data-bs-custom-class="custom-tooltip"`. Then, with one CSS variable, you can change the tooltip background and arrow color.
  104. ```css
  105. .custom-tooltip {
  106. --bs-tooltip-bg: var(--bs-primary);
  107. }
  108. ```
  109. Which looks like this:
  110. ![Custom tooltip](/assets/img/2022/05/docs-custom-tooltip.png)
  111. There are dozens of CSS variables in play across our components. All of them are referenced in a new section on the relevant docs page. For example, [here are our modal CSS variables](https://getbootstrap.com/docs/5.2/components/modal/#variables). This is in addition to all the Sass variables, mixins, loops, and maps used for each component.
  112. ## Utility variables
  113. Not every utility class uses CSS variables, but the ones that do gain a good amount of power and customization. [Background](https://getbootstrap.com/docs/5.2/utilities/background/), [border](https://getbootstrap.com/docs/5.2/utilities/borders/), and [color](https://getbootstrap.com/docs/5.2/utilities/colors/) utilities all have what we call "local CSS variables" to improve their usefulness. Each of them uses CSS variables to customize the alpha transparency value of `rgba()` colors.
  114. Consider our [background color utilities](https://getbootstrap.com/docs/5.2/utilities/background/), `.bg-*`. By default each utility class has a local variable, `--bs-bg-opacity` with a default value of `1`. To change the background utility alpha value, you can override that value with your own styles, or some new `.bg-opacity-*` utilities.
  115. ```html
  116. <div class="p-3 bg-success bg-opacity-25">
  117. ...
  118. </div>
  119. ```
  120. Here's how `.bg-success` looks with all our `.bg-opacity-*` classes applied:
  121. ![Background opacity examples](/assets/img/2022/05/bg-opacity.png)
  122. And the same is available for [border color opacity](https://getbootstrap.com/docs/5.2/utilities/borders/#opacity) (`--bs-border-opacity` and `.border-opacity-*`) and [text color opacity](https://getbootstrap.com/docs/5.2/utilities/colors/#opacity) (`--bs-text-opacity` and `.text-opacity-*`). So many color options are now available with these utilities.
  123. By default, we ship with five values for these various opacity utilities.
  124. | Class names | Alpha value |
  125. | --- | --- |
  126. | `.text-opacity-10`<br /> `.bg-opacity-10`<br /> `.border-opacity-10` | `.1` |
  127. | `.text-opacity-25`<br /> `.bg-opacity-25`<br /> `.border-opacity-25` | `.25` |
  128. | `.text-opacity-50`<br /> `.bg-opacity-50`<br /> `.border-opacity-50` | `.5` |
  129. | `.text-opacity-75`<br /> `.bg-opacity-75`<br /> `.border-opacity-75` | `.75` |
  130. | `.text-opacity-100`<br /> `.bg-opacity-100`<br /> `.border-opacity-100` | `1` |
  131. Expect more CSS variables to make their way into our utilities. There's a lot of power in real-time customization, even for what we consider immutable styles.
  132. ---
  133. **Ready to get started with Bootstrap?** Checkout the [quick start guide](https://getbootstrap.com/docs/5.2/getting-started/introduction/#quick-start) so you can put these new CSS variables to work in your next project!