Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/sfc-scoped-css-child-component-styling.md
1---2title: Use Deep Selectors for Styling Child Component Elements3impact: HIGH4impactDescription: Scoped styles cannot target elements inside child components without deep selectors, leading to silently broken styles5type: gotcha6tags: [vue3, sfc, scoped-css, deep-selector, child-components]7---89# Use Deep Selectors for Styling Child Component Elements1011**Impact: HIGH** - When using scoped CSS in Vue SFCs, styles do not penetrate into child components. Without using deep selectors (`:deep()`), your styles will silently fail to apply to elements rendered by child components or third-party libraries.1213## Task Checklist1415- [ ] Use `:deep()` selector to style elements inside child components16- [ ] Never use deprecated `>>>` or `/deep/` selectors (Vue 3 only supports `:deep()`)17- [ ] Scope deep selectors to a parent class when possible to limit impact18- [ ] Consider using unscoped styles or CSS modules for heavily nested styling1920**Problematic Code:**21```vue22<template>23<div class="container">24<ThirdPartyDatePicker />25</div>26</template>2728<style scoped>29/* BAD: These styles won't apply to elements inside ThirdPartyDatePicker */30.container .date-input {31border-color: blue;32}3334.container .calendar-popup {35background: white;36}37</style>38```3940**Correct Code:**41```vue42<template>43<div class="container">44<ThirdPartyDatePicker />45</div>46</template>4748<style scoped>49/* GOOD: Use :deep() to style child component elements */50.container :deep(.date-input) {51border-color: blue;52}5354.container :deep(.calendar-popup) {55background: white;56}5758/* Also correct: deep selector at root level */59:deep(.date-picker-wrapper) {60padding: 1rem;61}62</style>63```6465## How Scoped CSS Works6667Vue scoped CSS adds a unique data attribute to all elements in the component's template and appends it to CSS selectors:6869```vue70<!-- Template output -->71<div class="container" data-v-7ba5bd90>72<!-- Child component elements DON'T get data-v-7ba5bd90 -->73<div class="date-input">...</div>74</div>75```7677```css78/* Generated scoped CSS */79.container[data-v-7ba5bd90] .date-input[data-v-7ba5bd90] { ... }80/* ^ This won't match because .date-input doesn't have the attribute */81```8283## Vue 3 Deep Selector Syntax8485```vue86<style scoped>87/* Vue 3 recommended syntax */88.parent :deep(.child-class) {89color: red;90}9192/* DEPRECATED - don't use these in Vue 3 */93.parent >>> .child-class { } /* Won't work in SCSS */94.parent /deep/ .child-class { } /* Deprecated */95.parent ::v-deep .child-class { } /* Old syntax */96</style>97```9899## Scoping Deep Selectors for Safety100101Always scope `:deep()` to a parent selector to limit its reach:102103```vue104<style scoped>105/* BAD: Affects ALL .btn elements in child components globally */106:deep(.btn) {107background: blue;108}109110/* GOOD: Only affects .btn inside .my-component */111.my-component :deep(.btn) {112background: blue;113}114</style>115```116117## Child Component Root Element Exception118119Note: A child component's root element IS affected by parent scoped CSS. This is intentional for layout purposes:120121```vue122<!-- Parent.vue -->123<template>124<ChildComponent class="styled-child" />125</template>126127<style scoped>128/* This WILL work - targets child's root element */129.styled-child {130margin: 1rem;131border: 1px solid gray;132}133</style>134```135136## Performance Consideration137138Using `:deep()` with element selectors can be slower:139140```vue141<style scoped>142/* SLOWER: Element selector with deep */143.container :deep(p) {144color: red;145}146147/* FASTER: Class selector with deep */148.container :deep(.paragraph) {149color: red;150}151</style>152```153154## Reference155- [Vue.js Scoped CSS - Deep Selectors](https://vuejs.org/api/sfc-css-features.html#deep-selectors)156- [Vue Loader Scoped CSS](https://vue-loader.vuejs.org/guide/scoped-css.html)157