angular-cn/aio/dist/generated/docs/guide/component-styles.json

5 lines
21 KiB
JSON

{
"id": "guide/component-styles",
"title": "Component styles",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/component-styles.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"component-styles\">Component styles<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#component-styles\"><i class=\"material-icons\">link</i></a></h1>\n<p>Angular applications are styled with standard CSS. That means you can apply\neverything you know about CSS stylesheets, selectors, rules, and media queries\ndirectly to Angular applications.</p>\n<p>Additionally, Angular can bundle <em>component styles</em>\nwith components, enabling a more modular design than regular stylesheets.</p>\n<p>This page describes how to load and apply these component styles.</p>\n<p>You can run the <live-example></live-example> in Stackblitz and download the code from there.</p>\n<h2 id=\"using-component-styles\">Using component styles<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#using-component-styles\"><i class=\"material-icons\">link</i></a></h2>\n<p>For every Angular component you write, you may define not only an HTML template,\nbut also the CSS styles that go with that template,\nspecifying any selectors, rules, and media queries that you need.</p>\n<p>One way to do this is to set the <code>styles</code> property in the component metadata.\nThe <code>styles</code> property takes an array of strings that contain CSS code.\nUsually you give it one string, as in the following example:</p>\n<code-example path=\"component-styles/src/app/hero-app.component.ts\" header=\"src/app/hero-app.component.ts\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-root',\n template: `\n &#x3C;h1>Tour of Heroes&#x3C;/h1>\n &#x3C;app-hero-main [hero]=\"hero\">&#x3C;/app-hero-main>\n `,\n styles: ['h1 { font-weight: normal; }']\n})\nexport class HeroAppComponent {\n/* . . . */\n}\n\n</code-example>\n<h2 id=\"style-scope\">Style scope<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#style-scope\"><i class=\"material-icons\">link</i></a></h2>\n<div class=\"alert is-critical\">\n<p>The styles specified in <code>@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a></code> metadata <em>apply only within the template of that component</em>.</p>\n</div>\n<p>They are <em>not inherited</em> by any components nested within the template nor by any content projected into the component.</p>\n<p>In this example, the <code>h1</code> style applies only to the <code>HeroAppComponent</code>,\nnot to the nested <code>HeroMainComponent</code> nor to <code>&#x3C;h1></code> tags anywhere else in the application.</p>\n<p>This scoping restriction is a <strong><em>styling modularity feature</em></strong>.</p>\n<ul>\n<li>You can use the CSS class names and selectors that make the most sense in the context of each component.</li>\n</ul>\n<ul>\n<li>Class names and selectors are local to the component and don't collide with\nclasses and selectors used elsewhere in the application.</li>\n</ul>\n<ul>\n<li>Changes to styles elsewhere in the application don't affect the component's styles.</li>\n</ul>\n<ul>\n<li>You can co-locate the CSS code of each component with the TypeScript and HTML code of the component,\nwhich leads to a neat and tidy project structure.</li>\n</ul>\n<ul>\n<li>You can change or remove component CSS code without searching through the\nwhole application to find where else the code is used.</li>\n</ul>\n<a id=\"special-selectors\"></a>\n<h2 id=\"special-selectors\">Special selectors<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#special-selectors\"><i class=\"material-icons\">link</i></a></h2>\n<p>Component styles have a few special <em>selectors</em> from the world of shadow DOM style scoping\n(described in the <a href=\"https://www.w3.org/TR/css-scoping-1\">CSS Scoping Module Level 1</a> page on the\n<a href=\"https://www.w3.org\">W3C</a> site).\nThe following sections describe these selectors.</p>\n<h3 id=\"host\">:host<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#host\"><i class=\"material-icons\">link</i></a></h3>\n<p>Use the <code>:host</code> pseudo-class selector to target styles in the element that <em>hosts</em> the component (as opposed to\ntargeting elements <em>inside</em> the component's template).</p>\n<code-example path=\"component-styles/src/app/hero-details.component.css\" region=\"host\" header=\"src/app/hero-details.component.css\">\n:host {\n display: block;\n border: 1px solid black;\n}\n\n</code-example>\n<p>The <code>:host</code> selector is the only way to target the host element. You can't reach\nthe host element from inside the component with other selectors because it's not part of the\ncomponent's own template. The host element is in a parent component's template.</p>\n<p>Use the <em>function form</em> to apply host styles conditionally by\nincluding another selector inside parentheses after <code>:host</code>.</p>\n<p>The next example targets the host element again, but only when it also has the <code>active</code> CSS class.</p>\n<code-example path=\"component-styles/src/app/hero-details.component.css\" region=\"hostfunction\" header=\"src/app/hero-details.component.css\">\n:host(.active) {\n border-width: 3px;\n}\n\n</code-example>\n<p>The <code>:host</code> selector can also be combined with other selectors.\nAdd selectors behind the <code>:host</code> to select child elements, for example using <code>:host h2</code> to target all <code>&#x3C;h2></code> elements inside a component's view.</p>\n<div class=\"alert is-helpful\">\n<p>You should not add selectors (other than <code>:host-context</code>) in front of the <code>:host</code> selector to style a component based on the outer context of the component's view. Such selectors are not scoped to a component's view and will select the outer context, but it's not native behavior. Use <code>:host-context</code> selector for that purpose instead.</p>\n</div>\n<h3 id=\"host-context\">:host-context<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#host-context\"><i class=\"material-icons\">link</i></a></h3>\n<p>Sometimes it's useful to apply styles based on some condition <em>outside</em> of a component's view.\nFor example, a CSS theme class could be applied to the document <code>&#x3C;body></code> element, and\nyou want to change how your component looks based on that.</p>\n<p>Use the <code>:host-context()</code> pseudo-class selector, which works just like the function\nform of <code>:host()</code>. The <code>:host-context()</code> selector looks for a CSS class in any ancestor of the component host element,\nup to the document root. The <code>:host-context()</code> selector is useful when combined with another selector.</p>\n<p>The following example applies a <code>background-color</code> style to all <code>&#x3C;h2></code> elements <em>inside</em> the component, only\nif some ancestor element has the CSS class <code>theme-light</code>.</p>\n<code-example path=\"component-styles/src/app/hero-details.component.css\" region=\"hostcontext\" header=\"src/app/hero-details.component.css\">\n:host-context(.theme-light) h2 {\n background-color: #eef;\n}\n\n</code-example>\n<h3 id=\"deprecated-deep--and-ng-deep\">(deprecated) <code>/deep/</code>, <code>>>></code>, and <code>::ng-deep</code><a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#deprecated-deep--and-ng-deep\"><i class=\"material-icons\">link</i></a></h3>\n<p>Component styles normally apply only to the HTML in the component's own template.</p>\n<p>Applying the <code>::ng-deep</code> pseudo-class to any CSS rule completely disables view-encapsulation for\nthat rule. Any style with <code>::ng-deep</code> applied becomes a global style. In order to scope the specified style\nto the current component and all its descendants, be sure to include the <code>:host</code> selector before\n<code>::ng-deep</code>. If the <code>::ng-deep</code> combinator is used without the <code>:host</code> pseudo-class selector, the style\ncan bleed into other components.</p>\n<p>The following example targets all <code>&#x3C;h3></code> elements, from the host element down\nthrough this component to all of its child elements in the DOM.</p>\n<code-example path=\"component-styles/src/app/hero-details.component.css\" region=\"deep\" header=\"src/app/hero-details.component.css\">\n:host ::ng-deep h3 {\n font-style: italic;\n}\n\n</code-example>\n<p>The <code>/deep/</code> combinator also has the aliases <code>>>></code>, and <code>::ng-deep</code>.</p>\n<div class=\"alert is-important\">\n<p>Use <code>/deep/</code>, <code>>>></code> and <code>::ng-deep</code> only with <em>emulated</em> view encapsulation.\nEmulated is the default and most commonly used view encapsulation. For more information, see the\n<a href=\"guide/view-encapsulation\">View Encapsulation</a> section.</p>\n</div>\n<div class=\"alert is-important\">\n<p>The shadow-piercing descendant combinator is deprecated and <a href=\"https://www.chromestatus.com/feature/6750456638341120\">support is being removed from major browsers</a> and tools.\nAs such we plan to drop support in Angular (for all 3 of <code>/deep/</code>, <code>>>></code> and <code>::ng-deep</code>).\nUntil then <code>::ng-deep</code> should be preferred for a broader compatibility with the tools.</p>\n</div>\n<a id=\"loading-styles\"></a>\n<h2 id=\"loading-component-styles\">Loading component styles<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#loading-component-styles\"><i class=\"material-icons\">link</i></a></h2>\n<p>There are several ways to add styles to a component:</p>\n<ul>\n<li>By setting <code>styles</code> or <code>styleUrls</code> metadata.</li>\n<li>Inline in the template HTML.</li>\n<li>With CSS imports.</li>\n</ul>\n<p>The scoping rules outlined earlier apply to each of these loading patterns.</p>\n<h3 id=\"styles-in-component-metadata\">Styles in component metadata<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#styles-in-component-metadata\"><i class=\"material-icons\">link</i></a></h3>\n<p>You can add a <code>styles</code> array property to the <code>@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a></code> decorator.</p>\n<p>Each string in the array defines some CSS for this component.</p>\n<code-example path=\"component-styles/src/app/hero-app.component.ts\" header=\"src/app/hero-app.component.ts (CSS inline)\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-root',\n template: `\n &#x3C;h1>Tour of Heroes&#x3C;/h1>\n &#x3C;app-hero-main [hero]=\"hero\">&#x3C;/app-hero-main>\n `,\n styles: ['h1 { font-weight: normal; }']\n})\nexport class HeroAppComponent {\n/* . . . */\n}\n\n</code-example>\n<div class=\"alert is-critical\">\n<p>Reminder: these styles apply <em>only to this component</em>.\nThey are <em>not inherited</em> by any components nested within the template nor by any content projected into the component.</p>\n</div>\n<p>The Angular CLI command <a href=\"cli/generate\"><code>ng generate component</code></a> defines an empty <code>styles</code> array when you create the component with the <code>--inline-style</code> flag.</p>\n<code-example language=\"sh\" class=\"code-shell\">\nng generate component hero-app --inline-style\n</code-example>\n<h3 id=\"style-files-in-component-metadata\">Style files in component metadata<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#style-files-in-component-metadata\"><i class=\"material-icons\">link</i></a></h3>\n<p>You can load styles from external CSS files by adding a <code>styleUrls</code> property\nto a component's <code>@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a></code> decorator:</p>\n<code-tabs>\n <code-pane header=\"src/app/hero-app.component.ts (CSS in file)\" path=\"component-styles/src/app/hero-app.component.1.ts\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-root',\n template: `\n &#x3C;h1>Tour of Heroes&#x3C;/h1>\n &#x3C;app-hero-main [hero]=\"hero\">&#x3C;/app-hero-main>\n `,\n styleUrls: ['./hero-app.component.css']\n})\nexport class HeroAppComponent {\n/* . . . */\n}\n\n</code-pane>\n <code-pane header=\"src/app/hero-app.component.css\" path=\"component-styles/src/app/hero-app.component.css\">\nh1 {\n font-weight: normal;\n}\n\n\n</code-pane>\n</code-tabs>\n<div class=\"alert is-critical\">\n<p>Reminder: the styles in the style file apply <em>only to this component</em>.\nThey are <em>not inherited</em> by any components nested within the template nor by any content projected into the component.</p>\n</div>\n<div class=\"alert is-helpful\">\n<p> You can specify more than one styles file or even a combination of <code>styles</code> and <code>styleUrls</code>.</p>\n</div>\n<p>When you use the Angular CLI command <a href=\"cli/generate\"><code>ng generate component</code></a> without the <code>--inline-style</code> flag, it creates an empty styles file for you and references that file in the component's generated <code>styleUrls</code>.</p>\n<code-example language=\"sh\" class=\"code-shell\">\nng generate component hero-app\n</code-example>\n<h3 id=\"template-inline-styles\">Template inline styles<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#template-inline-styles\"><i class=\"material-icons\">link</i></a></h3>\n<p>You can embed CSS styles directly into the HTML template by putting them\ninside <code>&#x3C;<a href=\"api/animations/style\" class=\"code-anchor\">style</a>></code> tags.</p>\n<code-example path=\"component-styles/src/app/hero-controls.component.ts\" region=\"inlinestyles\" header=\"src/app/hero-controls.component.ts\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-hero-controls',\n template: `\n &#x3C;<a href=\"api/animations/style\" class=\"code-anchor\">style</a>>\n button {\n background-color: white;\n border: 1px solid #777;\n }\n &#x3C;/<a href=\"api/animations/style\" class=\"code-anchor\">style</a>>\n &#x3C;h3>Controls&#x3C;/h3>\n &#x3C;button (click)=\"activate()\">Activate&#x3C;/button>\n `\n})\n\n</code-example>\n<h3 id=\"template-link-tags\">Template link tags<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#template-link-tags\"><i class=\"material-icons\">link</i></a></h3>\n<p>You can also write <code>&#x3C;link></code> tags into the component's HTML template.</p>\n<code-example path=\"component-styles/src/app/hero-team.component.ts\" region=\"stylelink\" header=\"src/app/hero-team.component.ts\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-hero-team',\n template: `\n &#x3C;!-- We must use a relative URL so that the AOT compiler can find the stylesheet -->\n &#x3C;link rel=\"stylesheet\" href=\"../assets/hero-team.component.css\">\n &#x3C;h3>Team&#x3C;/h3>\n &#x3C;ul>\n &#x3C;li *<a href=\"api/common/NgForOf\" class=\"code-anchor\">ngFor</a>=\"let member of hero.team\">\n {{member}}\n &#x3C;/li>\n &#x3C;/ul>`\n})\n\n</code-example>\n<div class=\"alert is-critical\">\n<p>When building with the CLI, be sure to include the linked style file among the assets to be copied to the server as described in the <a href=\"https://github.com/angular/angular-cli/wiki/stories-asset-configuration\">CLI wiki</a>.</p>\n<!-- 2018-10-16: The link above is still the best source for this information. -->\n<p>Once included, the CLI will include the stylesheet, whether the link tag's href URL is relative to the application root or the component file.</p>\n</div>\n<h3 id=\"css-imports\">CSS @imports<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#css-imports\"><i class=\"material-icons\">link</i></a></h3>\n<p>You can also import CSS files into the CSS files using the standard CSS <code>@import</code> rule.\nFor details, see <a href=\"https://developer.mozilla.org/en/docs/Web/CSS/@import\"><code>@import</code></a>\non the <a href=\"https://developer.mozilla.org\">MDN</a> site.</p>\n<p>In this case, the URL is relative to the CSS file into which you're importing.</p>\n<code-example path=\"component-styles/src/app/hero-details.component.css\" region=\"import\" header=\"src/app/hero-details.component.css (excerpt)\">\n/* The AOT compiler needs the `./` to show that this is local */\n@import './hero-details-box.css';\n\n</code-example>\n<h3 id=\"external-and-global-style-files\">External and global style files<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#external-and-global-style-files\"><i class=\"material-icons\">link</i></a></h3>\n<p>When building with the CLI, you must configure the <code>angular.json</code> to include <em>all external assets</em>, including external style files.</p>\n<p>Register <strong>global</strong> style files in the <code>styles</code> section which, by default, is pre-configured with the global <code>styles.css</code> file.</p>\n<p>See the <a href=\"https://github.com/angular/angular-cli/wiki/stories-global-styles\">CLI wiki</a> to learn more.</p>\n<!-- 2018-10-16: The link above is still the best source for this information. -->\n<h3 id=\"non-css-style-files\">Non-CSS style files<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/component-styles#non-css-style-files\"><i class=\"material-icons\">link</i></a></h3>\n<p>If you're building with the CLI,\nyou can write style files in <a href=\"https://sass-lang.com/\">sass</a>, <a href=\"http://lesscss.org/\">less</a>, or <a href=\"https://stylus-lang.com/\">stylus</a> and specify those files in the <code>@<a href=\"api/core/Component#styleUrls\" class=\"code-anchor\">Component.styleUrls</a></code> metadata with the appropriate extensions (<code>.scss</code>, <code>.less</code>, <code>.styl</code>) as in the following example:</p>\n<code-example>\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-root',\n templateUrl: './app.component.html',\n styleUrls: ['./app.component.scss']\n})\n...\n</code-example>\n<p>The CLI build process runs the pertinent CSS preprocessor.</p>\n<p>When generating a component file with <code>ng generate component</code>, the CLI emits an empty CSS styles file (<code>.css</code>) by default.\nYou can configure the CLI to default to your preferred CSS preprocessor\nas explained in the <a href=\"https://github.com/angular/angular-cli/wiki/stories-css-preprocessors\" title=\"CSS Preprocessor integration\">CLI wiki</a>.</p>\n<!-- 2018-10-16: The link above is still the best source for this information. -->\n<div class=\"alert is-important\">\n<p>Style strings added to the <code>@<a href=\"api/core/Component#styles\" class=\"code-anchor\">Component.styles</a></code> array <em>must be written in CSS</em> because the CLI cannot apply a preprocessor to inline styles.</p>\n</div>\n\n \n</div>\n\n<!-- links to this doc:\n - guide/component-overview\n - guide/deprecations\n - guide/example-apps-list\n-->\n<!-- links from this doc:\n - api/animations/style\n - api/common/NgForOf\n - api/core/Component\n - api/core/Component#styleUrls\n - api/core/Component#styles\n - cli/generate\n - guide/component-styles#component-styles\n - guide/component-styles#css-imports\n - guide/component-styles#deprecated-deep--and-ng-deep\n - guide/component-styles#external-and-global-style-files\n - guide/component-styles#host\n - guide/component-styles#host-context\n - guide/component-styles#loading-component-styles\n - guide/component-styles#non-css-style-files\n - guide/component-styles#special-selectors\n - guide/component-styles#style-files-in-component-metadata\n - guide/component-styles#style-scope\n - guide/component-styles#styles-in-component-metadata\n - guide/component-styles#template-inline-styles\n - guide/component-styles#template-link-tags\n - guide/component-styles#using-component-styles\n - guide/view-encapsulation\n - http://lesscss.org/\n - https://developer.mozilla.org\n - https://developer.mozilla.org/en/docs/Web/CSS/@import\n - https://github.com/angular/angular-cli/wiki/stories-asset-configuration\n - https://github.com/angular/angular-cli/wiki/stories-css-preprocessors\n - https://github.com/angular/angular-cli/wiki/stories-global-styles\n - https://github.com/angular/angular/edit/master/aio/content/guide/component-styles.md?message=docs%3A%20describe%20your%20change...\n - https://sass-lang.com/\n - https://stylus-lang.com/\n - https://www.chromestatus.com/feature/6750456638341120\n - https://www.w3.org\n - https://www.w3.org/TR/css-scoping-1\n-->"
}