Two ways in, and the difference between them is scoping.
A <style> block inside a .razor component is scoped to that component, mirroring how Astro
scopes .astro styles.
<div class="box"><p>Only this paragraph is blue.</p></div>
<style>
.box { border: 1px solid #512bd4; }
p { color: blue; }
@media (min-width: 600px) { .box { padding: 1rem; } }
</style><div class="box"><p>Only this paragraph is blue.</p></div>
<style>
.box { border: 1px solid #512bd4; }
p { color: blue; }
@@media (min-width: 600px) { .box { padding: 1rem; } }
</style>
<style is:global>
.site-wide { font-style: italic; }
</style>CSS at-rules need a doubled @@, because @ is Razor's transition character. It renders as
@media, and the CSS the build sees is the un-doubled one.
is:global is emitted unscoped and is:inline is left exactly where you wrote it, both matching
Astro.
[AstroStylesheet] is the .razor equivalent of an .astro file's own import:
---
import './card.css';
import '../styles/theme.scss';
---@attribute [AstroStylesheet("./card.css")]
@attribute [AstroStylesheet("../styles/theme.scss")]A .razor file has no imports, so the path is declared and the build generates a real one into the
compiled module. Because the import is real, Vite does the work: Sass and the other preprocessors,
CSS Modules, Tailwind's @apply, PostCSS, minification.
An imported stylesheet is not scoped, exactly as it is not in an .astro file. There is nothing
to rewrite until the preprocessor has run, which is the same reason Astro leaves one global. Use a
<style> block for styles that belong to the component.
Both go through Vite as real stylesheets, so both are hoisted into the page head and deduplicated by the module graph rather than by AstroSharp splicing text into a fragment.
For a <style> block that means the build lifts it out of the component and writes it to a generated
stylesheet under .astrosharp/styles/, already scoped. Astro does not scope framework-component
styles, so the scoping is AstroSharp's: every element the component rendered gets a per-component
attribute, and each selector is rewritten to require it — p { color: blue } becomes
p[data-ashp-<hash>] { color: blue } and cannot reach paragraphs elsewhere.
The two halves are computed independently and have to agree: the selector is rewritten at build time, the attribute is added at render time, and both derive from the component's type name. Nothing tells the other what it chose.
@media / @supports / @container keep their prelude and have their inner rules scoped;
@keyframes and @font-face are left intact.
A <style> block that interpolates is not processed. Razor compiles
<style>
.dynamic { color: @Colour; }
</style>into an element it builds piece by piece rather than into a literal, so its text does not exist until the component renders — and the CSS handed to Vite would be the source rather than what runs. It falls back to the old behaviour: emitted inline, scoped at render, no preprocessing. The build says so rather than leaving you to notice.
Pass the value through a CSS custom property instead. The block stays a literal, so it keeps
everything above, and the value still varies per render — the same trade Astro's define:vars
makes:
<div class="card">…</div>
<style define:vars={{ tint: colour, scale }}>
.card { color: var(--tint); opacity: var(--scale); }
</style><div class="card" style="@Astro.Vars(new { tint = Colour, scale = Scale })">…</div>
<style>
.card { color: var(--tint); opacity: var(--scale); }
</style>Astro.Vars writes each property as --name:value. Names are used as written, so what is in the C#
is what var() takes; a null value leaves its property undeclared, so a var(--tint, blue) fallback
applies; and numbers are formatted invariantly, which is what keeps them valid CSS whatever
locale the machine running the build has. Pass a dictionary for a name C# cannot spell as an
identifier.
is:inline opts out of everything, by definition — the block stays where it was written and is
not processed.
Sass in a <style> block is not supported. A scoped block is scoped by rewriting selectors, and
there are no selectors to rewrite until Sass has run. Use [AstroStylesheet] with a .scss file,
which is unscoped, and keep the scoped block plain CSS.
define:vars is .astro-only — the directive is compiler syntax. Astro.Vars above is the
equivalent, and it writes the same custom properties onto an element of your choosing rather than
onto one the compiler picks.