Skip to content

Commit 2f77f42

Browse files
kb(common): css isolation solution
1 parent 2521fef commit 2f77f42

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

knowledge-base/common-css-isolation.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,40 @@ The problem is that this applies only to plain HTML elements, but it does not ap
7070

7171
## Solution
7272

73-
Use CSS rules that are global to the application and not scoped to a particular component through the CSS Isolation feature. You can also use the `Class` parameter of the Telerik components to cascade through it if you want to target particular instances only.
73+
There are two ways to go around this:
74+
75+
* Use CSS rules that are global to the application and not scoped to a particular component through the CSS Isolation feature. You can also use the `Class` parameter of the Telerik components to cascade through it if you want to target particular instances only.
76+
77+
* Use the `::deep` pseudoselector that the framework translates to the random identifier attribute it adds to the DOM in the scoped selectors, and wrap nested components in HTML elements such as `<span>` or `<div>`
78+
79+
>caption Sample CSS selector that uses `::deep` to cascade for nested components
80+
81+
````CSS
82+
.my-component-button-class,
83+
::deep .my-component-button-class {
84+
font-size: 20px !important;
85+
}
86+
````
87+
88+
>caption Sample way to wrap nested components in HTML elements from the current component so `::deep` rules can affect them
89+
90+
````CSHTML
91+
<p>This button's class is defined in the component's scoped css file. It's applied to the wrapping element from this component (a span in this case, make sure to use an appropriate one to have valid HTML) and the ::deep pseudoselector applies it to the button.</p>
92+
<span>
93+
<TelerikButton Class="my-component-button-class"
94+
Primary="true">
95+
The font size should be larger but the scoped class is not applied properly.
96+
</TelerikButton>
97+
</span>
98+
99+
<p>Custom component nesting has the same behavior - elements from child components do not get the CSS isolation attribute but you can also cascade through some HTML element</p>
100+
<div>
101+
<MyCustomComponent Class="my-component-button-class" />
102+
</div>
103+
104+
<p>This html successfully takes the class defined in the scoped css file because it is plain HTML on the component that defines the custom CSS rule.</p>
105+
<button class="my-component-button-class">
106+
The font size is larger thanks to the scoped css file.
107+
</button>
108+
````
109+

0 commit comments

Comments
 (0)