Styling Vue components

Beginner · 13 min

What an SFC's style block can do: scoped CSS and its deep, slotted, and global selectors, CSS modules, v-bind() in CSS, and where Tailwind fits.

Before thisSingle-file componentsClass and style bindings

Program With Erik · 13 min

Styling Vue components

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Program With ErikReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A plain style block in a .vue file is global CSS; adding the scoped attribute makes the compiler stamp each element with a data-v attribute and rewrite every selector to match it, so the rules only reach that component's own template.
  • Scoped styles stop at child component boundaries, except for the child's root element; :deep() reaches into children, :slotted() targets content passed in through slots, and :global() opts a single rule back out of scoping.
  • A style block with the module attribute compiles to CSS modules: class names are hashed and exposed to the template as $style, which gives the same isolation as scoped CSS through renamed classes instead of attribute selectors.
  • v-bind() inside a style block connects CSS to component state by compiling to a CSS custom property that Vue sets on the component's root element and keeps updated.
  • Tailwind and other utility-first setups work in Vue templates exactly as in plain HTML; many Vue projects use utilities for layout and a scoped block for the few rules that need real selectors.

The problem: CSS has one global namespace

Every rule in every stylesheet on a page competes for every element. Write .title { font-size: 2rem } in one component and it applies to every .title in the app, including the one a colleague wrote last month for a completely different purpose. Teams have fought this with naming conventions like BEM (covered in CSS architecture and naming conventions), but conventions only work as long as everyone follows them.

A Vue single-file component already puts template, logic, and styles in one file. The <style> block’s job is to make those styles belong to the component as well, and Vue’s compiler gives you several ways to do that.

Plain style blocks are global

Start with the default, because it surprises people: a <style> block with no attributes is plain global CSS. Vue extracts it into the page’s stylesheet and every rule applies everywhere. That is useful for resets, fonts, and design tokens defined once in App.vue, and a trap everywhere else.

Scoped CSS: attribute selectors added for you

Adding scoped changes how the compiler treats the block:

<template>
  <article class="card">
    <h2 class="title">{{ heading }}</h2>
  </article>
</template>

<style scoped>
.card { padding: 1rem; border-radius: 8px; }
.title { font-size: 1.25rem; }
</style>

The compiler gives the component a short hash, then adds an attribute with it to every element in the template and to every selector in the block:

<article class="card" data-v-7ba5bd90>
  <h2 class="title" data-v-7ba5bd90>…</h2>
</article>
.card[data-v-7ba5bd90] { padding: 1rem; border-radius: 8px; }
.title[data-v-7ba5bd90] { font-size: 1.25rem; }

Think of it as a stamp on everything the component owns. A .title in another component carries a different stamp, so it doesn’t match. There is no runtime cost: the rewrite happens at build time and the result is ordinary CSS.

One deliberate exception: a child component’s root element gets both the child’s stamp and the parent’s. That lets a parent position or size a child (margins, grid placement) from its own scoped styles, without reaching inside it.

Escaping the scope on purpose

Scoping is a boundary, and sometimes you need to cross it. Vue gives you three pseudo-selectors, each compiled into a specific rewrite:

/* Reach into a child component's internals */
.date-field :deep(.picker-input) { border-color: var(--brand); }
/* compiles to: .date-field[data-v-7ba5bd90] .picker-input */

/* Style content a parent passed into this component's <slot> */
:slotted(p) { margin: 0; }

/* One rule that is global, from inside a scoped block */
:global(body.modal-open) { overflow: hidden; }

:slotted() exists because slot content belongs to the parent that wrote it, so it carries the parent’s stamp, not yours. Without :slotted() your scoped rules can’t touch it.

:deep() is the one to be careful with. It’s the standard way to restyle a third-party component, but it couples you to that component’s internal class names, which are not part of its public API and can change in any release.

CSS modules: renamed classes instead of stamped elements

<style module> takes a different route to the same isolation. Instead of adding attributes, the compiler renames each class to a unique hash and gives you an object mapping the names you wrote to the names it generated:

<template>
  <button :class="[$style.button, { [$style.danger]: danger }]">
    <slot />
  </button>
</template>

<script setup lang="ts">
defineProps<{ danger?: boolean }>()
</script>

<style module>
.button { padding: 0.5rem 1rem; }
.danger { background: #dc2626; color: white; }
</style>

$style.button might render as _button_1k3x9_1. You can name the object (<style module="classes">) or read it in script with useCssModule(). Modules are handy when you need to pass a class name around as a value, such as giving a child a class through a prop, because the class is just a string. This is the same CSS Modules idea React projects use with .module.css files, built into the SFC.

v-bind() in CSS: state-driven styles

Sometimes a style genuinely depends on runtime state: a progress bar’s width, a user-chosen accent colour. The class and style bindings lesson covered :style on elements. Vue also lets the stylesheet itself read component state with v-bind():

<script setup lang="ts">
import { ref } from 'vue'
const accent = ref('#2563eb')
const theme = { radius: '12px' }
</script>

<template>
  <div class="panel"><input v-model="accent" type="color" /></div>
</template>

<style scoped>
.panel {
  border: 2px solid v-bind(accent);
  border-radius: v-bind('theme.radius');
}
</style>

Under the hood the compiler replaces each v-bind() with a CSS custom property, roughly var(--7ba5bd90-accent), and Vue sets that property as an inline style on the component’s root element. When accent changes, only the custom property is updated. The stylesheet stays static, and you keep the full power of selectors, pseudo-classes, and media queries.

Tailwind, preprocessors, and choosing

Two other common setups need almost no Vue-specific knowledge:

  • Preprocessors. <style lang="scss" scoped> works once sass is installed, because Vite handles the preprocessing before Vue’s scoping step. The trade-offs of Sass and PostCSS themselves are covered in CSS preprocessors and PostCSS.
  • Tailwind. Utility classes go straight into the template like any other class. If you use @apply inside an SFC style block with Tailwind v4, the block needs an @reference to your main stylesheet so it knows your theme, since each block is processed on its own.

A reasonable default for a new Vue app: utilities or a small global token file for the design system, scoped for component-specific rules, :deep() only at the edge where you restyle a library, and module when you need classes as values. None of these exclude each other, and most real codebases mix at least two.

What the video covers

Erik walks through every option in the SFC style block in one sitting: scoped CSS, CSS modules, deep selectors, global selectors, and dynamic CSS with v-bind(). The title argues against scoped CSS; the content is a fair comparison that lets you pick. It doesn’t explain the compiled output (the data-v attributes and generated custom properties) or cover Tailwind, which is what the sections above add.

Where this fits

Styling builds on single-file components and class and style bindings. Next: forms and validation in Vue.

Check yourself

  1. 01You add `<style scoped>.title { color: red }</style>` to a parent component, and a child component also renders an element with class `title`. Which elements turn red, and why?

    Only the parent's own `.title` elements, plus the child's root element if that root has the class. The compiler rewrites the selector to something like `.title[data-v-7ba5bd90]` and only stamps that attribute on elements in the parent's template and on child components' root elements, so the child's inner elements don't match.

  2. 02When would you reach for `:deep()`, and what is the cost?

    When a parent must restyle markup inside a child it doesn't control, typically a third-party component. `.wrapper :deep(.inner)` compiles to `.wrapper[data-v-xxx] .inner`, so the inner part is no longer tied to the scope. The cost is coupling: the parent now depends on the child's internal class names, which can change without warning.

  3. 03How does `color: v-bind(accent)` in a style block stay reactive when `accent` changes?

    The compiler replaces it with a CSS custom property such as `var(--7ba5bd90-accent)` and Vue sets that property as an inline style on the component's root element. When `accent` changes, Vue updates the custom property and the browser recalculates the styles; the stylesheet itself never changes.

  4. 04What is the practical difference between `<style scoped>` and `<style module>` for isolating a component's CSS?

    Scoped CSS keeps your class names and adds an attribute selector, so you write `class="card"` in the template. CSS modules rename the classes to unique hashes and you reference them through `$style`, as in `:class="$style.card"`. Both isolate the styles; modules also give you an explicit object of class names, which can be passed to child components as props or read in script with `useCssModule()`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark