{ "id": "guide/style-precedence", "title": "Style Precedence", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Style Precedencelink

\n

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:

\n
    \n
  1. \n

    Template bindings are the most specific because they apply to the element directly and exclusively, so they have the highest precedence.

    \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
    Binding typeExample
    Property binding<div [class.foo]=\"hasFoo\">
    <div [style.color]=\"color\">
    Map binding<div [class]=\"classExpression\">
    <div [style]=\"styleExpression\">
    Static value<div class=\"foo\">
    <div style=\"color: blue\">
    \n
  2. \n
  3. \n

    Directive host bindings are less specific because you can use directives in multiple locations, so they have a lower precedence than template bindings.

    \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
    Binding typeExample
    Property bindinghost: {'[class.foo]': 'hasFoo'}
    host: {'[style.color]': 'color'}
    Map bindinghost: {'[class]': 'classExpr'}
    host: {'[style]': 'styleExpr'}
    Static valuehost: {'class': 'foo'}
    host: {'style': 'color: blue'}
    \n
  4. \n
  5. \n

    Component host bindings have the lowest precedence.

    \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
    Binding typeExample
    Property bindinghost: {'[class.foo]': 'hasFoo'}
    host: {'[style.color]': 'color'}
    Map bindinghost: {'[class]': 'classExpression'}
    host: {'[style]': 'styleExpression'}
    Static valuehost: {'class': 'foo'}
    host: {'style': 'color: blue'}
    \n
  6. \n
\n

Precedence and specificitylink

\n

In the following example, binding to a specific class, as in [class.special], takes precedence over a generic [class] binding.\nSimilarly, binding to a specific style, as in [style.color], takes precedence over a generic [style] binding.

\n\n<h3>Basic specificity</h3>\n\n<!-- The `class.special` binding overrides any value for the `special` class in `classExpression`. -->\n<div [class.special]=\"isSpecial\" [class]=\"classExpression\">Some text.</div>\n\n<!-- The `style.color` binding overrides any value for the `color` property in `styleExpression`. -->\n<div [style.color]=\"color\" [style]=\"styleExpression\">Some text.</div>\n\n\n

Precedence and bindings from different sourceslink

\n

Specificity rules also apply to bindings even when they originate from different sources.\nAn element can have bindings that originate from its own template, from host bindings on matched directives, and from host bindings on matched components.

\n\n<h3>Source specificity</h3>\n\n<!-- The `class.special` template binding overrides any host binding to the `special` class set by `dirWithClassBinding` or `comp-with-host-binding`.-->\n<comp-with-host-binding [class.special]=\"isSpecial\" dirWithClassBinding>Some text.</comp-with-host-binding>\n\n<!-- The `style.color` template binding overrides any host binding to the `color` property set by `dirWithStyleBinding` or `comp-with-host-binding`. -->\n<comp-with-host-binding [style.color]=\"color\" dirWithStyleBinding>Some text.</comp-with-host-binding>\n\n\n

Precedence of bindings and static attributeslink

\n

Bindings take precedence over static attributes because they are dynamic.\nIn the following case, class and [class] have similar specificity, but the [class] binding takes precedence.

\n\n<h3>Dynamic vs static</h3>\n\n<!-- If `classExpression` has a value for the `special` class, this value overrides the `class=\"special\"` below -->\n<div class=\"special\" [class]=\"classExpression\">Some text.</div>\n\n<!-- If `styleExpression` has a value for the `color` property, this value overrides the `style=\"color: blue\"` below -->\n<div style=\"color: blue\" [style]=\"styleExpression\">Some text.</div>\n\n\n\n\n

Delegating to styles with lower precedencelink

\n

Higher precedence styles can defer to lower precedence styles using undefined values.\nFor example, consider the following template:

\n\n<comp-with-host-binding dirWithHostBinding></comp-with-host-binding>\n\n\n

Imagine that the dirWithHostBinding directive and the comp-with-host-binding component both have a [style.width] host binding.

\n\n@HostBinding('style.width')\nwidth = '200px';\n\n\n

If dirWithHostBinding sets its binding to undefined, the width property falls back to the value of the comp-with-host-binding host binding.

\n\n@HostBinding('style.width')\nwidth = ''; // undefined\n\n
\n

If dirWithHostBinding sets its binding to null, Angular removes the width property entirely.

\n \n @HostBinding('style.width')\n width = null;\n \n
\n\n \n
\n\n\n" }