Performance: Inline vs external styles
MEDIUM IMPACT
This affects page load speed and rendering performance by influencing CSS parsing, caching, and style recalculations.
import { Component } from '@angular/core'; @Component({ selector: 'app-good-style', template: `<div class="text-red">Hello</div>`, styleUrls: ['./good-style.component.css'] }) export class GoodStyleComponent {} /* good-style.component.css */ .text-red { color: red; font-size: 2rem; }
import { Component } from '@angular/core'; @Component({ selector: 'app-bad-style', template: `<div style="color: red; font-size: 2rem;">Hello</div>` }) export class BadStyleComponent {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline styles | No extra DOM nodes, but increases HTML size | Triggers style recalculation per element | Higher paint cost due to blocking | [X] Bad |
| External stylesheets | No extra DOM nodes, smaller HTML | Single style recalculation after CSS load | Lower paint cost, non-blocking | [OK] Good |