Skip to content

Commit 59f2020

Browse files
committed
new post: 2025-03-19-understanding-deep-selectors-in-vuejs-scoped-css.md
1 parent df801e2 commit 59f2020

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Understanding Deep Selectors in Vue.js Scoped CSS
3+
description: Explore the use of deep selectors in Vue.js to style nested components effectively with scoped CSS.
4+
date: 2025-03-19T11:07:43.000Z
5+
tags:
6+
- Vue.js
7+
- CSS
8+
- Web Development
9+
categories:
10+
- Development
11+
- Frontend
12+
math: false
13+
---
14+
15+
# Understanding Deep Selectors in Vue.js Scoped CSS
16+
17+
When developing applications with Vue.js, you often use scoped CSS to ensure that your styles are confined to specific components. While this encapsulation is beneficial for maintaining clean styles, there are situations when you want to target nested components that are not directly styled due to the scoped nature. This is where deep selectors come into play.
18+
19+
## The Problem with Scoped CSS
20+
21+
Scoped CSS (using `<style scoped>`) restricts your styles to the component they are defined in. This means that styles won't "leak" out to other components, preserving the design integrity. However, there are cases, especially when working with third-party component libraries like Vuetify, where you want to style child components or elements deeply nested within your component.
22+
23+
## The Solution: Deep Selectors
24+
25+
To handle this, Vue.js provides several "deep selectors" that allow you to penetrate the scope and apply styles to nested components:
26+
27+
- `>>>` - The legacy deep selector for scoped CSS, compatible with webpack/vue-cli.
28+
- `::v-deep` - The recommended deep selector in Vue 3, ensuring future compatibility.
29+
- `/deep/` - Another deprecated version that is still recognized by tools like SASS and PostCSS.
30+
31+
### Understanding the Selectors
32+
33+
| Selector | Description |
34+
|-----------|----------------------------------------------------|
35+
| `>>>` | Former deep selector for scoped CSS (webpack/vue-cli compatible). |
36+
| `/deep/` | Similar to `>>>` but written differently; less recommended. |
37+
| `::v-deep`| Officially recommended syntax starting from Vue 3 and Vite. |
38+
39+
All three selectors serve the same purpose: they break through the component's style encapsulation.
40+
41+
## Example Usage
42+
43+
Let’s consider an example where you want to style a button from Vuetify within your Vue component.
44+
45+
### Code Sample
46+
47+
```vue
48+
<template>
49+
<v-btn class="my-btn">Click Me</v-btn>
50+
</template>
51+
52+
<style scoped>
53+
.my-btn >>> .v-btn__content {
54+
color: red;
55+
}
56+
</style>
57+
```
58+
59+
Or using the modern syntax:
60+
61+
```vue
62+
<style scoped>
63+
.my-btn ::v-deep .v-btn__content {
64+
color: red;
65+
}
66+
</style>
67+
```
68+
69+
In the above examples, without a deep selector, the styles would not apply to `.v-btn__content` because it resides within a child component. By using `::v-deep` or `>>>`, we can effectively style it.
70+
71+
### Visualizing the Difference
72+
73+
Consider the following comparisons illustrating which styles work and which do not with and without deep selectors:
74+
75+
```css
76+
<style scoped>
77+
/* ❌ Does NOT work without a deep selector */
78+
.my-btn .v-btn__content {
79+
color: red;
80+
}
81+
82+
/* ✅ Works with a deep selector */
83+
.my-btn ::v-deep .v-btn__content {
84+
color: red;
85+
}
86+
</style>
87+
```
88+
89+
## Working with Vite and Vue 3
90+
91+
If you are using Vite along with Vue 3, you can simplify your syntax even further:
92+
93+
```vue
94+
<style scoped>
95+
.my-class ::v-deep(.child-class) {
96+
/* styles for deep children */
97+
}
98+
99+
/* Even more concise */
100+
::v-deep(.child-class) {
101+
/* global access */
102+
}
103+
</style>
104+
```
105+
106+
## Conclusion
107+
108+
Deep selectors are essential tools when working with scoped CSS in Vue.js, enabling you to effectively apply styles to nested components. When your styles seem ineffective due to scoped limitations, remember that selectors like `::v-deep` or `>>>` can help you reach those deep child elements.
109+
110+
Understanding and utilizing deep selectors ensures that your components not only look good but are also styled precisely as you intend them to be, even when using third-party libraries.

0 commit comments

Comments
 (0)