DEV Community

Cover image for Vue 3: Extending entire CSS/SCSS files to the children of a scoped parent component using :deep()
Anthony Jekanyika
Anthony Jekanyika

Posted on

Vue 3: Extending entire CSS/SCSS files to the children of a scoped parent component using :deep()

Lets say for some reason you have now come to the realisation that you know have to isolate the CSS in a specific component from rest of the your project. Then, like the great Vue developer that you are you confidently scroll to your component's opening style tag and add the scoped attribute then call it a day. You check the project to confirm that your fix worked but you forgot that your component has its own child components which no longer inherit the CSS from the parent component because its now scoped. How do we fix this?

Enter the :deep() pseudo-class

The deep selector allows us to share CSS classes between a scoped parent and its child components. Today I am going to show you how to do it with entire CSS/SCSS files.

First install SASS into your project with the following code in your terminal npm i -D sass sass-loader

Here is an example of how a parent component will extend its imported CSS files to its child components:

<template>
   <Child-1 />
   <Child-2 />
   <Child-2 />
</template>

<style lang="scss" scoped>
  :deep() {
    @import "./css/style-1";
    @import "./css/style-2";
  }
</style>
Enter fullscreen mode Exit fullscreen mode

Important Note. Remember in SASS when importing a CSS/SCSS file you don't add the .css, .scss or .sass extension.

Top comments (1)

Collapse
 
brianmhlanga profile image
Brian Mhlanga

Thank you for this, always have scenarios were components would lose styling afted adding scoped in the opening style tag