System Variables
Angular Material components depend on system variables defined as CSS variables through the mat.theme Sass mixin. That means, we can use the CSS variables defined by mat.theme to apply Angular Material theme’s colors and typography to our application.
The color variables are useful for emphasizing important text and actions, providing stronger application branding, and ensuring strong contrast ratios between surface and on-surface elements.
The typography variables are useful for creating clear information hierarchy and text consistency through the application.
The following example styles demonstrate a component using the color and typography variables to create an application-wide banner presenting important information to the user:
:host { background: var(--mat-sys-primary-container); color: var(--mat-sys-on-primary-container); border: 1px solid var(--mat-sys-outline-variant); font: var(--mat-sys-body-large);}Colors
Material Design uses color to create accessible, personal color schemes that communicate your product’s hierarchy, state, and brand. See Material Design’s Color System page to learn more about its use and purpose.
General concepts
Here are helpful-to-know words you’ll see in the names of color roles:
- Surface - A role used for backgrounds and large, low-emphasis areas of the screen.
- Primary, Secondary, Tertiary - Accent color roles used to emphasize or de-emphasize foreground elements.
- Container - Roles used as a fill color for foreground elements like buttons. They should not be used for text or icons.
- On - Roles starting with this term indicate a color for text or icons on top of its paired parent color. For example, on primary is used for text and icons against the primary fill color.
- Variant - Roles ending with this term offer a lower emphasis alternative to its non-variant pair. For example, outline variant is a less emphasized version of the outline color.
The following color roles are the most often used in Angular Material components. Use these colors and follow their uses to add theme colors to your application’s custom components.
Primary
Use primary roles for the most prominent components across the UI, such as the FAB, high-emphasis buttons, and active states.
| Role and description | CSS Variable | Usage in Angular Material |
|---|---|---|
| Primary - High-emphasis fills, texts, and icons against surface | --mat-sys-primary | The most common color used by Angular Material components to participate in the application theme. Examples include the background color of filled buttons, the icon color of selected radio buttons, and the outline color of form fields. |
| On primary - Text and icons against primary | --mat-sys-on-primary | Use this color for icons, text, and other visual elements placed on a primary background. This color is calculated to be optimal for accessibility and legibility. |
| Primary container - Standout fill color against surface, for key components like FAB | --mat-sys-primary-container | Less commonly used in Angular Material. |
| On primary container - Text and icons against primary container | --mat-sys-on-primary-container | Less commonly used in Angular Material. |
Surface
Use surface roles for more neutral backgrounds, and container colors for components like cards, sheets, and dialogs.
| Role and description | CSS Variable | Usage in Angular Material |
|---|---|---|
| Surface - Default color for backgrounds. | --mat-sys-surface | A low-emphasis background color that provides a clear contrast for both light and dark themes and their varied theme colors. Examples include the background color of the application and most components such as the dialog, card, table, and more. |
| On surface - Text and icons against any surface color | --mat-sys-on-surface | Use this color for icons, text, and other visual elements placed on a surface background. This color is calculated to be optimal for accessibility and legibility. |
| On surface variant - Lower-emphasis color for text and icons against any surface color | --mat-sys-on-surface-variant | Less commonly used in Angular Material. |
The following colors should be used for backgrounds and large, low-emphasis areas of the screen.
Containers filled with a surface color should apply the --mat-sys-on-surface color to text and icons placed within.
| Role and description | CSS Variable |
|---|---|
| Surface dim - Dimmest surface color in light and dark themes | --mat-sys-surface-dim |
| Surface bright - Brightest surface color in light and dark themes | --mat-sys-surface-bright |
| Surface container lowest - Lowest-emphasis container color | --mat-sys-surface-container-lowest |
| Surface container low - Low-emphasis container color | --mat-sys-surface-container-low |
| Surface container - Default container color | --mat-sys-surface-container |
| Surface container high - High-emphasis container color | --mat-sys-surface-container-high |
| Surface container highest - Highest-emphasis container color | --mat-sys-surface-container-highest |
Error
Use error roles to communicate error states, such as an incorrect password entered into a text field.
| Role and description | CSS Variable | Usage in Angular Material |
|---|---|---|
| Error - Attention-grabbing color against surface for fills, icons, and text, indicating urgency | --mat-sys-error | High-contrast color meant to alert the user to attract immediate attention. Examples include the background color of the badge and the text color of invalid form fields inputs. |
| On error - Text and icons against error | --mat-sys-on-error | Use this color for icons, text, and other visual elements placed on an error background. This color is calculated to be optimal for accessibility and legibility. |
| Error container - Attention-grabbing fill color against surface | --mat-sys-error-container | Less commonly used in Angular Material. |
| On error container - Text and icons against error container | --mat-sys-on-error-container | Less commonly used in Angular Material. |
Outline
There are two outline colors to be used against surface:
| Role and description | CSS Variable | Usage in Angular Material |
|---|---|---|
| Outline - Important boundaries, such as a text field outline | --mat-sys-outline | Used for borders and dividers to help provide visual separation between and around elements. Examples include the color of the divider and border color of an outlined form field. |
| Outline variant - Decorative elements, such as dividers | --mat-sys-outline-variant | Use this color for a less prominent outline. For example, border color for cards. |
Secondary
Use secondary roles for less prominent components in the UI such as filter chips. These colors are less commonly used in Angular Material, but are available for adding color variety and creating additional emphasis to components.
| Role and description | CSS Variable |
|---|---|
| Secondary - Less prominent fills, text, and icons against surface | --mat-sys-secondary |
| On secondary - Text and icons against secondary | --mat-sys-on-secondary |
| Secondary container - Less prominent fill color against surface, for recessive components like tonal buttons | --mat-sys-secondary-container |
| On secondary container - Text and icons against secondary container | --mat-sys-on-secondary-container |
Tertiary
Use tertiary roles for contrasting accents that balance primary and secondary colors or bring heightened attention to an element such as an input field. These colors are less commonly used in Angular Material, but are available for adding color variety and creating additional emphasis to components.
| Role and description | CSS Variable |
|---|---|
| Tertiary - Complementary fills, text, and icons against surface | --mat-sys-tertiary |
| On tertiary - Text and icons against tertiary | --mat-sys-on-tertiary |
| Tertiary container - Complementary container color against surface, for components like input fields | --mat-sys-tertiary-container |
| On tertiary container - Text and icons against tertiary container | --mat-sys-on-tertiary-container |
Fixed and on-fixed accent colors
These colors are the same for both light and dark themes. They are unused by any Angular Material components.
| Role | CSS Variable |
|---|---|
| Primary Fixed | --mat-sys-primary-fixed |
| On primary fixed | --mat-sys-on-primary-fixed |
| Primary Fixed Dim | --mat-sys-primary-fixed-dim |
| On primary fixed dim | --mat-sys-on-primary-fixed-dim |
| Secondary Fixed | --mat-sys-secondary-fixed |
| On secondary fixed | --mat-sys-on-secondary-fixed |
| Secondary Fixed Dim | --mat-sys-secondary-fixed-dim |
| On secondary fixed dim | --mat-sys-on-secondary-fixed-dim |
| Tertiary Fixed | --mat-sys-tertiary-fixed |
| On tertiary fixed | --mat-sys-on-tertiary-fixed |
| Tertiary Fixed Dim | --mat-sys-tertiary-fixed-dim |
| On tertiary fixed dim | --mat-sys-on-tertiary-fixed-dim |
Typography
There are five categories of font types defined by Material Design. Learn more about how these categories and their sizes should be used in your application by visiting Material Design’s Typography documentation.
Typescale levels
displayheadlinetitlebodylabel
Each typescale level has three sizes:
largemediumsmall
Binding typescale level to native HTML elements
By default, Angular Material does not bind any of the typescale levels to any native HTML elements. But, below is the list of typescale levels those can be used to bind to native HTML elements.
| Typescale level | Size | Native Element |
|---|---|---|
display | large | <h1> |
display | medium | <h2> |
display | small | <h3> |
headline | large | <h4> |
headline | medium | <h5> |
headline | small | <h6> |
Below is the list of all typography variables defined by Angular Material. Each system variable can be applied to the font CSS style.
Body
Body styles are used for longer passages of text in your app.
| Size | CSS Variable | Default Value in Angular Material |
|---|---|---|
| small | --mat-sys-body-small | 400 .75rem / 1rem Roboto |
| medium | --mat-sys-body-medium | 400 .875rem / 1.25rem Roboto |
| large | --mat-sys-body-large | 400 1rem / 1.5rem Roboto |
Display
As the largest text on the screen, display styles are reserved for short, important text or numerals. They work best on large screens.
| Size | CSS Variable | Default Value in Angular Material |
|---|---|---|
| small | --mat-sys-display-small | 400 2.25rem / 2.75rem Roboto |
| medium | --mat-sys-display-medium | 400 2.812rem / 3.25rem Roboto |
| large | --mat-sys-display-large | 400 3.562rem / 4rem Roboto |
Headline
Headlines are best-suited for short, high-emphasis text on smaller screens. These styles can be good for marking primary passages of text or important regions of content.
| Size | CSS Variable | Default Value in Angular Material |
|---|---|---|
| small | --mat-sys-headline-small | 400 1.5rem / 2rem Roboto |
| medium | --mat-sys-headline-medium | 400 1.75rem / 2.25rem Roboto |
| large | --mat-sys-headline-large | 400 2rem / 2.5rem Roboto |
Label
Label styles are smaller, utilitarian styles, used for things like the text inside components or for very small text in the content body, such as captions.
Buttons, for example, use the label large style.
| Size | CSS Variable | Default Value in Angular Material |
|---|---|---|
| small | --mat-sys-label-small | 500 .688rem / 1rem Roboto |
| medium | --mat-sys-label-medium | 500 .75rem / 1rem Roboto |
| large | --mat-sys-label-large | 500 .875rem / 1.25rem Roboto |
Title
Titles are smaller than headline styles, and should be used for medium-emphasis text that remains relatively short. For example, consider using title styles to divide secondary passages of text or secondary regions of content.
| Size | CSS Variable | Default Value in Angular Material |
|---|---|---|
| small | --mat-sys-title-small | 500 .875rem / 1.25rem Roboto |
| medium | --mat-sys-title-medium | 500 1rem / 1.5rem Roboto |
| large | --mat-sys-title-large | 400 1.375rem / 1.75rem Roboto |
Accessing the parts of the variable definition
Additionally, the parts of the variable definition can be accessed individually by appending the keywords font, line-height, size, tracking, and weight.
To get the needed CSS variable, you can keep 3 things in mind and it will help you get the correct CSS variable:
- Pre-typescale levels
displayheadlinetitlebodylabel
- Variations
largemediumsmall
- Properties
font(The CSS font shorthand, includes all font properties except letter-spacing) - No tokenfont-family-fontfont-size-sizefont-weight-weightline-height-line-heightletter-spacing-tracking
--mat-sys-* variables are available for each of the above typescale, which you can use to read or modify the typescale.
Now, just use below format to get the correct CSS variable:
.some-class { some-property: var( --mat-sys-<pre_typescale_level>-<variation>-<property_token> );}So, for example, to get font of display-large, you would write CSS like below:
.display-large-clone { font: var(--mat-sys-display-large);
/* As --sys-display-large-font does not include letter-spacing, make sure to include that, too */ letter-spacing: var(--mat-sys-display-large-tracking);}One more example, to get font-weight of headline of small size, you will write CSS like below:
.headline-small-font-weight { font-weight: var(--mat-sys-headline-small-weight);}One more example, the values for medium body text may be defined as follows:
--mat-sys-body-medium: 400 0.875rem / 1.25rem Roboto, sans-serif;--mat-sys-body-medium-font: Roboto, sans-serif;--mat-sys-body-medium-line-height: 1.25rem;--mat-sys-body-medium-size: 0.875rem;--mat-sys-body-medium-tracking: 0.016rem;--mat-sys-body-medium-weight: 400;Toolbar Colors
Let’s modify color and background color of the toolbar.
.mat-toolbar { background-color: var(--mat-sys-primary-container); color: var(--mat-sys-on-primary-container);}Theme Toggle Border Color
To make theme toggle border color more visible, let’s modify the outline color of the theme toggle.
:host { outline: 1px solid #dee2e6; outline: 1px solid var(--mat-sys-outline);}Application Colors
Let’s change the text and other colors of the application to match the theme.
.due-date { color: rgba(0, 0, 0, 0.7); color: color-mix(in srgb, var(--mat-sys-on-surface), transparent 30%);}
.todo-card { &.cdk-drag-preview { box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2), 0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12); box-shadow: 0 5px 5px -3px color-mix(in srgb, var(--mat-sys-shadow), transparent 80%), 0 8px 10px 1px color-mix(in srgb, var(--mat-sys-shadow), transparent 86%), 0 3px 14px 2px color-mix(in srgb, var(--mat-sys-shadow), transparent 88%); }}.empty-state { color: rgba(0, 0, 0, 0.6); color: color-mix(in srgb, var(--mat-sys-on-surface), transparent 40%);}
.completed-header { color: rgba(0, 0, 0, 0.6); color: color-mix(in srgb, var(--mat-sys-on-surface), transparent 40%); border-top: 1px solid rgba(0, 0, 0, 0.12); border-top: 1px solid color-mix(in srgb, var(--mat-sys-outline), transparent 88%);}Application Typography
Let’s use Angular Material’s typography to style the no todos text.
.empty-state { h2 { font: var(--mat-sys-display-small); letter-spacing: var(--mat-sys-display-small-tracking); }
p { font: var(--mat-sys-title-small); letter-spacing: var(--mat-sys-title-small-tracking); }}And also completed header text.
.completed-header { // rest remains same font: var(--mat-sys-title-small); letter-spacing: var(--mat-sys-title-small-tracking);}Customizing Tokens
Angular Material components also allow for narrowly targeted customization of specific tokens through the overrides mixins. This enables fine-grained adjustments to specific system-level theme CSS variables as well as individual component tokens, such as a component’s border-color or title font size.
The overrides API validates that the customized tokens are correctly spelled and can be used to ensure backwards compatibility if tokens are added, moved, or renamed in future versions.
System Tokens
System-level tokens can be changed to different values through the mat.theme-overrides mixin, which will redefine CSS variables that are used in the application.
The following example applies a violet color palette for the application, but alters the primary-container token to a specific shade of blue.
@use "@angular/material" as mat;
html { color-scheme: light dark; @include mat.theme( ( color: mat.$violet-palette, typography: Roboto, density: 0, ) );
.example-orange-primary-container { @include mat.theme-overrides( ( primary-container: #84ffff, ) ); }}Alternatively, an optional override map can be provided in the mat.theme mixin to replace values applied by the mixin:
@use '@angular/material' as mat;
html { color-scheme: light dark; @include mat.theme(( color: mat.$violet-palette, typography: Roboto, density: 0 ), $overrides: ( primary-container: orange, );}Component Tokens
Each Angular Material component defines an overrides mixin that can be used to customize tokenized styles for their color, typography, and density.
More information for each component’s override API, including their list of available customizable tokens, can be found on their respective documentation page under the Styling tab.
The following example demonstrates the Card’s overrides API to change the background color to red, increase the corner border radius, and specify a larger title font size.
html { @include mat.card-overrides( ( elevated-container-color: red, elevated-container-shape: 32px, title-text-size: 2rem, ) );}When to use what?
With lot of mixins for theming overrides and customizations it can become confusing to decide when to use which so I will try to summarize which mixin to use in which scenario.
| Index | Mixin | Use Case | Ideal number of usage |
|---|---|---|---|
| 1 | mat.theme | Theme for application | 1 |
| 2 | mat.theme-overrides | Change system-level tokens. Example: a success theme with green color | Equals to contextual themes in application |
| 3 | mat.<COMPONENT>-overrides | Change component-level tokens. Example: emphasis button’s colors to indicate newly added feature, like chat with AI | As and when needed |
Add Todo Button Overrides
We will use the mat.fab-overrides mixin to change the tokens of the add todo button.
@use "@angular/material" as mat;.add-todo-fab { // rest remains same @include mat.fab-overrides( ( container-shape: 100%, container-color: var(--mat-sys-tertiary-container), foreground-color: var(--mat-sys-on-tertiary-container), state-layer-color: var(--mat-sys-on-tertiary-container), ) );}Todo Card Component
Let’s modify colors of priority chips in todo cards.
@use "@angular/material" as mat;
// rest remains same
$priority-colors: ( low: ( outline-color: var(--mat-sys-primary), background: var(--mat-sys-surface-container), foreground: var(--mat-sys-on-surface-container), ), medium: ( outline-color: var(--mat-sys-secondary), background: var(--mat-sys-secondary-container), foreground: var(--mat-sys-on-secondary-container), ), high: ( outline-color: var(--mat-sys-error), background: var(--mat-sys-error-container), foreground: var(--mat-sys-on-error-container), ),);
@each $priority, $colors in $priority-colors { .priority-chip.priority-#{$priority} { background-color: map-get($colors, background); @include mat.chips-overrides( ( outline-color: map-get($colors, outline-color), label-text-color: map-get($colors, foreground), ) ); }}Then modify the class of the priority chip to apply the overrides in file src/app/components/todo-list-section/todo-list-section.component.html
<mat-chip class="priority-chip"><mat-chip [class]="'priority-chip priority-' + todo.priority"> {{ todo.priority }}</mat-chip>