You can see this thread for some older solutions: How to style child components from parent component's CSS file? keep in mind ::ng-deep
is deprecated.
I prefer the following and I will probably add it to that thread.
The only way to get your css to permeate down to a child is to make the css file global. You can use ViewEncapsulation.None
to make the css of the entire component global, but that's pretty messy. I prefer to just put this global class inside the global styles.css
, or you can make a new file and add it to the styles
array in angular.json
.
So the question is what do we use as the selector? We can't just select button
as it will override all the buttons in the project, and we don't want to use .ngx-dropdown-button
alone because we might want different styles for different dropdowns. We want to add a specific class inside the component first. Make sure it is unique, probably best to use the component's name.
<ngx-select-dropdown
class="my-component-name-dropdown"
[config]="config"
[options]="dropdownOptions"
[(ngModel)]="dataModel"
[multiple]="false"
></ngx-select-dropdown>
Now we can select this specific dropdown from our global styles file. Though, we need to make sure we achieve a high enough specificity to override the default styles. The quick and dirty way is to just use !important
next to every property, but then you leave no way to override your css. A better way is to repeat a class name until we achieve the desired specificity. I found I needed to repeat a class three times here.
In Global Styles File
.my-component-name-dropdown
.ngx-dropdown-button.ngx-dropdown-button.ngx-dropdown-button {
color: red;
}
Or the quick and dirty way
.my-component-name-dropdown .ngx-dropdown-button {
color: red !important;
}
Of course if you want to override all ngx-dropdown-buttons, the unique class is not necessary and you can just use the .ngx-dropdown-button
class.
Note: Many UI components have a class
option in the config / options object to let you override the default css, you may want to request that the devs add this.