Colors in code
This article assumes that you have already defined the colors you will use with the help of the Theme Builder.
HTML data attributes are used to define the color of a component. The components can use all colors in the theme.
When setting the color on a component, there are two data attributes that can be used:
data-color-schemewhich changes the color modedata-colorwhich changes the color
data-color-scheme
This data attribute is used to switch between light, dark, or other color modes. Content that is a child of this data attribute will get the mode specified by the parent. By default, light mode will be used.
You can change data-color-scheme on all or parts of the page and switch between different color modes as often as you want. For example, the main page can be light,
but with a dark footer. You can then set data-color-scheme="dark" on the footer.
data-color-scheme can be set to light, dark, or auto. auto will follow the user's system settings.
data-color
This data attribute is used to change the color of a component. You can use all colors that exist in your theme.
Some components can inherit color from the nearest parent element that has data-color defined.
This means you don't need to use data-color on all components in a hierarchy if they should have the same color.
How components are affected
We divide components into 3 groups for color:
- Explicit
- Neutral
- Cascading
Explicit
Components used for validation or system alerts follow their own dedicated system colors and do not inherit color from the nearest parent with data-color set.
The dedicated system colors are success, warning, danger, and info.
These are components like:
AlertValidationMessageErrorSummary
Neutral
These are components that should always be neutral and are not affected by inherited color or direct data-color. These are components like Modal and Tooltip.
They always use the neutral color.
Cascading
This group contains the rest of the components and will inherit color from the nearest parent with data-color set.
Use data-color-scheme and data-color together
You can use data-color-scheme and data-color together to change both the color mode and color of components.
data-color sets variables that are reset by data-color-scheme. If you change the color mode, you must set data-color again.
Brief summary
You change color with data-color, but this must be set again if you change the color mode with data-color-scheme. Some components inherit color from the nearest parent with data-color set.