9

I am using the ng-multiselect-dropdown. How can I override the CSS?

component.html

<ng-multiselect-dropdown [placeholder]="'Select Region'" 
    [data]="dropdownList" [(ngModel)]="selectedItems" 
    [settings]="dropdownSettings" (onSelect)="onItemSelect($event)" 
    (onSelectAll)="onSelectAll($event)" > 
</ng-multiselect-dropdown>

component.css

    .multiselect-dropdown[_ngcontent-c5] .dropdown-btn[_ngcontent-c5] {
        display: inline-block;
        border: 1px solid #adadad;
        width: 100%;
        padding: 6px 12px;
        margin-bottom: 0;
        font-size: 12px;
        font-weight: 400;
        line-height: 1.1;
        text-align: left;
        vertical-align: middle;
        cursor: pointer;
        background-image: none;
        border-radius: 4px;
   }

I need to override the default CSS with the above CSS code

5
  • you can add above css in your global style i.e style.css file with removing the angular default appended classes Commented Mar 18, 2019 at 12:22
  • to override global style rule on the component style file, add this on your component encapsulation: ViewEncapsulation.None, Commented Mar 18, 2019 at 12:26
  • even by adding in the global style sheet it is not working @TheParam Commented Mar 18, 2019 at 12:46
  • i added encapsulation : ViewEncapsulation.None but still it is loading with its default css @JoelJoseph Commented Mar 18, 2019 at 12:55
  • also add !important to your component styles Commented Mar 18, 2019 at 12:58

4 Answers 4

10

Angular by default adds some _ngcontent-xx to your component CSS file so that it won't conflict with other components.

To solve your problem you need to add below CSS in your global style.css file or another way to make your component as encapsulation: ViewEncapsulation.None meaning its CSS won't append default classes of Angular.

Solution 1: Add in global stylesheet.

style.css

.multiselect-dropdown .dropdown-btn {
    display: inline-block;
    border: 1px solid #adadad;
    width: 100%;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 12px;
    font-weight: 400;
    line-height: 1.1;
    text-align: left;
    vertical-align: middle;
    cursor: pointer;
    background-image: none;
    border-radius: 4px;
}

Solution 2 Make component ViewEncapsulation.None

component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  encapsulation: ViewEncapsulation.None // Add this line
})
export class AppComponent  {

}

Here is solution on stackblitz

Hope this will help!

Sign up to request clarification or add additional context in comments.

3 Comments

added the CSS in the global stylesheet but it is not working
in html i have only <ng-multiselect-dropdown [placeholder]="'Select Region'" [data]="dropdownList" [(ngModel)]="selectedItems" [settings]="dropdownSettings" (onSelect)="onItemSelect($event)" (onSelectAll)="onSelectAll($event)" > </ng-multiselect-dropdown>
You forgot .container in th start .container .multiselect-dropdown .dropdown-btn {
10

You can try this way:

    :host ::ng-deep .multiselect-dropdown .dropdown-btn {
    display: inline-block;
    border: 1px solid #adadad;
    width: 100%;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 12px;
    font-weight: 400;
    line-height: 1.1;
    text-align: left;
    vertical-align: middle;
    cursor: pointer;
    background-image: none;
    border-radius: 4px;
}

you can override css of any node module or library by :host and ::ng-deep.

2 Comments

Best answer because it does'nt require to change global stylesheet or component.
Great Answer still works in 2023. I used it with ng-multi-select where I need to increase height of dropdown
1

Surround your scss with :host ::ng-deep block like this:

:host ::ng-deep {
  .multiselect-dropdown {
    background: white
  }
}

Comments

0

Unlike other form control CSS classes, the multiselect class required me to include the container class in the CSS. Only after doing this, along with including encapsulation: ViewEncapsulation.None in the component and adding !important, did it work.

.container .multiselect-dropdown .dropdown-btn  {
  padding: 10px 15px !important;
  font-size: 16px !important;
  width: 100% !important;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  ....
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.