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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

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

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay