--- title: Scoped Styles May Not Apply to Teleported Content impact: MEDIUM impactDescription: Scoped styles can fail to apply to teleported elements due to data attribute limitations type: gotcha tags: [vue3, teleport, scoped-styles, css] --- # Scoped Styles May Not Apply to Teleported Content **Impact: MEDIUM** - When using scoped styles with Teleport, the styles may not apply correctly to teleported elements. This is a known limitation related to how Vue's scoped style attributes work with elements rendered outside the component's DOM tree. ## Task Checklist - [ ] Test scoped styles on teleported content - [ ] Use `:deep()` selector or non-scoped styles for teleported content - [ ] Consider CSS modules as an alternative - [ ] Keep teleported content styles in a separate non-scoped style block **Problem - Scoped Styles Not Applied:** ```vue ``` **Solution 1 - Use Non-Scoped Styles for Teleported Content:** ```vue ``` **Solution 2 - Use :deep() Selector:** ```vue ``` **Solution 3 - CSS Modules:** ```vue ``` ## Multi-Root Components with Teleport Using Teleport as one of multiple root nodes causes additional issues: ```vue ``` Pass classes explicitly to avoid inheritance issues: ```vue ``` ## Best Practice: Dedicated Modal Styles Create a dedicated stylesheet for modal/overlay components: ```css /* modal-styles.css */ .modal-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5); display: flex; align-items: center; justify-content: center; } .modal-content { background: white; border-radius: 8px; padding: 24px; max-width: 500px; width: 90%; } ``` ```vue ``` ## Reference - [Vue.js SFC CSS Features - Scoped CSS](https://vuejs.org/api/sfc-css-features.html#scoped-css) - [GitHub Issue #2047 - Scoped styles and teleport](https://github.com/vuejs/core/issues/2047)