Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update scoped styles documentation in guides #11311

Merged
merged 4 commits into from
Mar 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/content/docs/en/guides/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ Astro `<style>` CSS rules are automatically **scoped by default**. Scoped styles
This CSS:
```astro title="src/pages/index.astro"
<style>
h1 {
color: red;
h1 {
color: red;
}

.text {
color: blue;
.text {
color: blue;
}
</style>
```
Expand Down Expand Up @@ -214,7 +214,7 @@ import 'package-name/styles.css';
<html><!-- Your page here --></html>
```

If your package **does _not_ suggest using a file extension** (i.e. `package-name/styles`), you'll need to update your Astro config first!
If your package **does not suggest using a file extension** (i.e. `package-name/styles`), you'll need to update your Astro config first!

Say you are importing a CSS file from `package-name` called `normalize` (with the file extension omitted). To ensure we can prerender your page correctly, add `package-name` to [the `vite.ssr.noExternal` array](https://vite.dev/config/ssr-options.html#ssr-noexternal):

Expand Down Expand Up @@ -302,9 +302,11 @@ Astro CSS rules are evaluated in this order of appearance:
- **imported styles**
- **scoped styles** (highest precedence)

### Scoped Styles
### Scoped Styles

Depending on your chosen value for [`scopedStyleStrategy`](/en/reference/configuration-reference/#scopedstylestrategy), scoped styles may or may not increase the [CLASS column specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_cascade/Specificity#class_column).

Using [scoped styles](#scoped-styles) does not increase the _specificity_ of your CSS, but they will always come last in the _order of appearance_. They will therefore take precedence over other styles of the same specificity. For example, if you import a stylesheet that conflicts with a scoped style, the scoped style's value will apply:
However, [scoped styles](#scoped-styles) will always come last in the order of appearance. These styles will therefore take precedence over other styles of the same specificity. For example, if you import a stylesheet that conflicts with a scoped style, the scoped styles value will apply:

```css title="src/components/make-it-purple.css"
h1 {
Expand All @@ -325,10 +327,10 @@ import "./make-it-purple.css"
</div>
```

If you make the imported style _more specific_, it will have higher precedence over the scoped style:
Scoped styles will be overwritten if the imported style is more specific. The style with a higher specificity will take precedence over the scoped style:

```css title="src/components/make-it-purple.css"
div > h1 {
#intro {
color: purple;
}
```
Expand All @@ -340,7 +342,7 @@ import "./make-it-purple.css"
h1 { color: red }
</style>
<div>
<h1>
<h1 id="intro">
This header will be purple!
</h1>
</div>
Expand Down Expand Up @@ -517,7 +519,7 @@ export default defineConfig({
},
},
})

```

### In framework components
Expand Down