Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

378
Visualizações
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 ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda