Skip to content
Tutorials Center
My learning

Topics

Lesson 54 of 87 ยท Reusability, Transitions and Styling

Styling Strategies

Choose how to style a Vue app. Learn design tokens with CSS variables, utility CSS and Tailwind, class-based dark mode, and how component libraries compare.

Vue does not prescribe how you write CSS. That freedom is useful, but a growing app needs an answer to questions such as: where do colors live, how does a button get its size, how do we support dark mode, and should we build our own components or adopt a library? A good answer is rarely "one tool". It is usually a small set of layers, each with one job.

This lesson walks through the layers most Vue teams end up with and shows the pieces you can run in the browser. Scoped CSS and CSS Modules covered how a single component keeps its styles private. Here we zoom out to the whole app.

Think in layers

flowchart TD
  A["1. Design tokens<br/>CSS variables: colors, spacing, radius"] --> B["2. Base styles<br/>reset, typography, body"]
  B --> C["3. Utilities (optional)<br/>small single-purpose classes"]
  C --> D["4. Component styles<br/>scoped or module CSS"]
  D --> E["5. Local overrides<br/>props, slots, custom properties"]

Each layer builds on the one above it. Lower layers never hard-code values that belong to a higher layer.

The rule that keeps this tidy: values come from tokens, structure comes from components. A component's CSS says "use the surface color and the medium radius", not "use #ffffff and 8px". When the brand color changes, you edit one token.

Design tokens with CSS custom properties

A design token is a named design decision. In CSS the natural home for tokens is custom properties (--name), declared on :root and read with var():

CSS
:root {
  --color-surface: #ffffff;
  --color-text: #1b1f24;
  --color-accent: #2a7de1;
  --radius: 8px;
  --space: 12px;
}

.panel {
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--radius);
  padding: var(--space);
}

Custom properties beat preprocessor variables (Sass, Less) for theming for two reasons. They exist at runtime, so you can change them with JavaScript or by switching a class. And they inherit, so you can override a token for just one part of the page by redefining it on that element. Both scoped and module styles read them without any special syntax.

Prefer semantic names (--color-surface, --color-danger) over literal ones (--white, --red-500). Literal palettes still have a place as a lower layer, and the semantic tokens point into them: --color-danger: var(--red-600).

Dark mode with a class

There are two common ways to switch themes:

  • prefers-color-scheme in a media query follows the operating system automatically but gives the user no manual control.
  • A class on <html> (usually dark) that redefines the tokens. It lets the user choose, and it can still start from the OS preference.

Because components only read semantic tokens, dark mode becomes a matter of redefining those tokens once:

CSS
:root { --color-surface: #ffffff; --color-text: #1b1f24; }
.dark { --color-surface: #161b22; --color-text: #e6edf3; }

In Vue, the only job left is toggling the class. A watch with immediate: true applies the initial value and every change. The example also shows a local override: the second panel has a warm class that redefines only the accent color and radius, and the change stays inside that panel because custom properties inherit downward.

Tokens, a dark class and a local override

Toggle the checkbox and both panels recolor, because every rule reads a token. Notice that no component CSS changed.

Real apps should remember the choice and avoid a flash of the wrong theme on load. The usual approach is a tiny inline script in index.html, run before the app loads, that reads the saved setting (falling back to matchMedia("(prefers-color-scheme: dark)")) and sets the dark class. It needs localStorage, which the in-lesson sandbox does not allow, so here it is read-only:

HTML
<script>
  try {
    const saved = localStorage.getItem("theme");
    const prefersDark = matchMedia("(prefers-color-scheme: dark)").matches;
    if (saved === "dark" || (!saved && prefersDark)) {
      document.documentElement.classList.add("dark");
    }
  } catch (error) {
    /* storage can be blocked: stay on the light theme */
  }
</script>

Utility CSS and Tailwind

A utility-first approach styles elements by combining many small classes, each doing one thing (p-4, rounded, bg-blue-600, text-white). Tailwind CSS is the best-known framework for it. You write the classes in your templates, and Tailwind generates a stylesheet that contains only the classes it finds in your source files.

With Vite (what npm create vue@latest uses), recent Tailwind versions are added as a Vite plugin and a single CSS import:

JavaScript
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'

export default defineConfig({ plugins: [vue(), tailwindcss()] })
CSS
/* src/style.css */
@import "tailwindcss";

/* class-based dark mode: the "dark:" variant applies under .dark */
@custom-variant dark (&:where(.dark, .dark *));
VUE
<template>
  <button class="rounded-lg bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 dark:bg-blue-400 dark:text-slate-900">
    Save
  </button>
</template>

Utility CSS has real strengths: no naming of one-off classes, styles that sit next to the markup, a constrained scale that keeps spacing and colors consistent, and a stylesheet that stops growing. It has costs too: long class lists, and the discipline to extract repeated groups.

In Vue the extraction tool is the component. If a button appears 40 times, build AppButton once, with props for variant and size, instead of copying a long class string. Keep the class strings in one place and choose between them with a computed list:

JavaScript
const variants = {
  primary: 'bg-blue-600 text-white',
  quiet: 'bg-transparent text-blue-700',
}

Tailwind finds classes by scanning your source files as plain text. A class name built at runtime, such as `bg-${color}-500`, never appears as a complete string, so it is not generated. Always write whole class names (a lookup object like the one above) or use inline styles for truly dynamic values.

Try it: the same idea with hand-written utilities

Tailwind itself needs a build step, so here is the pattern with a handful of our own utility classes. The AppButton component maps variant and size props to class lists, and the page prints the exact list so you can see what the component produces.

A button component over utility classes

Callers only say variant="danger". The long class list lives in exactly one place, and changing the look of every danger button means editing one line.

Component libraries

You do not have to build every button, dialog and date picker. Component libraries come in three broad styles:

Style What you get Examples Trade-off
Styled kits Finished, themeable components in one visual language Vuetify (Material Design), PrimeVue, Element Plus, Naive UI, Quasar Fast to start; the look is theirs, and deep customization fights the library
Headless libraries Behavior, keyboard support and ARIA with no styling Reka UI (formerly Radix Vue), Headless UI Full control of the design; you write all the CSS
Copy-in collections Component source added to your project and styled with Tailwind shadcn-vue You own the code, so you maintain it too

When comparing, check things that matter more than screenshots:

  1. Accessibility. Do dialogs trap focus, do menus support arrow keys, are labels wired up? (Accessibility in Vue shows what to expect.)
  2. Theming. Can the library read your tokens, or does it need its own theme system?
  3. Bundle size and tree shaking. Can you import only the components you use?
  4. Vue 3 and TypeScript support, plus how quickly the maintainers fix bugs.
  5. Your design. If a designer hands you a unique brand, a headless library or your own components may cost less than overriding a styled kit.

Wrap library components in your own thin components (AppDialog, AppSelect) from day one. The rest of the app then depends on your API, and swapping or restyling the library later touches only the wrappers.

A sensible default recipe

For a new project with no strong constraint:

  1. Declare tokens in one global stylesheet, including a .dark override.
  2. Add a small reset and base typography.
  3. Write components with scoped (or module) styles that read only tokens.
  4. Add Tailwind if your team likes utilities, or skip it; the token layer works either way.
  5. Reach for a headless or styled library for hard widgets (dialogs, comboboxes, date pickers) rather than building focus management yourself.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why are CSS custom properties a better theming tool than Sass variables?

    Sass variables disappear at build time. Custom properties stay in the browser, so switching a class or setting a variable restyles the page instantly, and overrides apply only to the subtree where they are set.
  2. 2. A component reads var(--color-surface). How does a .dark class on <html> change it?

    The component's CSS never changes. Redefining the token on a parent element is enough because custom properties inherit and var() is resolved by the browser.
  3. 3. Why does a class like bg-${color}-500 fail in a Tailwind project?

    Tailwind does not execute your code. It looks for whole class strings in the files, so write full names (for example in a lookup object) instead of assembling pieces.
  4. 4. What is the best way to avoid repeating a long utility class list on many buttons?

    In Vue, components are the unit of reuse. A button component keeps the class strings in one place and exposes a small, meaningful API.
  5. 5. What is a headless component library?

    Headless libraries handle focus, keyboard and ARIA details and render unstyled markup, so you apply your own design.

Summary

  • Style an app in layers: tokens, base styles, optional utilities, component styles and local overrides.
  • Put design decisions in CSS custom properties with semantic names, and let components read them with var().
  • Implement dark mode by redefining the tokens under a .dark class that you toggle from Vue; set the initial class before the app loads to avoid a flash.
  • Utility CSS such as Tailwind keeps styles beside markup. Use full class names, and extract components instead of repeating class lists.
  • Styled kits are fast, headless libraries give design control, and copy-in collections give you ownership; compare accessibility, theming and bundle size.
  • Wrap third-party components in your own so a later change touches only the wrappers.

Next: TypeScript with Vue