How to disable the effect of ViewEncapsulation.None? E.g. One of my component (firstComponent) defines a css class with some properties. There is secondComponent which uses the same css class. I want my "secondComponent" to use the different specific values for properties defined by first component stylesheet. How can I achieve this?
Note : I redefined the same class in "secondComponent" with different values, keeping the viewEncapsulation of secondComponent default. It didn't work for me.
FirstComponent:
@Component({
moduleId: module.id,
selector: "FirstComponent",
templateUrl: 'FirstComponent.component.html',
styleUrls: ['FirstComponent.component.css'],
encapsulation: ViewEncapsulation.None
})
FirstComponent.component.css
.ui-tree .ui-tree-container {
background-color: #252525;
color: white;
}
Second Component:
@Component({
moduleId: module.id,
selector: "SecondComponent",
templateUrl: 'SecondComponent.component.html',
styleUrls: ['SecondComponent.component.css'],
})
SecondComponent.Component.css
.ui-tree .ui-tree-container {
background-color: white;
color: black;
}
I am creating p-tree in both the component, which internally uses .ui-tree-container. I want my secondComponent's tree's background should be white, while for all other places tree's background should remain black.
If you want to reuse CSS white only customising somes parts, you could make use of scss variables
Step #1
Create a scss file with common properties and default colors
commontree.scss
Step #2
In your component's scss file, modify the variables' values if needed and import the comon scss file
component1.scsss
component2.scss
For this, you can (and probably should) keep view encapsulation to the default
ViewEncapsulation.Emulated
Stackblitz demo