SFC CSS Features

Scoped CSS

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:

  1. <style scoped>
  2. .example {
  3. color: red;
  4. }
  5. </style>
  6. <template>
  7. <div class="example">hi</div>
  8. </template>

Into the following:

  1. <style>
  2. .example[data-v-f3f3eg9] {
  3. color: red;
  4. }
  5. </style>
  6. <template>
  7. <div class="example" data-v-f3f3eg9>hi</div>
  8. </template>

Child Component Root Elements

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.

Deep Selectors

If you want a selector in scoped styles to be “deep”, i.e. affecting child components, you can use the :deep() pseudo-class:

  1. <style scoped>
  2. .a :deep(.b) {
  3. /* ... */
  4. }
  5. </style>

The above will be compiled into:

  1. .a[data-v-f3f3eg9] .b {
  2. /* ... */
  3. }

:::tip DOM content created with v-html are not affected by scoped styles, but you can still style them using deep selectors. :::

Slotted 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:

  1. <style scoped>
  2. :slotted(div) {
  3. color: red;
  4. }
  5. </style>

Global Selectors

If you want just one rule to apply globally, you can use the :global pseudo-class rather than creating another <style> (see below):

  1. <style scoped>
  2. :global(.red) {
  3. color: red;
  4. }
  5. </style>

Mixing Local and Global Styles

You can also include both scoped and non-scoped styles in the same component:

  1. <style>
  2. /* global styles */
  3. </style>
  4. <style scoped>
  5. /* local styles */
  6. </style>

Scoped Style Tips

  • 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.

CSS Modules

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:

  1. <template>
  2. <p :class="$style.red">This should be red</p>
  3. </template>
  4. <style module>
  5. .red {
  6. color: red;
  7. }
  8. </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.

Custom Inject Name

You can customize the property key of the injected classes object by giving the module attribute a value:

  1. <template>
  2. <p :class="classes.red">red</p>
  3. </template>
  4. <style module="classes">
  5. .red {
  6. color: red;
  7. }
  8. </style>

Usage with Composition API

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:

  1. import { useCssModule } from 'vue'
  2. // inside setup() scope...
  3. // default, returns classes for <style module>
  4. useCssModule()
  5. // named, returns classes for <style module="classes">
  6. useCssModule('classes')

v-bind() in CSS

SFC <style> tags support linking CSS values to dynamic component state using the v-bind CSS function:

  1. <template>
  2. <div class="text">hello</div>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. color: 'red'
  9. }
  10. }
  11. }
  12. </script>
  13. <style>
  14. .text {
  15. color: v-bind(color);
  16. }
  17. </style>

The syntax works with <script setup>, and supports JavaScript expressions (must be wrapped in quotes):

  1. <script setup>
  2. const theme = {
  3. color: 'red'
  4. }
  5. </script>
  6. <template>
  7. <p>hello</p>
  8. </template>
  9. <style scoped>
  10. p {
  11. color: v-bind('theme.color');
  12. }
  13. </style>

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.