Styles make your app look nice. You can add styles directly inside components or in separate files. Both ways help you control how things appear.
Inline vs external styles in Angular
/* Inline styles in Angular component */ import { Component } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>Hello!</p>`, styles: [`p { color: blue; }`] }) export class ExampleComponent {} /* External styles in Angular component */ import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent {}
Inline styles go inside the @Component decorator using the styles array.
External styles are placed in separate CSS files and linked with styleUrls.
import { Component } from '@angular/core'; @Component({ selector: 'app-inline', template: `<h1>Hi</h1>`, styles: [`h1 { font-size: 2rem; color: green; }`] }) export class InlineComponent {}
import { Component } from '@angular/core'; @Component({ selector: 'app-external', templateUrl: './external.component.html', styleUrls: ['./external.component.css'] }) export class ExternalComponent {}
/* external.component.css */
p { color: red; font-weight: bold; }This component shows both inline styles for the <h2> and external styles for the <p> tag. Inline styles are quick for small changes. External styles keep CSS organized.
import { Component } from '@angular/core'; @Component({ selector: 'app-style-demo', template: ` <h2>Inline Styled Title</h2> <p>This paragraph uses external styles.</p> `, styles: [`h2 { color: purple; font-family: Arial, sans-serif; }`], styleUrls: ['./style-demo.component.css'] }) export class StyleDemoComponent {} /* style-demo.component.css */ p { color: teal; font-size: 1.2rem; font-style: italic; }
Inline styles are good for small, component-specific tweaks.
External styles help keep your code clean and styles reusable.
Angular supports both, so choose what fits your project needs.
Inline styles go inside the component decorator using styles.
External styles live in separate CSS files linked with styleUrls.
Use inline for quick changes, external for organized and reusable styles.
Practice
Solution
Step 1: Understand Angular component decorator properties
Thestylesproperty is used to define inline CSS styles directly inside the component decorator.Step 2: Differentiate between
stylesandstyleUrlsstyleUrlsis used for external CSS files, not inline styles.Final Answer:
Inside thestylesarray in the component decorator -> Option CQuick Check:
Inline styles =stylesproperty [OK]
styles, external in styleUrls [OK]- Confusing
styleswithstyleUrls - Placing inline styles in global styles.css
- Trying to put styles inside
template
Solution
Step 1: Recall the correct property name for external styles
The correct property isstyleUrls(plural, camelCase) to link external CSS files.Step 2: Check syntax correctness
The value must be an array of strings with file paths, e.g., ['app.component.css'].Final Answer:
@Component({ styleUrls: ['app.component.css'] }) -> Option BQuick Check:
External styles usestyleUrlsarray [OK]
styleUrls with array of CSS files for external styles [OK]- Using singular
styleUrlinstead ofstyleUrls - Putting file names inside
stylesinstead ofstyleUrls - Using wrong property names like
styleSheets
styles and styleUrls in an Angular component decorator?Solution
Step 1: Understand Angular style merging behavior
Angular merges styles from bothstyles(inline) andstyleUrls(external) when both are present.Step 2: Confirm no errors occur
Angular does not throw errors for using both; it applies all styles combined.Final Answer:
Both inline and external styles will be applied and combined -> Option DQuick Check:
Inline + external styles combine in Angular [OK]
- Thinking one style type overrides the other
- Expecting a compilation error when both are used
- Assuming only external styles apply if both exist
@Component({
selector: 'app-test',
styles: 'h1 { color: red; }',
styleUrls: ['test.component.css']
})Solution
Step 1: Check the type of
stylespropertystylesmust be an array of strings, but here it is a single string.Step 2: Verify other properties
styleUrlsis correctly an array; selector is valid;templateis optional.Final Answer:
stylesshould be an array of strings, not a single string -> Option AQuick Check:
styles= array of strings [OK]
styles, even if one style [OK]- Using string instead of array for
styles - Confusing
styleUrlstype - Thinking
templateis mandatory
Solution
Step 1: Consider scope and speed of style changes
Inline styles instylesapply only to that component and are quick to edit without creating files.Step 2: Evaluate other options
Global styles affect all components; creating new CSS files is slower; inline HTML styles are not recommended for maintainability.Final Answer:
Use inline styles in thestylesarray for quick, component-specific changes -> Option AQuick Check:
Quick, local style changes = inlinestyles[OK]
- Adding quick changes to global styles affecting all components
- Creating new files for every small style change
- Using inline HTML styles instead of Angular styles
Ready to prove it?
Pass the test and this pattern moves unknown
