The importance of scoped CSS in Vue.js

#Vue#CSS

Hey there! I’m sorry for being out a while but I’ve been really busy organising the Vue Day 2019 conference, and I must say things are on the road and it’s getting very exciting!

Btw, do you know that our friends at Vue School are going to double the subscription price? But if you SIGN UP NOW you’ll save 52% and get it for 12$/month lifetime! They’re creating amazing content and… psst, and preparing a Vue.js 3 course too 😉.

That said, let’s dive into the tip!


Sometimes I see new devs coming to Vue.js getting confused about scoped CSS in Vue.js. Some using it without really knowing how does it work.

If you are there, I hope this tip helps you understand why and when to use them (and when not) 😉.

I’m not going to dive deep into the theory as you have it already in the vue-loader docs. Just to know, it’s a vue-loader’s feature to avoid style collisions and encapsulate styles by emulating Shadow DOM functionality.

Let’s better see an example of a problematic situation when you don’t use scoped CSS.

Imagine we have a BaseList.vue component with the following structure:

<template>
  <ul class="list">
    <li class="list-item" v-for="item in items" :key="item">
      {{ item }}
    </li>
  </ul>
</template>

Then create two components with the same code. Call them RedList.vue and BlueList.vue:

<template>
  <BaseList :items="items" />
</template>

<script>
import BaseList from "./BaseList";

export default {
  props: ["items"],
  components: {
    BaseList
  }
};
</script>

Now add two different styles to each of them, according to the colors. For instance, for BlueList.vue:

<style>
.list-item {
  color: white;
  background: #42a5f5;
}
</style>

Put them together like I did in this CodeSandbox, and… surprise! You’ll see that even though both components have a different color defined, they show the same color:

That’s because when we don’t use scoped CSS, they’re global, even though they are in different components. So, in this case one of the styles is overriding the other.

That’s why scoped CSS are that important: they avoid these collisions to happen out of the box.

Here’s the joke: if you try to put the scoped word into the style tag in the example above, you’ll see that the styles of the list item won’t apply 😅.

But that’s normal, next week you’ll see how to style inner elements when using scoped CSS, so stay tuned!

To end up, I want to mention that you can mix scoped (thus local) styles with global styles (not scoped). You want to use global styles in these situations:

  • When applying cross-component styles (utility styles).
  • When writing a third party library. If you apply scoped CSS, you’d be making your library impossible to customise styles.

Keep in mind scoped CSS is not the only solution. You might be comfortable as well using CSS modules or methodologies such as BEM.

Remember you can read this tip online (with copy/pasteable code), and don’t forget to share VueDose with your colleagues, so they also know about these tips as well!

See you next week.

Alex

Do you want to sponsor a VueDose tip? Send an email to info@vuedose.tips and have more info!

Start saving time and get a tip about the Vue ecosystem every week, right in your inbox.