Introduction
Scoped styles keep CSS rules inside a component only. This stops styles from changing other parts of your page by mistake.
Scoped styles keep CSS rules inside a component only. This stops styles from changing other parts of your page by mistake.
<style lang="css" scoped> /* CSS rules here */ </style>
scoped attribute on the <style> tag to limit styles to the component.<style scoped>
p {
color: blue;
}
</style>lang="scss".<style lang="scss" scoped> .card { border: 1px solid black; } </style>
This component shows a green button with white text. The styles only affect this button inside the component.
--- // No frontmatter needed for this example --- <style scoped> button { background-color: green; color: white; padding: 0.5rem 1rem; border: none; border-radius: 0.25rem; cursor: pointer; } </style> <button>Click me</button>
Scoped styles in Astro work by adding unique attributes behind the scenes to keep styles local.
Remember that global styles still apply unless overridden by scoped styles.
Scoped styles keep CSS inside a component only.
Use scoped attribute on the <style> tag.
This helps avoid style conflicts and keeps your project tidy.
scoped styles in Astro components?scoped attribute inside the <style> tag.<style scoped>. Others misuse tags or attributes.<h1> text be when rendered?<style scoped>
h1 { color: blue; }
</style>
<h1>Hello World</h1>
<style>
h1 { color: red; }
</style>h1 and sets color blue.h1 color red, but scoped styles override global styles inside the component.<style> scoped
p { font-weight: bold; }
</style><style> tag: <style scoped>, not inside the CSS.<style scoped> for component-specific button styles.<style> without scoped attribute.Pass the test and this pattern moves unknown