--- title: Scoped CSS Cannot Style Slot Content Directly impact: HIGH impactDescription: Slot content receives the parent component's scope, not the child's, causing styles to fail unexpectedly type: gotcha tags: [vue3, sfc, scoped-css, slots, deep-selector] --- # Scoped CSS Cannot Style Slot Content Directly **Impact: HIGH** - When a parent passes content through a slot, that content receives the parent component's scoped style attributes, not the child component's. This means the child component cannot style slot content with regular scoped CSS. ## Task Checklist - [ ] Use `:deep()` selector in the wrapper component to style slot content - [ ] Alternatively, use `:slotted()` pseudo-selector to target slotted elements - [ ] For complex slot styling, consider using CSS modules or unscoped styles - [ ] Document expected slot content structure when styling assumptions exist **Problematic Code:** ```vue ``` ```vue ``` **Correct Code:** ```vue ``` ## Using :deep() Alternative ```vue ``` ## Why This Happens Slot content is compiled in the parent component's scope: ```vue

Card Title

Card description

``` The `

` has `data-v-parent123`, but Card's scoped CSS expects `data-v-card456`. ## :slotted() vs :deep() for Slots Both work, but have subtle differences: ```vue ``` ## Combining with Named Slots ```vue ``` ## Performance Tip: Use Classes Element selectors with `:slotted()` can be slower: ```vue ``` ## When to Use Unscoped Styles For complex slot styling, unscoped styles may be cleaner: ```vue ``` ## Reference - [Vue.js Scoped CSS - Slotted Selectors](https://vuejs.org/api/sfc-css-features.html#slotted-selectors) - [Vue.js Deep Selectors](https://vuejs.org/api/sfc-css-features.html#deep-selectors)