2016-09-15 14:51:36 -07:00
# Theming your Angular Material app
### What is a theme?
A **theme ** is the set of colors that will be applied to the Angular Material components. The
2016-10-18 16:24:29 -02:00
library's approach to theming is based on the guidance from the [Material Design spec][1].
2016-09-15 14:51:36 -07:00
2016-10-18 16:24:29 -02:00
In Angular Material, a theme is created by composing multiple palettes. In particular,
2016-09-15 14:51:36 -07:00
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.
In Angular Material 2, all theme styles are generated _ statically _ at build-time so that your
app doesn't have to spend cycles generating theme styles on startup.
[1]: https://material.google.com/style/color.html#color -color-palette
### Using a pre-built theme
Angular Material comes prepackaged with several pre-built theme css files. These theme files also
include all of the styles for core (styles common to all components), so you only have to include a
2016-10-18 16:24:29 -02:00
single css file for Angular Material in your app.
2016-09-15 14:51:36 -07:00
2016-10-18 16:24:29 -02:00
You can include a theme file directly into your application from
2016-09-27 16:58:55 +01:00
`@angular/material/core/theming/prebuilt`
2016-09-15 14:51:36 -07:00
If you're using Angular CLI, this is as simple as including one line
in your `style.css` file:
``` css
2016-11-12 14:27:47 -08:00
@ import '~@angular/material/core/theming/prebuilt/deeppurple-amber.css' ;
2016-09-15 14:51:36 -07:00
```
Alternatively, you can just reference the file directly. This would look something like
``` html
2016-11-12 14:27:47 -08:00
< link href = "node_modules/@angular/material/core/theming/prebuilt/indigo-pink.css" rel = "stylesheet" >
2016-10-18 16:24:29 -02:00
```
The actual path will depend on your server setup.
2016-09-15 14:51:36 -07:00
You can also concatenate the file with the rest of your application's css.
### Defining a custom theme
When you want more customization than a pre-built theme offers, you can create your own theme file.
A theme file is a simple Sass file that defines your palettes and passes them to mixins that output
the corresponding styles. A typical theme file will look something like this:
``` scss
2016-11-12 14:27:47 -08:00
@import ' ~@angular/material/core/theming/all-theme ' ;
2016-09-15 14:51:36 -07:00
// Plus imports for other components in your app.
// Include the base styles for Angular Material core. We include this here so that you only
// have to load a single css file for Angular Material in your app.
@include md-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.
2016-10-18 16:24:29 -02:00
$candy-app-primary : md-palette ( $md-indigo ) ;
$candy-app-accent : md-palette ( $md-pink , A200 , A100 , A400 ) ;
2016-09-27 16:58:55 +01:00
// The warn palette is optional (defaults to red).
2016-10-18 16:24:29 -02:00
$candy-app-warn : md-palette ( $md-red ) ;
2016-09-15 14:51:36 -07:00
// Create the theme object (a Sass map containing all of the palettes).
2016-10-18 16:24:29 -02:00
$candy-app-theme : md-light-theme ( $candy-app-primary , $candy-app-accent , $candy-app-warn ) ;
2016-09-15 14:51:36 -07:00
// Include theme styles for core and each component used in your app.
2016-09-27 16:58:55 +01:00
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
2016-10-18 16:24:29 -02:00
@include angular-material-theme ( $candy-app-theme ) ;
2016-09-15 14:51:36 -07:00
```
You only need this single Sass file; you do not need to use Sass to style the rest of your app.
2016-10-18 16:24:29 -02:00
If you are using the Angular CLI, support for compiling Sass to css is built-in; you only have to
add a new entry to the `"styles"` list in `angular-cli.json` pointing to the theme
2016-09-15 14:51:36 -07:00
file (e.g., `unicorn-app-theme.scss` ).
If you're not using the Angular CLI, you can use any existing Sass tooling to build the file (such
as gulp-sass or grunt-sass). The simplest approach is to use the `node-sass` CLI; you simply run:
```
node-sass src/unicorn-app-theme.scss dist/unicorn-app-theme.css
```
and then include the output file in your application.
The theme file can be concatenated and minified with the rest of the application's css.
#### Multiple themes
2016-10-18 16:24:29 -02:00
You can extend the example above to define a second (or third or fourth) theme that is gated by
some selector. For example, we could append the following to the example above to define a
2016-09-15 14:51:36 -07:00
secondary dark theme:
``` scss
. unicorn-dark-theme {
$dark-primary : md-palette ( $md-blue-grey ) ;
$dark-accent : md-palette ( $md-amber , A200 , A100 , A400 ) ;
$dark-warn : md-palette ( $md-deep-orange ) ;
$dark-theme : md-dark-theme ( $dark-primary , $dark-accent , $dark-warn ) ;
2016-10-18 16:24:29 -02:00
@include angular-material-theme ( $dark-theme ) ;
2016-09-15 14:51:36 -07:00
}
```
With this, any element inside of a parent with the `unicorn-dark-theme` class will use this
dark theme.
2016-10-18 16:24:29 -02:00
### Theming your own components
For more details about theming your own components, see [theming-your-components.md ](https://github.com/angular/material2/blob/master/docs/theming-your-components.md )
2016-09-15 14:51:36 -07:00
### Future work
* Once CSS variables (custom properties) are available in all the browsers we support,
we will explore how to take advantage of them to make theming even simpler.