site/src/content/docs/utilities/colors.mdx
import { getData } from '@libs/data'
<Callout name="warning-color-assistive-technologies" />Colorize text with color utilities. If you want to colorize links, you can use the .link-* helper classes which have :hover and :focus states.
<Example code={[
...getData('theme-colors').map((themeColor) => <p class="text-${themeColor.name}${themeColor.contrast_color ? bg-${themeColor.contrast_color}` : ``}">.text-${themeColor.name}</p>
As of v5.1.0, text color utilities are generated with Sass using CSS variables. This allows for real-time color changes without compilation and dynamic alpha transparency changes.
Consider our default .text-primary utility.
.text-primary {
--bs-text-opacity: 1;
color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important;
}
We use an RGB version of our --bs-primary (with the value of 13, 110, 253) CSS variable and attached a second CSS variable, --bs-text-opacity, for the alpha transparency (with a default value 1 thanks to a local CSS variable). That means anytime you use .text-primary now, your computed color value is rgba(13, 110, 253, 1). The local CSS variable inside each .text-* class avoids inheritance issues so nested instances of the utilities don’t automatically have a modified alpha transparency.
To change that opacity, override --bs-text-opacity via custom styles or inline styles.
<Example code={`<div class="text-primary">This is default primary text</div>
<div class="text-primary" style="--bs-text-opacity: .5;">This is 50% opacity primary text</div>`} />Or, choose from any of the .text-opacity utilities:
<Example code={`<div class="text-primary">This is default primary text</div>
<div class="text-primary text-opacity-75">This is 75% opacity primary text</div> <div class="text-primary text-opacity-50">This is 50% opacity primary text</div> <div class="text-primary text-opacity-25">This is 25% opacity primary text</div>`} />Sometimes contextual classes cannot be applied due to the specificity of another selector. In some cases, a sufficient workaround is to wrap your element’s content in a <div> or more semantic element with the desired class.
In addition to the following Sass functionality, consider reading about our included CSS custom properties (aka CSS variables) for colors and more.
Most color utilities are generated by our theme colors, reassigned from our generic color palette variables.
Grayscale colors are also available, but only a subset are used to generate any utilities.
<ScssDocs name="gray-color-variables" file="scss/_variables.scss" /> <ScssDocs name="theme-text-map" file="scss/_maps.scss" />Variables for setting colors in .text-*-emphasis utilities in light and dark mode:
Theme colors are then put into a Sass map so we can loop over them to generate our utilities, component modifiers, and more.
<ScssDocs name="theme-colors-map" file="scss/_variables.scss" />Grayscale colors are also available as a Sass map. This map is not used to generate any utilities.
<ScssDocs name="gray-colors-map" file="scss/_variables.scss" />RGB colors are generated from a separate Sass map:
<ScssDocs name="theme-colors-rgb" file="scss/_maps.scss" />Color opacities build on that with their own map that’s consumed by the utilities API:
<ScssDocs name="utilities-text-colors" file="scss/_maps.scss" />Color mode adaptive text colors are also available as a Sass map:
<ScssDocs name="theme-text-map" file="scss/_maps.scss" /> <ScssDocs name="theme-text-dark-map" file="scss/_maps.scss" />Color utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.