Angular Material2 theming - how to set app background?
Solution 1
If you want to change the theme's background color for the entire app in a clean way, you can override your theme with the following.
// Set custom background color
$custom-background-color: map_get($mat-blue-grey, 50);
// -or- Can set colour by hex value too
$custom-background-color: #628cc9;
$background: map-get($theme, background);
$background: map_merge($background, (background: $custom-background-color));
$theme: map_merge($theme, (background: $background));
This assumes you have already set up your $theme
using mat-light-theme
or mat-dark-theme
. Of course you can substitute $mat-blue-grey
for a color map of your choosing.
Here is a full example of how I am using this. I have the following in a file called theme.scss
, which is included in my angular.json
"styles" entry:
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core;
// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue.
$primary: mat-palette($mat-red, 600, 400, 900);
$accent: mat-palette($mat-blue-grey, 500, 200, 700);
$background-color: map_get($mat-blue-grey, 50);
// The warn palette is optional (defaults to red).
$warn: mat-palette($mat-blue);
// Create the theme object (a Sass map containing all of the palettes).
$theme: mat-light-theme($primary, $accent, $warn);
// Insert custom background color
$background: map-get($theme, background);
$background: map_merge($background, (background: $background-color));
$theme: map_merge($theme, (background: $background));
// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($theme);
@include my-app-theme($theme);
Solution 2
Not exactly answer to your question, but I guess many people will end up here searching for "how to set app background color".
In your project/index.html
set your body class to mat-app-background
<body class="mat-app-background">
<app-root></app-root>
</body>
And make sure in your project/angular.json
you have:
"styles": [
"./node_modules/@angular/material/prebuilt-themes/YOUR_STYLE.css",
...
],
Solution 3
edit: This strategy involves replacing Material functionality. for most cases, I would recommend Jake Stoeffler's answer above.
If you want to set the background colors, you likely want to customize the entire background and foreground palette by emulating the mat-light-theme or mat-dark-theme functions with your own replacement. Your replacement would include your own palettes instead of the mat-light-theme-foreground and background palettes.
example: https://stackblitz.com/edit/angular-material-custom-background?file=theme.scss
I don't know if this method is recommended or officially supported.
Solution 4
Angular 12
// For example Orange palette
$orange-palette: mat.define-palette(mat.$orange-palette);
// Define theme
$theme: mat.define-dark-theme((
color: (
primary: $orange-palette,
accent: $orange-palette
)
));
// Get color param from our theme
$palette-color : map-get($theme, color);
// Get background param from color param
$background: map-get($palette-color, background);
// $background also has background param contains color, set it to red (for example)
$background: map_merge($background, (background: red));
// Set background param for palette
$palette-color: map_merge($palette-color, (background: $background));
// Set palette for theme
$theme: map_merge($theme, (color: $palette-color));
@include mat.all-component-themes($theme);
Or with function:
@function mat-set-background($theme, $backgroundColor) {
$palette-color : map-get($theme, color);
$background: map-get($palette-color, background);
$background: map_merge($background, (background: $backgroundColor));
$palette-color: map_merge($palette-color, (background: $background));
@return map_merge($theme, (color: $palette-color));
}
$orange-palette: mat.define-palette(mat.$orange-palette);
$theme: mat.define-dark-theme((
color: (
primary: $orange-palette,
accent: $orange-palette
)
));
$theme: mat-set-background($theme, #FF0000);
@include mat.all-component-themes($theme);
Solution 5
see : palette theme scss on github Angular (2) Material (2)
Extract of the code:
// Background palette for light themes.
$mat-light-theme-background: (
status-bar: map_get($mat-grey, 300),
app-bar: map_get($mat-grey, 100),
background: map_get($mat-grey, 50),
hover: rgba(black, 0.04), // TODO(kara): check style with Material Design UX
card: white,
dialog: white,
disabled-button: $black-12-opacity,
raised-button: white,
focused-button: $black-6-opacity,
selected-button: map_get($mat-grey, 300),
selected-disabled-button: map_get($mat-grey, 400),
disabled-button-toggle: map_get($mat-grey, 200),
);
// Background palette for dark themes.
$mat-dark-theme-background: (
status-bar: black,
app-bar: map_get($mat-grey, 900),
background: #303030,
hover: rgba(white, 0.04), // TODO(kara): check style with Material Design UX
card: map_get($mat-grey, 800),
dialog: map_get($mat-grey, 800),
disabled-button: $white-12-opacity,
raised-button: map-get($mat-grey, 800),
focused-button: $white-6-opacity,
selected-button: map_get($mat-grey, 900),
selected-disabled-button: map_get($mat-grey, 800),
disabled-button-toggle: map_get($mat-grey, 1000),
);
// Foreground palette for light themes.
$mat-light-theme-foreground: (
base: black,
divider: $black-12-opacity,
dividers: $black-12-opacity,
disabled: rgba(black, 0.38),
disabled-button: rgba(black, 0.38),
disabled-text: rgba(black, 0.38),
hint-text: rgba(black, 0.38),
secondary-text: rgba(black, 0.54),
icon: rgba(black, 0.54),
icons: rgba(black, 0.54),
text: rgba(black, 0.87),
slider-off: rgba(black, 0.26),
slider-off-active: rgba(black, 0.38),
);
// Foreground palette for dark themes.
$mat-dark-theme-foreground: (
base: white,
divider: $white-12-opacity,
dividers: $white-12-opacity,
disabled: rgba(white, 0.3),
disabled-button: rgba(white, 0.3),
disabled-text: rgba(white, 0.3),
hint-text: rgba(white, 0.3),
secondary-text: rgba(white, 0.7),
icon: white,
icons: white,
text: white,
slider-off: rgba(white, 0.3),
slider-off-active: rgba(white, 0.3),
);
![Narxx](https://i.stack.imgur.com/oYmYL.jpg?s=256&g=1)
Narxx
Software Engineer, with a passion for photography and video games :) Expertise in web front end development, including responsive design, advanced concepts of UI/UX, using VueJS, CSS3, HTML5 and Javascript
Updated on September 22, 2021Comments
-
Narxx almost 3 years
I am building an angular2 app using angular material2. I am trying to set the background of my application "the correct way", but I can't figure out how.
I found a class I can use on my
<body>
element:mat-app-background
which I can add, that gives me a default color (depending on whether I'm using the light or dark themes).I wish to define this background color to use my brands' color, but I cannot figure out how to do it.
In
_theming.scss
it is defined like so:// Mixin that renders all of the core styles that depend on the theme. @mixin mat-core-theme($theme) { @include mat-ripple-theme($theme); @include mat-option-theme($theme); @include mat-pseudo-checkbox-theme($theme); // Wrapper element that provides the theme background when the // user's content isn't inside of a `md-sidenav-container`. .mat-app-background { $background: map-get($theme, background); background-color: mat-color($background, background); } ... }
So I thought it would make sense to try adding the background color to my custom theme, somehow, but I couldn't understand how to do so.
On the Material2 theming documentation it only says:
"In Angular Material, a theme is created by composing multiple palettes. In particular, a theme consists of:
- A primary palette: colors most widely used across all screens and components.
- An accent palette: colors used for the floating action button and interactive elements.
- A warn palette: colors used to convey error state.
- A foreground palette: colors for text and icons.
- A background palette: colors used for element backgrounds. "
How can I add my background to the theme, or do it in any other way?