网页 综合 更新时间 2026-11-13 16:59:50

SFC CSS Features | Vue.js

自动蜘蛛 vuejs.org 正文约 3159 字
SFC CSS Features | Vue.js
Reactivity: Utilities
Reactivity: Advanced
Dependency Injection
Options: Composition
Syntax Specification
Server-Side Rendering
TypeScript Utility Types
When a <style> tag has the scoped attribute, its CSS will apply to elements of the current component only. This is similar to the style encapsulation found in Shadow DOM. It comes with some caveats, but doesn't require any polyfills. It is achieved by using PostCSS to transform the following:
With scoped, the parent component's styles will not leak into child components. However, a child component's root node will be affected by both the parent's scoped CSS and the child's scoped CSS. This is by design so that the parent can style the child root element for layout purposes.
If you want a selector in scoped styles to be "deep", i.e. affecting child components, you can use the :deep() pseudo-class:
The above will be compiled into:
DOM content created with v-html are not affected by scoped styles, but you can still style them using deep selectors.
By default, scoped styles do not affect contents rendered by <slot/>, as they are considered to be owned by the parent component passing them in. To explicitly target slot content, use the :slotted pseudo-class:
If you want just one rule to apply globally, you can use the :global pseudo-class rather than creating another <style> (see below):
You can also include both scoped and non-scoped styles in the same component:
Scoped styles do not eliminate the need for classes. Due to the way browsers render various CSS selectors, p { color: red } will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in .example { color: red }, then you virtually eliminate that performance hit.
Be careful with descendant selectors in recursive components! For a CSS rule with the selector .a .b, if the element that matches .a contains a recursive child component, then all .b in that child component will be matched by the rule.
A <style module> tag is compiled as CSS Modules and exposes the resulting CSS classes to the component as an object under the key of $style:
The resulting classes are hashed to avoid collision, achieving the same effect of scoping the CSS to the current component only.
Refer to the CSS Modules spec for more details such as global exceptions and composition.
You can customize the property key of the injected classes object by giving the module attribute a value:
The injected classes can be accessed in setup() and <script setup> via the useCssModule API. For <style module> blocks with custom injection names, useCssModule accepts the matching module attribute value as the first argument:
SFC <style> tags support linking CSS values to dynamic component state using the v-bind CSS function:
The syntax works with <script setup>, and supports JavaScript expressions (must be wrapped in quotes):
The actual value will be compiled into a hashed CSS custom property, so the CSS is still static. The custom property will be applied to the component's root element via inline styles and reactively updated if the source value changes.
Edit this page on GitHub
访问原文 返回搜索 https://vuejs.org/api/sfc-css-features
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。