Bird
Raised Fist0
Angularframework~20 mins

ngStyle for dynamic styles in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ngStyle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What style will be applied by ngStyle?
Given the Angular component template below, what will be the background color of the <div> when isActive is true?
Angular
<div [ngStyle]="{'background-color': isActive ? 'green' : 'red'}">Status Box</div>
AThe background color will be green.
BThe background color will be red.
CThe background color will be blue.
DNo background color will be applied.
Attempts:
2 left
💡 Hint
Check the ternary condition inside ngStyle for the background-color property.
📝 Syntax
intermediate
2:00remaining
Identify the correct ngStyle syntax
Which of the following is the correct syntax to apply multiple dynamic styles using ngStyle in Angular?
A<div ngStyle="color: textColor; font-size: fontSize + 'px'"></div>
B<div [ngStyle]="{'color': textColor, 'font-size': fontSize + 'px'}"></div>
C<div [ngStyle]="color: textColor; font-size: fontSize + 'px'"></div>
D<div [ngStyle]="{color: 'textColor', fontSize: fontSize + 'px'}"></div>
Attempts:
2 left
💡 Hint
Remember that ngStyle expects an object with style properties as keys and values as expressions.
🔧 Debug
advanced
2:00remaining
Why does ngStyle not update dynamically?
Consider this Angular component snippet:
styles = { 'color': 'blue' };

updateColor() {
  this.styles.color = 'red';
}

And the template:
<div [ngStyle]="styles">Text</div>

Why might the color not change to red after calling updateColor()?
ABecause the <code>updateColor</code> method is missing a return statement.
BBecause ngStyle only accepts string literals, not object variables.
CBecause Angular does not detect changes when object properties are mutated directly.
DBecause the styles object must be reassigned to a new object for change detection.
Attempts:
2 left
💡 Hint
Think about how Angular detects changes in objects passed to bindings.
❓ state_output
advanced
2:00remaining
What styles are applied after state change?
Given this Angular component code:
isHighlighted = false;

getStyle() {
  return {
    'font-weight': this.isHighlighted ? 'bold' : 'normal',
    'color': this.isHighlighted ? 'orange' : 'black'
  };
}

And template:
<button (click)="isHighlighted = !isHighlighted">Toggle</button>
<span [ngStyle]="getStyle()">Text</span>

What will be the style of the <span> after clicking the button once?
Afont-weight: normal; color: black;
Bfont-weight: normal; color: orange;
Cfont-weight: bold; color: black;
Dfont-weight: bold; color: orange;
Attempts:
2 left
💡 Hint
The button toggles the boolean, changing styles accordingly.
🧠 Conceptual
expert
2:00remaining
Why prefer ngStyle over inline style bindings?
In Angular, why is it generally better to use ngStyle for dynamic styling instead of directly binding to the style attribute like [style.color] or inline styles?
ABecause ngStyle allows setting multiple styles at once with a single binding, improving readability and performance.
BBecause [style.color] bindings are deprecated and no longer supported in Angular 17+.
CBecause ngStyle prevents any style changes unless explicitly triggered by user events.
DBecause ngStyle automatically adds vendor prefixes to CSS properties.
Attempts:
2 left
💡 Hint
Think about managing multiple styles and code clarity.

Practice

(1/5)
1. What does the Angular directive ngStyle do?
easy
A. It dynamically applies CSS styles to an element based on an object.
B. It adds classes to an element based on a condition.
C. It binds HTML attributes to an element.
D. It listens to user events like clicks.

Solution

  1. Step 1: Understand the purpose of ngStyle

    ngStyle is used to bind an object containing CSS styles to an element dynamically.
  2. Step 2: Compare with other directives

    Unlike ngClass which adds/removes classes, ngStyle directly sets inline styles.
  3. Final Answer:

    It dynamically applies CSS styles to an element based on an object. -> Option A
  4. Quick Check:

    ngStyle = dynamic CSS styles [OK]
Hint: Remember: ngStyle changes styles, ngClass changes classes [OK]
Common Mistakes:
  • Confusing ngStyle with ngClass
  • Thinking ngStyle listens to events
  • Believing ngStyle binds HTML attributes
2. Which of the following is the correct syntax to apply ngStyle in a template?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall the correct binding syntax

    ngStyle expects an object inside square brackets with key-value pairs for styles.
  2. Step 2: Check each option

    correctly uses an object with keys and values inside curly braces and quotes for keys with hyphens.
  3. Final Answer:

    <div [ngStyle]="{color: 'red', 'font-weight': 'bold'}"></div> -> Option B
  4. Quick Check:

    ngStyle = object in square brackets [OK]
Hint: Use square brackets and an object literal for ngStyle [OK]
Common Mistakes:
  • Using string instead of object for ngStyle
  • Missing square brackets for binding
  • Not quoting CSS property names with hyphens
3. Given this component code:
color = 'blue';
isBold = true;

What will be the rendered style of this element?
<div [ngStyle]="{'color': color, 'font-weight': isBold ? 'bold' : 'normal'}">Text</div>
medium
A. Text with default color and normal font weight
B. Text with blue color and normal font weight
C. Text with default color and bold font weight
D. Text with blue color and bold font weight

Solution

  1. Step 1: Evaluate the color binding

    The variable color is 'blue', so the style 'color' will be 'blue'.
  2. Step 2: Evaluate the font-weight binding

    isBold is true, so the ternary expression returns 'bold'.
  3. Final Answer:

    Text with blue color and bold font weight -> Option D
  4. Quick Check:

    color=blue and font-weight=bold [OK]
Hint: Check variable values and ternary results carefully [OK]
Common Mistakes:
  • Ignoring ternary condition result
  • Assuming default styles instead of bound values
  • Mixing up true/false in ternary
4. Identify the error in this Angular template snippet:
<div [ngStyle]="{color: color, fontWeight: 'bold'}">Hello</div>

Assuming color is a string variable in the component.
medium
A. The CSS property 'fontWeight' should be quoted as 'font-weight'.
B. The variable 'color' is not defined in the component.
C. ngStyle cannot bind to multiple styles at once.
D. The syntax is correct; no error.

Solution

  1. 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'.
  2. Step 2: Analyze the given code

    The key fontWeight is camelCase and unquoted, which Angular does not convert automatically in ngStyle.
  3. Final Answer:

    The CSS property 'fontWeight' should be quoted as 'font-weight'. -> Option A
  4. Quick Check:

    CSS keys with hyphens need quotes in ngStyle [OK]
Hint: Quote CSS keys with hyphens in ngStyle object [OK]
Common Mistakes:
  • Using camelCase keys without quotes for CSS properties
  • Assuming ngStyle accepts unquoted hyphenated keys
  • Thinking ngStyle only accepts single style
5. You want to change the background color of a button dynamically based on a component variable isActive. Which ngStyle binding correctly applies a green background when active and gray when inactive?
hard
A. Click me
B. Click me
C. Click me
D. Click me

Solution

  1. Step 1: Understand CSS property naming in ngStyle

    CSS properties with hyphens must be quoted as strings, e.g., 'background-color'.
  2. Step 2: Check the ternary expression syntax

    Values must be strings, so 'green' and 'gray' need quotes.
  3. 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.
  4. Final Answer:

    <button [ngStyle]="{'background-color': isActive ? 'green' : 'gray'}">Click me</button> -> Option C
  5. Quick Check:

    Quote CSS keys and string values in ngStyle [OK]
Hint: Quote CSS keys with hyphens and string values in ngStyle [OK]
Common Mistakes:
  • Using camelCase keys without quotes
  • Forgetting quotes around color strings
  • Mixing camelCase and hyphenated keys incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard