ngStyle lets you change CSS styles on HTML elements while your app runs. It helps make your page look different based on data or user actions.
ngStyle for dynamic styles in Angular
<element [ngStyle]="{ 'styleName': expression, 'anotherStyle': expression }"></element>Use square brackets [] to bind ngStyle to an object of style names and values.
Style names are strings and values can be variables or expressions.
<div [ngStyle]="{ 'color': 'red', 'font-weight': 'bold' }">Text</div>fontSize variable in pixels.<p [ngStyle]="{ 'font-size.px': fontSize }">Resizable text</p>isActive.<button [ngStyle]="{ 'background-color': isActive ? 'green' : 'gray' }">Click me</button>This component shows a heading with color and size controlled by variables. Clicking the button toggles the color between blue and orange and the font size between 20px and 30px.
import { Component } from '@angular/core'; @Component({ selector: 'app-dynamic-style', template: ` <h2 [ngStyle]="{ color: titleColor, 'font-size.px': titleSize }">Dynamic Style Example</h2> <button (click)="changeStyle()">Change Style</button> ` }) export class DynamicStyleComponent { titleColor = 'blue'; titleSize = 20; changeStyle() { this.titleColor = this.titleColor === 'blue' ? 'orange' : 'blue'; this.titleSize = this.titleSize === 20 ? 30 : 20; } }
ngStyle works well for multiple style changes at once.
For single style changes, you can also use [style.styleName] binding.
Remember to use quotes around style names with dashes, like 'font-size.px'.
ngStyle binds an object of CSS styles to an element dynamically.
It helps change styles based on variables or expressions in your component.
Use it to make your app look interactive and responsive to user actions.
Practice
ngStyle do?Solution
Step 1: Understand the purpose of
ngStylengStyleis used to bind an object containing CSS styles to an element dynamically.Step 2: Compare with other directives
UnlikengClasswhich adds/removes classes,ngStyledirectly sets inline styles.Final Answer:
It dynamically applies CSS styles to an element based on an object. -> Option AQuick Check:
ngStyle = dynamic CSS styles [OK]
- Confusing ngStyle with ngClass
- Thinking ngStyle listens to events
- Believing ngStyle binds HTML attributes
ngStyle in a template?Solution
Step 1: Recall the correct binding syntax
ngStyleexpects an object inside square brackets with key-value pairs for styles.Step 2: Check each option
correctly uses an object with keys and values inside curly braces and quotes for keys with hyphens.Final Answer:
<div [ngStyle]="{color: 'red', 'font-weight': 'bold'}"></div> -> Option BQuick Check:
ngStyle = object in square brackets [OK]
- Using string instead of object for ngStyle
- Missing square brackets for binding
- Not quoting CSS property names with hyphens
color = 'blue';
isBold = true;
What will be the rendered style of this element?
<div [ngStyle]="{'color': color, 'font-weight': isBold ? 'bold' : 'normal'}">Text</div>Solution
Step 1: Evaluate the color binding
The variablecoloris 'blue', so the style 'color' will be 'blue'.Step 2: Evaluate the font-weight binding
isBoldis true, so the ternary expression returns 'bold'.Final Answer:
Text with blue color and bold font weight -> Option DQuick Check:
color=blue and font-weight=bold [OK]
- Ignoring ternary condition result
- Assuming default styles instead of bound values
- Mixing up true/false in ternary
<div [ngStyle]="{color: color, fontWeight: 'bold'}">Hello</div>Assuming
color is a string variable in the component.Solution
Step 1: Check CSS property naming in ngStyle
CSS properties with hyphens must be quoted as strings in the object keys, e.g., 'font-weight'.Step 2: Analyze the given code
The keyfontWeightis camelCase and unquoted, which Angular does not convert automatically in ngStyle.Final Answer:
The CSS property 'fontWeight' should be quoted as 'font-weight'. -> Option AQuick Check:
CSS keys with hyphens need quotes in ngStyle [OK]
- Using camelCase keys without quotes for CSS properties
- Assuming ngStyle accepts unquoted hyphenated keys
- Thinking ngStyle only accepts single style
isActive. Which ngStyle binding correctly applies a green background when active and gray when inactive?Solution
Step 1: Understand CSS property naming in ngStyle
CSS properties with hyphens must be quoted as strings, e.g., 'background-color'.Step 2: Check the ternary expression syntax
Values must be strings, so 'green' and 'gray' need quotes.Step 3: Evaluate each option
Click me correctly quotes the CSS property and string values. Click me uses camelCase without quotes, which is invalid. Click me quotes the key incorrectly as camelCase. Click me misses quotes around color strings.Final Answer:
<button [ngStyle]="{'background-color': isActive ? 'green' : 'gray'}">Click me</button> -> Option CQuick Check:
Quote CSS keys and string values in ngStyle [OK]
- Using camelCase keys without quotes
- Forgetting quotes around color strings
- Mixing camelCase and hyphenated keys incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
