Angular Material2主题设置-如何设置应用背景? [英] Angular Material2 theming - how to set app background?
问题描述
我正在使用angular material2构建angular2应用程序.我正在尝试以正确的方式"设置应用程序的背景,但是我不知道该怎么做.
我发现可以在<body>
元素上使用的类:mat-app-background
我可以添加一个类,它为我提供了默认颜色(取决于我使用的是亮还是暗主题).
我希望定义此背景色以使用品牌的颜色,但我不知道该怎么做.
在_theming.scss
中的定义如下:
// 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);
}
...
}
因此,我认为尝试以某种方式将背景色添加到自定义主题中是很有意义的,但是我不知道该怎么做.
在Material2 主题文档中,它只说:/p>
在Angular Material中,主题是通过组合多个调色板创建的.特别地,主题包括:
- 主要调色板:所有屏幕和组件中使用最广泛的颜色.
- 强调调色板:用于浮动操作按钮和交互式元素的颜色.
- 警告调色板:用于传达错误状态的颜色.
- 前景调色板:文本和图标的颜色.
- 背景调色板:用于元素背景的颜色. "
如何将背景添加到主题中,或以其他任何方式?
如果您想以整洁的方式更改整个应用程序的主题背景色,则可以使用以下内容覆盖主题.
// 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));
这假设您已经使用mat-light-theme
或mat-dark-theme
设置了$theme
.当然,您可以用$mat-blue-grey
代替您选择的颜色图.
这里是我如何使用它的完整示例.我在名为angular.json
的文件中包含以下内容,该文件包含在我的angular.json
样式"条目中:
// 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);
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?
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);
这篇关于Angular Material2主题设置-如何设置应用背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!