ng-multiselect-dropdown custom css
Asked Answered
I

4

8

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

Iatrogenic answered 18/3, 2019 at 12:17 Comment(5)
you can add above css in your global style i.e style.css file with removing the angular default appended classesPinkard
to override global style rule on the component style file, add this on your component encapsulation: ViewEncapsulation.None,Chaparro
even by adding in the global style sheet it is not working @PinkardIatrogenic
i added encapsulation : ViewEncapsulation.None but still it is loading with its default css @JoelJosephIatrogenic
also add !important to your component stylesChaparro
P
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!

Pinkard answered 18/3, 2019 at 12:19 Comment(3)
added the CSS in the global stylesheet but it is not workingIatrogenic
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>Iatrogenic
You forgot .container in th start .container .multiselect-dropdown .dropdown-btn {Simas
C
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.

Claqueur answered 15/11, 2019 at 13:21 Comment(2)
Best answer because it does'nt require to change global stylesheet or component.Saba
Great Answer still works in 2023. I used it with ng-multi-select where I need to increase height of dropdownWolfgang
M
1

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

:host ::ng-deep {
  .multiselect-dropdown {
    background: white
  }
}
Mammalian answered 12/2, 2020 at 17:44 Comment(0)
A
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);
  ....
}
Aerodrome answered 3/9, 2024 at 19:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.