Themes
Themes are used to customize component styles to fit the specific aesthetic of a brand or product.
Resources
Theming basics
Themes are used to modify existing components to fit a specific visual style. By using Carbon’s tokens, developers can easily customize all of their components by changing a set of universal variables, eliminating the need to modify individual components.
Theme terms
Term | Definition | |
---|---|---|
Theme | The set of unique values assigned to the tokens of a Carbon interface | |
Token | The code identifier for a unique role or set of roles. Tokens are universal and never change across themes. | |
Role | The systematic usage(s) of a token. Roles cannot be changed between themes. | |
Value | The actual style (i.e. hex code) assigned to a token |
Default theme
Carbon provides four themes as shown in the color usage page. When carbon-components
is downloaded and installed, the components are preset to use the default (white) theme.
To use the gray 10, gray 90, or gray 100 theme as your default instead of white, set the $carbon--theme
variable to one of the available theme maps ($carbon--theme--g10
, $carbon--theme--g90
, $carbon--theme--g100
) and then include your normal components in your Sass build. These need to be imported before the main Carbon styles.
@import '@carbon/themes/scss/themes';// Use the gray 10 theme$carbon--theme: $carbon--theme--g10;@include carbon--theme();// Use the gray 90 theme$carbon--theme: $carbon--theme--g90;@include carbon--theme();
Customizing a theme
The default theme acts as a starting point; from there designers and developers can define how their own components and styles deviate from the default. Altering one, some, or all of the default token values will result in a new theme. The developer then packages those new values into a new theme SCSS stylesheet which will replace the values of the default theme.
2. Name the mixin:$carbon--theme--white: (interactive-01: #0062ff,interactive-02: #171717,interactive-03: #0062ff,interactive-04: #0062ff,ui-background: #ffffff,ui-01: #f3f3f3,ui-02: #ffffff,ui-03: #dcdcdc,
i.e., $my-theme
@include my-theme();
Alternatively, for relatively minor changes to an existing theme, a developer can make changes on a per-token basis. For example, after importing an existing Carbon theme, she could just set something like $interactive-01: hotpink;
.
Tokens
With tokens, the code only needs to be changed in one place to see the effect system-wide. Tokens are used across all components and help keep global patterns and styles consistent.
All tokens come pre-baked into the Carbon component source code. Tokens are denoted by the prefix $
(e.g. $ui-01
). Tokens can also be nested within other tokens. For example, $interactive-01
calls the IBM Design Language color palette token $ibm-color__blue--60
for its value in the default theme.
There are several token categories:
- Color
- Spacing
- Typography
- Global
Color
Each theme is assigned 52 universal color variables, which are determined by common roles and usage. This allows for uniform color application across themes while maintaining full styling flexibility.
//// _theme-tokens.scss// White theme tokens$interactive-01: #0062ff !default;$interactive-02: #171717 !default;$interactive-03: #0062ff !default;$interactive-04: #0062ff !default;$ui-background: #ffffff !default;$ui-01: #f3f3f3 !default;$ui-02: #ffffff !default;
Spacing
Carbon has two spacing scales, one for general spacing within components and the other for layout spacing. Both are designed to complement the components and typography throughout the system. Each scale has its own distinct purpose. The two scales have certain overlapping values that serve two different roles, so be mindful when choosing a spacing token.
//// _spacing.scss$spacing-baseline: 1rem !default;// Spacing scale$spacing-4xs: $spacing-baseline * 0.0625 !default;$spacing-3xs: $spacing-baseline * 0.125 !default;$spacing-2xs: $spacing-baseline * 0.25 !default;$spacing-xs: $spacing-baseline * 0.5 !default;$spacing-sm: $spacing-baseline * 0.75 !default;
Typography
Typography has four categories of type styles (universal, productive, editorial, and additional) that can be customized through tokens. These tokens are used both within components and across layouts. Type tokens are determined by their role across the system.
// Universal$caption-01: (font-family: carbon--font-family('sans'),font-size: carbon--type-scale(1),font-weight: carbon--font-weight('regular'),line-height: carbon--rem(16px),letter-spacing: 0.32px,) !default;
// Productive$productive-heading-03: (font-family: carbon--font-family('sans'),font-size: carbon--type-scale(5),font-weight: carbon--font-weight('regular'),line-height: carbon--rem(26px),letter-spacing: 0,) !default;
// Expressive$expressive-heading-03: (font-family: carbon--font-family('sans'),font-size: carbon--type-scale(5),font-weight: carbon--font-weight('regular'),line-height: 130%,letter-spacing: 0,breakpoints: (xlg: (
// Additional styles$quotation-01: (font-family: carbon--font-family('serif'),font-size: carbon--type-scale(5),font-weight: carbon--font-weight('regular'),line-height: 130%,letter-spacing: 0,breakpoints: (md: (
Global
The other categories are global and component-specific variables. These control more general styling of components, such as layer usage or border width.
// Global$input-border: 1px solid transparent !default !global;$input-label-weight: 400 !default !global;$disabled: $disabled-02 !default !global;$disabled-background-color: $disabled-01 !default !global;$focus: $ibm-color__blue-60 !default !global;// Link$link-visited: $ibm-color__purple-60 !default !global;
Theming applied
The following example demonstrates the relationship between the different themes. Each theme shares the same variables and roles, with only the value changing for each individual theme.
Key | Token | Role | White theme value | Dark theme value |
---|---|---|---|---|
1 | $text-02 | Label color | #565656 / Gray 70 | #bebebe / Gray 30 |
2 | $text-01 | Primary text color | #171717 / Gray 100 | #f3f3f3 / Gray 10 |
3 | $ui-04 | Border bottom color | #8c8c8c / Gray 50 | #a4a4a4 / Gray 40 |
4 | $icon-01 | Primary icon color | #171717 / Gray 100 | #f3f3f3 / Gray 10 |
5 | $field-01 | Field color | #f3f3f3 / Gray 10 | #3d3d3d / Gray 80 |
6 | $ui-02 | Page background | #ffffff / White | #282828 / Gray 90 |