122

In my Angular2 project I install lastest material plugin from https://material.angular.io/guide/getting-started. Next I add @import '~@angular/material/prebuilt-themes/deeppurple-amber.css'; to my css file for my component. But in my console Angular shows this error:

material.es5.js:148 Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.angular.io/guide/theming`

The material components not working. Whats wrong?

isherwood
  • 58,414
  • 16
  • 114
  • 157
lukassz
  • 3,135
  • 7
  • 32
  • 72
  • If I recall correctly, this file is a SASS file (.scss), maybe you can try with that? –  May 28 '17 at 19:09
  • I don't have any sass file – lukassz May 28 '17 at 19:14
  • No, you don't have sass files, but the angular material module does. That's how you use your own theme (again, if I recall correctly), so try seeing in the node module if it's a SASS or css file. –  May 28 '17 at 19:15
  • 6
    If you're working on a custom theming project, and you added a file to your "angular.json" file, make sure you stop the **ng serve** and restart it for the new "theme.scss" to load. – Jonathan Jun 25 '18 at 01:48

17 Answers17

242

Please insert below code into your styles.css which is located in your src folder.

@import "../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css";

You can select any css under the prebuilt-themes folder.

codelovesme
  • 3,049
  • 1
  • 16
  • 18
  • 2
    Just as @jelbourn mentioned at Issues [4739](https://github.com/angular/material2/issues/4739): Load prebuilt-themes in **Global** (styles.css), rather than in Component's **styleUrls**. – btpka3 Oct 30 '17 at 13:11
  • 26
    Use ~ for easier path to node modules. Example: @import '~@angular/material/prebuilt-themes/deeppurple-amber.css'; – BraveNinja Apr 20 '18 at 18:46
  • As @BraveNinja said, tilde "~" allows you to avoid issues with paths. See https://stackoverflow.com/a/39535907/877464 for a better explanation why. – caruso_g Jun 29 '18 at 12:59
  • why is this not handled by angular add @angular/material itself? – JFFIGK Oct 21 '18 at 00:41
  • 1
    @Trevor the link has become broken, that paragraph is not there anymore – Kirill G. Nov 02 '19 at 01:28
  • 1
    Updated link to docs: https://material.angular.io/guide/theming#using-a-pre-built-theme – Trevor Nov 02 '19 at 19:00
36

put that code into your angular-cli.json file

"styles": [
    "../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css"
  ],

it's works fine for me

Mohammad Zeeshan
  • 825
  • 1
  • 9
  • 20
  • 7
    This is the real correct answer. Never use style.css for import other css. ALWAYS use angular.json file. – Sum_Pie Mar 20 '19 at 17:52
  • 1
    @Bluex why is this better? – Rocco Aug 13 '19 at 07:17
  • @Rocco because it’s like style css in-line.. it works but is not a good solution rather is the worst – Sum_Pie Aug 14 '19 at 08:49
  • 6
    This is the best answer. If you get this error with later versions 9+ after running ```ng add @angular/material``` just terminate the batch job and re-run the app, sometimes the configs aren't fully loaded when compiling. – L. Theodore Obonye Oct 13 '20 at 16:53
  • The latest docs demand this approach ... https://material.angular.io/guide/theming#using-a-pre-built-theme – danday74 Sep 01 '21 at 16:27
19

I got it working with the following steps:

1) Import this (or other) Material theme into your main css file:

@import "../node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css";

2) Also make sure to register this main css file with the app.component file

@Component({
  selector: 'app',
  styleUrls: [
    './app.component.css'
  ]
})

3) Double check that the components you need are imported from @angular/material in your app.module file

import { MdCardModule, MdInputModule } from '@angular/material';
cmcevoy
  • 191
  • 4
9

Add the following line to your src/styles.css

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

You can try other Css classes as well. Here are the available classes:

@import '~@angular/material/prebuilt-themes/indigo-pink.css';

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

@import '~@angular/material/prebuilt-themes/pink-bluegrey.css';

@import '~@angular/material/prebuilt-themes/purple-green.css';

nPcomp
  • 8,637
  • 2
  • 54
  • 49
6

Add:

@import "~@angular/material/prebuilt-themes/indigo-pink.css"

to your style.css

grrigore
  • 1,050
  • 1
  • 21
  • 39
pulkeet
  • 127
  • 1
  • 2
4

If this is happening during testing with Karma, add the following pattern to the files list in your karma.config.js file.

module.exports = function (config) {
   config.set({
    basePath: '',
    ...,
    files: [
        {pattern: './node_modules/@angular/material/prebuilt-themes/indigo-pink.css', included: true, watched: true}
    ],
    ...
}

For more detail see here: https://www.bountysource.com/issues/44073085-a-testing-with-material-components-guide-to-demonstrate-including-core-theme-in-test-suite

The Aelfinn
  • 13,649
  • 2
  • 54
  • 45
4

If you need only the material icons and you don't want import the whole material css use this, in your root css:

/** disable angular mat theme warning */
.mat-theme-loaded-marker {
  display: none;
}
G.Vitelli
  • 1,229
  • 9
  • 18
3

If you're using Angular-CLI, you'll need to make a reference to the themes file, e.g. "candy.scss" in the .angular-cli.json file. Look closely, do you have a *.scss? It's a Sass file. Look here for information: Angular Material 2 Theming instructions. So, in the .angular-cli.json, under the styles property, add "themes/candy.scss", next to the "themes/styles.css". I have a folder in my projects called "themes". I put the styles.css and the candy.scss in the themes folder. Now, Angular-CLI can find your theme.

LargeDachshund
  • 916
  • 3
  • 12
  • 25
3

In addition to @import statements as mentioned above. Please ensure you add encapsulation: ViewEncapsulation.None inside @Component decorator.

@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None})

If you are looking for Angular Material setup for .Net core 1.1 or 2.0 Angular SPA visual studio template. Please find the well documented setup instruction details here.

Venkatesh Muniyandi
  • 5,132
  • 2
  • 37
  • 40
3

Add

@import '@angular/material/prebuilt-themes/deeppurple-amber.css'

to the style.css file, OR add

<link href="node_modules/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">

in the head section of index.html file

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
2

I don't know how many may find this useful, but restarting ng server worked after trying different solutions from Stack overflow.

Deepak
  • 2,660
  • 2
  • 8
  • 23
0

worked for me adding in css section of index.html

 <link href="node_modules/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">

As mentioned here here

naila naseem
  • 577
  • 3
  • 12
  • 21
0

Check any other @imports in your css or scss file. I experienced this issue and could not resolve it until other imports had been removed.

0

Add @import "~@angular/material/prebuilt-themes/indigo-pink.css"; to style.css of your angular project .

In case you want any other theme just folow the steps: Node modules -> @angular -> material -> prebuilt-themes ->

i)deeppurple-amber : @import "~@angular/material/prebuilt-themes/deeppurple-amber.css

ii)indigo-pink : @import "~@angular/material/prebuilt-themes/indigo-pink.css

iii)pink-bluegrey : @import "~@angular/material/prebuilt-themes/pink-bluegrey.css

iv)purple-green : @import "~@angular/material/prebuilt-themes/purple-green.css

And if you want to read you can visit :Theming your Angular Material app

octogenex
  • 137
  • 1
  • 8
0

I had the same error as you and I found the solution in one of the comments. Since that solution worked for me, I want to post it as an answer.

If your project was already running and you installed your material components afterwards, you need to restart your project first. That alone can be the fix already.

0

Material design modules contains scss, migrate css files into scss from following two steps:

  1. npm i schematics-scss-migrate
  2. ng g schematics-scss-migrate:scss-migrate

Hope this works fro everyone :)

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
0

I also encountered the same error once. What worked for me was to stop and restart ng serve. If you are serving your angular app when adding material or making changes in angular.json, you must restart ng serve for changes to take effect.