5 lines
10 KiB
JSON
5 lines
10 KiB
JSON
|
{
|
||
|
"id": "guide/style-precedence",
|
||
|
"title": "Style Precedence",
|
||
|
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/style-precedence.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=\"style-precedence\">Style Precedence<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/style-precedence#style-precedence\"><i class=\"material-icons\">link</i></a></h1>\n<p>When there are multiple bindings to the same class name or style attribute, Angular uses a set of precedence rules to determine which classes or styles to apply to the element.\nThese rules specify an order for which style and class related bindings have priority.\nThis styling precedence is as follows, from the most specific with the highest priority to least specific with the lowest priority:</p>\n<ol>\n<li>\n<p>Template bindings are the most specific because they apply to the element directly and exclusively, so they have the highest precedence.</p>\n<table width=\"100%\">\n <colgroup><col width=\"40%\">\n <col width=\"60%\">\n </colgroup><thead>\n <tr>\n <th>Binding type</th>\n <th>Example</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>Property binding</td>\n <td><code><div [class.foo]=\"hasFoo\"></code><br><code><div [style.color]=\"color\"></code></td>\n </tr>\n <tr>\n <td>Map binding</td>\n <td><code><div [class]=\"classExpression\"></code><br><code><div [<a href=\"api/animations/style\" class=\"code-anchor\">style</a>]=\"styleExpression\"></code></td>\n </tr>\n <tr>\n <td>Static value</td>\n <td><code><div class=\"foo\"></code><br><code><div <a href=\"api/animations/style\" class=\"code-anchor\">style</a>=\"color: blue\"></code></td>\n </tr>\n </tbody>\n</table>\n</li>\n<li>\n<p>Directive host bindings are less specific because you can use directives in multiple locations, so they have a lower precedence than template bindings.</p>\n<table width=\"100%\">\n <colgroup><col width=\"40%\">\n <col width=\"60%\">\n </colgroup><thead>\n <tr>\n <th>Binding type</th>\n <th>Example</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>Property binding</td>\n <td><code>host: {'[class.foo]': 'hasFoo'}</code><br><code>host: {'[style.color]': 'color'}</code></td>\n </tr>\n <tr>\n <td>Map binding</td>\n <td><code>host: {'[class]': 'classExpr'}</code><br><code>host: {'[<a href=\"api/animations/style\" class=\"code-anchor\">style</a>]': 'styleExpr'}</code></td>\n </tr>\n <tr>\n <td>Static value</td>\n <td><code>host: {'class': 'foo'}</code><br><code>host: {'<a href=\"api/animations/style\" class=\"code-anchor\">style</a>': 'color: blue'}</code></td>\n </tr>\n </tbody>\n</table>\n</li>\n<li>\n<p>Component host bindings have the lowest precedence.</p>\n <table width=\"100%\">\n <colgroup><col width=\"40%\">\n <col width=\"60%\">\n </colgroup><thead>\n <tr>\n <th>Binding type</th>\n <th>Example</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>Property binding</td>\n <td><code>host: {'[class.foo]': 'hasFoo'}</code><br><code>host: {'[style.color]': 'color'}</code></td>\n </tr>\n <tr>\n <td>Map binding</td>\n <td><code>host: {'[class]': 'classExpression'}</code><br><code>host: {'[<a href=\"api/animations/style\" class=\"code-anchor\">style</a>]': 'styleExpression'}</code></td>\n </tr>\n <tr>\n <td>Static value</td>\n <td><code>host: {'class': 'foo'}</code><br><code>host: {'<a href=\"api/animations/style\" class=\"code-anchor\">style</a>': 'color: blue'}</code></td>\n </tr>\n </tbody>\n</table>\n</li>\n</ol>\n<h2 id=\"precedence-and-specificity\">Precedence and specificity<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/style-precedence#precedence-and-specificity\"><i class=\"material-icon
|
||
|
}
|