SFC CSS Features
Vue SFCs provide advanced CSS features for component-scoped styling and dynamic CSS integration.Scoped CSS
Add thescoped attribute to automatically scope styles to the current component:
How It Works
The compiler transforms scoped styles using PostCSS:data-v-* attribute, and CSS selectors are rewritten to include this attribute.
Deep Selectors
Use:deep() to affect child components:
Slotted Selectors
Use:slotted() to target slot content:
-s suffix indicates slotted content scope.
Global Selectors
Use:global() for global styles within scoped stylesheets:
Scoped Animations
Keyframes in scoped styles are automatically scoped:CSS Modules
Use themodule attribute to enable CSS Modules:
Custom Inject Name
Name the CSS modules object:Usage in <script setup>
Access CSS modules via useCssModule():
CSS Modules Configuration
Configure CSS Modules via build tools:CSS v-bind()
Bind CSS values to component state:
How It Works
The compiler:- Extracts
v-bind()expressions from CSS - Generates hashed CSS variable names
- Injects a
useCssVars()call in the component - Sets CSS variables on the component’s root element
Quoteless Values
You can omit quotes for simple identifiers:SSR Considerations
In SSR, CSS variables are:- Inlined into the style attribute
- Prefixed with
:--to distinguish from user CSS vars - Reset to
initialto prevent inheritance issues
Combining Features
You can use multiple CSS features together:Preprocessor Support
All CSS features work with preprocessors:API Reference
compileStyle()
Compiles a single style block:SFCStyleCompileOptions
SFCStyleCompileResults
compileStyleAsync()
Async version with CSS Modules support:SFCAsyncStyleCompileOptions
CSSModulesOptions
parseCssVars()
Extracts CSS variables from SFC styles:v-bind().
cssVarsPlugin()
PostCSS plugin for transformingv-bind() to CSS variables:
useCssVars()
Runtime helper for injecting CSS variables (automatically imported):useCssModule()
Retrieve CSS Modules object in<script setup>:
PostCSS Plugins
Internal PostCSS plugins used by the compiler:scopedPlugin
Transforms scoped styles by adding attribute selectors:trimPlugin
Removes whitespace from compiled CSS:cssVarsPlugin
Transformsv-bind() to CSS custom properties:
Caveats
Scoped Styles
- Parent scoped styles do not leak into child components
- Child component root elements are affected by both parent and child scoped CSS
- Use classes on root elements for styling, not tag selectors
- Scoped styles do not eliminate the need for class naming strategies
CSS Modules
- Requires async compilation via
compileStyleAsync() - Build tool integration needed for full support
- Class name hashing strategy configured via build tool
v-bind() in CSS
- Expressions are evaluated in component context
- Updates are reactive and batched
- Use production build for hashed variable names
- Variables are scoped to component instance
- May have performance implications for frequently changing values
Best Practices
- Use scoped styles for component-specific styling
- Use
:deep()sparingly for child component overrides - Combine with CSS Modules for guaranteed name uniqueness
- Use
v-bind()for truly dynamic values tied to component state - Prefer CSS variables for theming when possible
- Use preprocessors for complex styling logic
- Use
:global()only when necessary for global styles