skip to content

A university course-registration portal restyled Angular Material buttons with `::ng-deep .mdc-button--unelevated` rules, and the brand color vanished after an upgrade; why, and what is the supported fix?

level: seniorimportance: must knowfreq 41%

answer

  1. who promised those class names?
  2. DOM and classes are implementation details
  3. Material 15 rewrote the DOM
  4. tokens are the public styling contract
  5. button-overrides for the filled appearance

basics

~20 s

Angular Material treats each component's DOM structure and CSS classes as private details that can change in any release, so selectors aimed at them break without warning. The supported fix is the overrides mixins, such as mat.button-overrides, or the --mat-sys variables.

solid answer

~50 s

Classes such as `.mdc-button--unelevated` or `.mat-mdc-unelevated-button` are how Material builds the button internally, and its theming guide says plainly that component DOM and CSS classes are private and may change at any time. Releases have done exactly that: 15.0.0 re-implemented buttons, cards, menus, tabs and more on MDC with new DOM and classes, and 19.0.0 changed where tokens and some styles are emitted, which moved specificity. A rule that no longer matches is valid CSS, so nothing fails; the color just disappears. The fix is to delete the rules and express the brand book through the public API: `mat.button-overrides((filled-container-color: ..., filled-label-text-color: ..., filled-container-shape: ...))` for the filled appearance, or `mat.theme-overrides((primary: ...))` when the brand color should flow into every component. The mixins validate their keys, so a future token rename fails the build instead of the page.

code

scss · 14 lines
scss
// Before: depends on private classes
:host ::ng-deep .mdc-button--unelevated {
  background-color: #7a1f2b !important;
}

// After: the public token API
@use '@angular/material' as mat;

html {
  @include mat.button-overrides((
    filled-container-color: #7a1f2b,
    filled-label-text-color: #ffffff,
  ));
}

go deeper

for a junior

Remember that Material's internal classes and DOM are private and that its overrides mixins and system variables are the supported way to restyle it.

for a middle

Explain how a restyle aimed at internal classes fails silently, and how to map a rule to a mat.button-overrides token.

for a senior

Walk through the 15.0.0 MDC rewrite and the 19.0.0 emission changes as failure modes, plan the migration to tokens, and explain why validated keys make upgrades safe.

for a principal

Set the team rule that Material is styled only through tokens and inputs, and decide when an unmet design need justifies a wrapper or a CDK-based component.

## What "private" means in Angular Material Every Angular Material component renders some internal DOM and puts CSS classes on it. A button, for example, gets classes such as `mdc-button`, `mat-mdc-button-base` and, for the filled appearance, `mdc-button--unelevated` and `mat-mdc-unelevated-button`, plus inner elements such as a label span and a focus-indicator span. Angular Material's theming guide is explicit that this structure and these classes are **private implementation details that may change at any time**, and that overriding component CSS outside the theming APIs is strongly discouraged and not directly supported. Private means no compatibility promise. A minor or major release may rename a class, restructure the template, change which element carries a style or change the order in which styles load. None of that counts as a breaking change to Material's public API, because the public styling API is something else. ## Why the portal's rules broke The registration portal used `::ng-deep` (the selector Angular offers for reaching past view encapsulation, which the Angular team discourages) to reach inside `mat-button` and recolor it. Several kinds of release change can defeat that kind of rule: 1. **Class or DOM changes.** Release 15.0.0 re-implemented buttons, cards, dialogs, menus, selects, tabs, tables and other components on MDC; its notes say the TypeScript API stayed largely the same but the DOM and CSS classes changed. Every selector written against the old classes stopped matching. 2. **Load order and specificity changes.** Release 19.0.0 loaded overlay and ripple styles later, which changed their specificity, and began emitting tokens in place rather than hoisting them, so some hand-written token declarations stopped applying. 3. **Token renames.** A rule that sets a `--mat-button-*` variable by hand bypasses validation; if the token is renamed or moved, the declaration silently does nothing. In every case the browser sees valid CSS that simply does not affect the button. No compiler error, no console warning: the brand color is just gone. ## The supported styling surface | Need | Public API | |---|---| | One component's colors, shapes or type | `mat.<component>-overrides`, such as `mat.button-overrides` | | A theme-wide value, such as the brand primary | `mat.theme-overrides` or the `$overrides` map of `mat.theme` | | Your own components matching the theme | `var(--mat-sys-*)` in your stylesheets | | Appearance choices | component inputs, such as `matButton="filled"` or a card's `appearance` | The overrides mixins check their keys. A misspelled or retired token name fails the Sass build with an error listing the valid names, so an upgrade that renames a token tells you where to look. ## Migrating the portal 1. Delete the `::ng-deep` rules and any `!important` declarations aimed at Material's classes. 2. Map each brand-book rule to a token. For the filled buttons: `filled-container-color`, `filled-label-text-color` and `filled-container-shape`. For outlined buttons the `outlined-*` group, for cards `elevated-container-shape` and friends. 3. Decide the scope. Brand colors that should reach every component usually belong in system values; one-off rules belong in a component mixin under the selector that needs them. 4. Build. Any key that is wrong fails now, not on the next upgrade. ```scss @use '@angular/material' as mat; html { @include mat.button-overrides(( filled-container-color: #7a1f2b, filled-label-text-color: #ffffff, filled-container-shape: 4px, )); } ``` ## Finding private-DOM styling in a codebase Before an upgrade, search the stylesheets for the patterns that depend on Material's internals: - `::ng-deep` followed by a Material selector; - selectors that start with `.mat-mdc-` or `.mdc-`, wherever they appear, including the global stylesheet; - `!important` declarations next to either of those; - hand-written declarations of `--mat-<component>-*` variables outside an overrides mixin. Each hit is a rule to map to a token or to retire. A team that clears the list once and then rejects new hits in code review turns every later Material upgrade into a build that either passes or names the token it needs. ## What still needs care - The overrides cover tokenized properties: color, typography, shape, density and the like. A layout change Material does not expose as a token is a sign to wrap the component in your own markup, or to build on the CDK, rather than to reach into its DOM. - A plain color in an override replaces the theme's light and dark pair, so pick values that work in both schemes or pass `light-dark()`. ## Common mistakes - Treating `mdc-*` classes as stable because they come from a design-spec library; they are as private as the `mat-mdc-*` ones. - Pinning Material's version to keep the hacks alive, which only postpones the break and blocks fixes. - Replacing `::ng-deep` with `ViewEncapsulation.None` and the same selectors; the selectors are still private.

  • Is setting `--mat-button-filled-container-color` directly in a stylesheet just as safe as using the mixin?
    No. It works today, but the theming guide asks you to set Material's variables through the overrides API. A hand-written declaration skips key validation, so if the token is renamed or moved the rule silently stops applying, and since 19.0.0 tokens are emitted in place, so declaration order relative to a theme mixin can matter.
  • When should the portal use `mat.theme-overrides((primary: ...))` instead of button overrides?
    When the brand color should reach every component that uses the primary role: filled buttons, checkboxes, sliders, selected states and custom styles reading `--mat-sys-primary`. Button overrides change only buttons, so using them for a brand color means repeating it in every component mixin.
  • What do you do when the brand book asks for something no token covers?
    Treat it as a sign the component is the wrong fit: wrap it in your own markup and style the wrapper, choose a different appearance input, or build the widget on the CDK. Reaching into the DOM again recreates the upgrade problem.

  • Treats mdc-* class names as a stable public styling API.
  • Fixes the break by adding !important to the old selectors.
  • Pins the Material version to keep the overrides working.
  • Swaps ::ng-deep for ViewEncapsulation.None but keeps the private selectors.
  • Expects the build to report a selector that no longer matches.