diff --git a/aio/content/examples/component-styles/e2e/app.e2e-spec.ts b/aio/content/examples/component-styles/e2e/app.e2e-spec.ts index 986bd2b494..910f9351d4 100644 --- a/aio/content/examples/component-styles/e2e/app.e2e-spec.ts +++ b/aio/content/examples/component-styles/e2e/app.e2e-spec.ts @@ -13,7 +13,7 @@ describe('Component Style Tests', function () { let externalH1 = element(by.css('body > h1')); // Note: sometimes webdriver returns the fontWeight as "normal", - // othertimes as "400", both of which are equal in CSS terms. + // other times as "400", both of which are equal in CSS terms. expect(componentH1.getCssValue('fontWeight')).toMatch(/normal|400/); expect(externalH1.getCssValue('fontWeight')).not.toMatch(/normal|400/); }); diff --git a/aio/content/examples/component-styles/plnkr.json b/aio/content/examples/component-styles/plnkr.json index e045ebcb38..fa496c7fa8 100644 --- a/aio/content/examples/component-styles/plnkr.json +++ b/aio/content/examples/component-styles/plnkr.json @@ -4,7 +4,8 @@ "files": [ "!**/*.d.ts", "!**/*.js", - "!**/*.native.*" + "!**/*.native.*", + "!**/*.[1].*" ], "tags": ["CSS"] } diff --git a/aio/content/examples/component-styles/src/app/hero-app.component.1.css b/aio/content/examples/component-styles/src/app/hero-app.component.1.css new file mode 100644 index 0000000000..39671e32a9 --- /dev/null +++ b/aio/content/examples/component-styles/src/app/hero-app.component.1.css @@ -0,0 +1,3 @@ +h1 { + font-weight: normal; +} diff --git a/aio/content/examples/component-styles/src/app/hero-app.component.1.ts b/aio/content/examples/component-styles/src/app/hero-app.component.1.ts new file mode 100644 index 0000000000..a89786c5d3 --- /dev/null +++ b/aio/content/examples/component-styles/src/app/hero-app.component.1.ts @@ -0,0 +1,25 @@ +import { Component, HostBinding } from '@angular/core'; +import { Hero } from './hero'; + +// #docregion +@Component({ + selector: 'app-root', + template: ` +

Tour of Heroes

+ + `, + styleUrls: ['./hero-app.component.css'] +}) +export class HeroAppComponent { +// #enddocregion + hero = new Hero( + 'Human Torch', + ['Mister Fantastic', 'Invisible Woman', 'Thing'] + ); + + @HostBinding('class') get themeClass() { + return 'theme-light'; + } +// #docregion +} +// #enddocregion diff --git a/aio/content/examples/component-styles/src/app/hero-app.component.ts b/aio/content/examples/component-styles/src/app/hero-app.component.ts index fdb08b988f..dd72cc5dfc 100644 --- a/aio/content/examples/component-styles/src/app/hero-app.component.ts +++ b/aio/content/examples/component-styles/src/app/hero-app.component.ts @@ -6,7 +6,8 @@ import { Hero } from './hero'; selector: 'app-root', template: `

Tour of Heroes

- `, + + `, styles: ['h1 { font-weight: normal; }'] }) export class HeroAppComponent { diff --git a/aio/content/guide/component-styles.md b/aio/content/guide/component-styles.md index 3c8eebb6b1..9b383b776c 100644 --- a/aio/content/guide/component-styles.md +++ b/aio/content/guide/component-styles.md @@ -9,23 +9,9 @@ with components, enabling a more modular design than regular stylesheets. This page describes how to load and apply these component styles. - - You can run the in Plunker and download the code from there. - ## Using component styles For every Angular component you write, you may define not only an HTML template, @@ -36,34 +22,43 @@ One way to do this is to set the `styles` property in the component metadata. The `styles` property takes an array of strings that contain CSS code. Usually you give it one string, as in the following example: - - +## Style scope + +
+ +The styles specified in `@Component` metadata _apply only within the template of that component_. + +
+ +They are _not inherited_ by any components nested within the template nor by any content projected into the component. + +In this example, the `h1` style applies only to the `HeroAppComponent`, +not to the nested `HeroMainComponent` nor to `

` tags anywhere else in the application. + +This scoping restriction is a ***styling modularity feature***. + +* You can use the CSS class names and selectors that make the most sense in the context of each component. -The selectors you put into a component's styles apply only within the template -of that component. The `h1` selector in the preceding example applies only to the `

` tag -in the template of `HeroAppComponent`. Any `

` elements elsewhere in -the application are unaffected. - -This is a big improvement in modularity compared to how CSS traditionally works. - -* You can use the CSS class names and selectors that make the most sense in the context of each component. * Class names and selectors are local to the component and don't collide with classes and selectors used elsewhere in the application. + + * Changes to styles elsewhere in the application don't affect the component's styles. + + * You can co-locate the CSS code of each component with the TypeScript and HTML code of the component, which leads to a neat and tidy project structure. + + * You can change or remove component CSS code without searching through the whole application to find where else the code is used. - {@a special-selectors} - - ## Special selectors Component styles have a few special *selectors* from the world of shadow DOM style scoping @@ -78,11 +73,8 @@ targeting elements *inside* the component's template). - - - The `:host` selector is the only way to target the host element. You can't reach the host element from inside the component with other selectors because it's not part of the component's own template. The host element is in a parent component's template. @@ -92,13 +84,9 @@ including another selector inside parentheses after `:host`. The next example targets the host element again, but only when it also has the `active` CSS class. - - - - ### :host-context Sometimes it's useful to apply styles based on some condition *outside* of a component's view. @@ -112,13 +100,9 @@ up to the document root. The `:host-context()` selector is useful when combined The following example applies a `background-color` style to all `

` elements *inside* the component, only if some ancestor element has the CSS class `theme-light`. - - - - ### (deprecated) `/deep/`, `>>>`, and `::ng-deep` Component styles normally apply only to the HTML in the component's own template. @@ -165,90 +149,80 @@ There are several ways to add styles to a component: The scoping rules outlined earlier apply to each of these loading patterns. -### Styles in metadata +### Styles in component metadata You can add a `styles` array property to the `@Component` decorator. -Each string in the array (usually just one string) defines the CSS. +Each string in the array defines some CSS for this component. - - + +
- -### Style URLs in metadata - -You can load styles from external CSS files by adding a `styleUrls` attribute -into a component's `@Component` decorator: - - - - - - - - -
- - - -The URL is relative to the *application root*, which is usually the -location of the `index.html` web page that hosts the application. -The style file URL is *not* relative to the component file. -That's why the example URL begins `src/app/`. -To specify a URL relative to the component file, see [Appendix 2](guide/component-styles#relative-urls). - +Reminder: these styles apply _only to this component_. +They are _not inherited_ by any components nested within the template nor by any content projected into the component.
+The CLI defines an empty `styles` array when you create the component with the `--inline-styles` flag. + +ng generate component hero-app --inline-style + + +### Style files in component metadata + +You can load styles from external CSS files by adding a `styleUrls` property +to a component's `@Component` decorator: + + + + + + +
+ +Reminder: the styles in the style file apply _only to this component_. +They are _not inherited_ by any components nested within the template nor by any content projected into the component. + +
- - -If you use module bundlers like Webpack, you can also use the `styles` attribute -to load styles from external files at build time. You could write: - -`styles: [require('my.component.css')]` - -Set the `styles` property, not the `styleUrls` property. The module -bundler loads the CSS strings, not Angular. -Angular sees the CSS strings only after the bundler loads them. -To Angular, it's as if you wrote the `styles` array by hand. -For information on loading CSS in this manner, refer to the module bundler's documentation. - + You can specify more than one styles file or even a combination of `style` and `styleUrls`.
+The CLI creates an empty styles file for you by default and references that file in the component's generated `styleUrls`. + +ng generate component hero-app + ### Template inline styles -You can embed styles directly into the HTML template by putting them +You can embed CSS styles directly into the HTML template by putting them inside `