Santhosh
Santhosh

Reputation: 920

ng-multiselect-dropdown custom css

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

Upvotes: 8

Views: 34952

Answers (4)

Naod Agere
Naod Agere

Reputation: 59

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);
  ....
}

Upvotes: 0

JP_
JP_

Reputation: 410

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.

Upvotes: 10

Matt Anders
Matt Anders

Reputation: 79

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

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

Upvotes: 1

TheParam
TheParam

Reputation: 10561

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!

Upvotes: 10

Related Questions