Home > Web Front-end > CSS Tutorial > How to Style Child Components in Vue.js Using /deep/, >>>, ::v-deep, and :deep?

How to Style Child Components in Vue.js Using /deep/, >>>, ::v-deep, and :deep?

Linda Hamilton
Release: 2024-12-16 17:09:15
Original
593 people have browsed it

How to Style Child Components in Vue.js Using /deep/, >>>, ::v-deep, and :deep?
>>, ::v-deep, and :deep? " />

How to Use /deep/, >>>, or ::v-deep in Vue.js

When working with component structures in Vue.js, applying style rules to elements within child components becomes necessary. Here, Vue provides several options to achieve this: /deep/, >>>, and ::v-deep.

Vue 2.0 - 2.6

Sass: Utilize ::v-deep to penetrate child component boundaries:

::v-deep .child-class {
    background-color: #000;
}
Copy after login

Plain CSS: Employ >>> to achieve the same effect:

>>> .child-class {
    background-color: #000;
}
Copy after login

Vue 3 (and Vue 2.7)

Unified Selector: Vue 3 introduces :deep as the unified selector, regardless of Sass usage:

:deep(.child-class) {
    background-color: #000;
}
Copy after login

Slot Content: Styling elements passed through slots:

:slotted(.slot-class) {
    background-color: #000;
}
Copy after login

Global Styles: Applying styles globally from scoped components:

:global(.my-class) {
    background-color: #000;
}
Copy after login

Key Considerations:

  • All styles must be defined within scoped