20

I am trying to change the default primary color in Angular Material 2 (alpha 6).

I got some idea from this official doc.

I located _default-theme.scss from node_modules > @angular2-material > core > style and replaced the color teal by purple in the following line.

$md-primary: md-palette($md-teal, 500, 100, 700, $md-contrast-palettes);

But the color teal is still shown in the page and pink never appears. What am I missing? How can I change the primary color?

(I am using Angular Material 2 alpha 6 with Angular 2 RC4)

1
  • 1
    You can't do that. That is just the source code. But actual material component you will use is already built into js with the built css styles already embedded. Currently there isn't support for changing variables through sass. You can follow along this issue (which I'm sure many are anticipating the resolution of). Commented Aug 3, 2016 at 4:46

2 Answers 2

45

Angular 4+ and Angular Material 2.0.0 beta 10, Angular CLI 1.3+

You will have to create a scss file "exampleTheme.scss" , you add it to your angular-cli.json angular-cli.json:

"styles": [
    //if you are using --scss the file is called style.scss
    "styles.css",
    "customTheme.scss"
 ],

In your customTheme.scss you can change your colors in use of the following code:

@import '~@angular/material/theming';
@include mat-core();

  $primary: mat-palette($mat-blue,200);
  $accent: mat-palette($mat-orange,200);

  $theme: mat-light-theme($primary, $accent);

@include angular-material-theme($theme);

Multiple Themes If you want to have an additional theme please read the guide linked in the end. But here a small overview you just have to replicate the theming process with new variables and colors

  $dark-primary: mat-palette($mat-blue,800);
  $dark-accent: mat-palette($mat-orange,800);

  $dark-theme: mat-dark-theme($primary, $accent);

and add

  .material-dark-theme {
    @include angular-material-theme($dark-theme);
  }

for more detailed information you should read the angular material theme guide

Sign up to request clarification or add additional context in comments.

5 Comments

Daniel, thanks for the in depth explanation. do you import $theme into style.css or does the app just see your reference in angular-cli.json?
The above answer is the most correct approach for the Angular CLI. For the latest documentation see github.com/angular/material2/blob/master/guides/theming.md; note the variable prefixes have been changed. @LargeDachshund If you are using the Angular CLI it will automagically be injected into the DOM by when transpiled.
Right solution, I would like to add that for Angular 4 and Angular Material 2.0 there is an update at material.angular.io/guide/theming , the process is the same but code changed a little.
updated the post accordingly, would you take a look ? Did I forget something ?
Is it possible to change custom code like #CE2029
0

I just overcame this problem today...

As the comment above states, it's not possible officially yet but you can overwrite the colours individually in sass files. It's a bit of work but does work very well.

My scss file structure

/scss
--/angular-material
-- _button.scss
-- _checkbox.scss
-- _input.scss
--  //etc
-- angular-material.scss // I @import all angular-material files into this
 main.scss // angular-material is imported into my main scss file...

As an example... my angular-material/_input.scss looks like this:

:root md-input {

@extend %typo-base;
padding-top: 1px;

.md-input-placeholder {
  color: $light-grey-color;
}

.md-input-placeholder.md-focused {
  color: $primary-color;
}

.md-input-underline {
  border-color: $light-grey-color;
}

.md-input-underline .md-input-ripple {
  background-color: $primary-color;
}

//...etc
}

The :root selector pierces the shadow dom to override the styles. The colour variables are imported from my main theme into the angular-material.scss file so they can be used in any of the material override files.

Hope this helps.

2 Comments

This looks promising. Of course, its not ideal, since css gets bloated with redundant styles, but hopefully soon there will be an easy and efficient way to modify ng2-material styles
Definitely not ideal! But at least if/when they do introduce styling these styles can easily be deleted (as they are kept seperate from the rest of the app styles) and proper theming will easily be able to be added with whatever system they introduce. Until then... this is the best I could come up with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.