Consider an Astro component with scoped styles applied. What happens to the styles of nested child elements inside this component?
<style scoped>
div {
color: red;
}
</style>
<div>
<p>This text is inside a div.</p>
</div>Think about how CSS normally cascades inside a component even when styles are scoped.
Scoped styles in Astro apply only to the component's root and its children, but CSS rules still cascade normally inside the component. So nested elements like <p> inside a styled <div> inherit or get affected by the styles.
Choose the correct way to write scoped styles inside an Astro component.
Remember how HTML attributes are written in Astro components for scoped styles.
In Astro, to apply scoped styles, you add the attribute scoped directly to the <style> tag without any value. Options C and D use invalid syntax, and B applies global styles, not scoped.
You have two Astro components: Parent.astro and Child.astro. The parent has scoped styles targeting <button> elements, but the button inside the child component does not get styled. Why?
<!-- Parent.astro -->
<style scoped>
button {
background-color: blue;
}
</style>
<Child />
<!-- Child.astro -->
<button>Click me</button>Think about how scoped styles isolate styles per component in Astro.
Scoped styles in Astro apply only to the component where they are declared. They do not cross component boundaries. So styles in Parent.astro do not affect elements inside Child.astro.
Given this Astro component and global CSS, what color will the <h2> text appear?
<!-- Global CSS -->
<style>
h2 {
color: green;
}
</style>
<!-- Component.astro -->
<style scoped>
h2 {
color: red;
}
</style>
<h2>Heading</h2>Consider CSS specificity and how scoped styles are applied in Astro.
Scoped styles in Astro add unique attributes to elements, making their selectors more specific than global styles. Therefore, the scoped style for <h2> with color red overrides the global green color inside the component.
Choose the main reason Astro encourages scoped styles inside components rather than global styles.
Think about how large projects manage styles and avoid bugs.
Scoped styles isolate CSS to the component, preventing styles from leaking or conflicting across components. This makes code easier to maintain and reduces bugs caused by unintended style overrides.
