Performance: Why Astro handles styles efficiently
Astro's style handling impacts page load speed and rendering performance by minimizing CSS sent and reducing style recalculations.
export const styles = ` .button { color: blue; } `; // Styles scoped and only included if component is used
import './global.css'; // All styles loaded globally regardless of usage
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global CSS import | Low | Multiple reflows if styles change | High due to large CSS | [X] Bad |
| Scoped component CSS | Low | Single reflow | Low due to minimal CSS | [OK] Good |
<style> tags for scoped styles applied only to the component.<style> applies scoped styles correctly. <style global> is for global styles. Attributes like inline or scoped are invalid.<script>
export let active = false;
</script>
<style>
.highlight { color: green; }
</style>
<div class={active ? 'highlight' : ''}>Hello</div><style>
.btn { background: blue; }
</style><style global> block, often in a layout component used by all pages.<style> for scoped styles to avoid conflicts and keep styles modular.Pass the test and this pattern moves unknown