Javascript is required
·
0 min read

Vue Tip: Reactive Values in CSS

Vue Tip: Reactive Values in CSS Image

Since Vue 3 it is possible to use reactive values in the <style>:

1<template>
2  <span class="label">Hello World!</span>
3</template>
4
5<script>
6export default {
7  props: {
8    color: {
9      type: String,
10      default: 'blue',
11    },
12  },
13}
14</script>
15
16<style>
17.label {
18  color: v-bind(color);
19}
20</style>

In this example, we bind the color property of our Vue component to the CSS color property of our label.

Internally, Vue uses CSS variables that are scoped to each component.

More details are available in the official documentation.

If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:

I will never share any of your personal data. You can unsubscribe at any time.