Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Attaching and toggling a CSS class of element in angular using it's HTML Selector

I have a dropdown component in angular 12. It is shows the selectize like behavior. It is implemented as

 <ngx-select-dropdown
        [config]="config"
        [options]="dropdownOptions"
        [(ngModel)]="dataModel"
        [multiple]="false"
      ></ngx-select-dropdown>

by using https://github.com/manishjanky/ngx-select-dropdown

After rendering it generates markup like

code generated by the dropdown

Now, I want to modify the class of the button (highlighted in yellow color) rendered in markup but I don't have direct access to the markup of the button How can we do that ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!