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

3

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay