There seems to be an error in the latest release of angular-cli
with Webpack when it comes to applying global styles. Please refer to this Github issue.
Update: 31 August 2016
Upgrading to the latest version (1.0.0-beta.11-webpack.8
) seems to have fixed the issue with angular-cli
global styles. To set up global styles, follow the instructions here. The default is to use a global CSS file, if you wish to use a global SCSS stylesheet, you can specify the --style=scss
flag when creating your app:
ng new scss-project --style=scss
angular-cli
will generate a global style sheet styles.scss
and include it in the angular-cli.json
file. This indicates to angular-cli
that styles declared in styles.scss
should be global and applied to the entire application (and not scoped to a specific component). This is the equivalent of including a style sheet via a link statement in your index.html
.
For existing projects, you can set the default style using:
ng set defaults.styleExt scss
In your angular-cli.json
, extra global styles sheets or scripts can be added to the styles and scripts arrays shown below:
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": "assets",
"index": "index.html",
"main": "main.ts",
"test": "test.ts",
"tsconfig": "tsconfig.json",
"prefix": "app",
"mobile": false,
"styles": [
"styles.scss"
],
"scripts": [],
"environments": {
"source": "environments/environment.ts",
"prod": "environments/environment.prod.ts",
"dev": "environments/environment.dev.ts"
}
}
]
Why are styles not being applied to your body tag?
I'm guessing here because you haven't posted any code, but the reason your SCSS styles are not being applied to the <body>
tag is because you are using Component Styles
. When generating an angular-cli
project with the --style=scss
flag, it creates a root level component called app.compontent.ts
:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
title = 'app works!';
}
It also generates a Component Style called app.component.scss
. This is an important point, because component styles are scoped to their component. A component style is set in the component like this:
styleUrls: ['app.component.scss']
Any styles you include in app.component.scss
will only be applied to the app.component.ts
and its corresponding template app.component.html
(as well as its children). The reason the styles aren't applied to the <body>
tag is because it lies outside of the root level component's scope in the index.html
file generated by angular-cli
:
<body>
<app-root>Loading...</app-root>
</body>
I hope that answers your question.