Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material - Change color of mat-list-option on selected

How can I change the color of selected option of mat-list-option? Right now I have something like this:

Current list enter image description here

I want to change the color of whole option or card 'on select' to green. Something like this: enter image description here

My code is like this:

<mat-selection-list #list>
    <mat-list-option *ngFor="let yuvak of yuvaks">
    {yuvak.name}
    {yuvak.phonenumber}
     </mat-list-option>
</mat-selection-list>
like image 750
Tanmay Parmar Avatar asked Nov 22 '18 23:11

Tanmay Parmar


People also ask

How do I use material select in angular?

You can read more about selects in the Material Design spec. To use angular material select, use <mat-select> formControl for selecting a value from the set of options. It is designed to work inside of an <mat-form-field> element. We can add options to the select by adding <mat-option> elements to the <mat-select>.

How to create a material dropdown in angular?

The width of the list depends on the length of the text inside the <option> tag. The width can be regulated with CSS styles, as well. To create a material dropdown in Angular, use the <mat-select> form control. Angular material provides <mat-select> form control for selecting a value from a set of options, similar to the native <select> element.

What is the default background color value for the angular material?

The accepted answer works fine, but it uses a hardcoded color value ( background: rgba (0, 139, 139, 0.7) ). This approach will actually break your styles and colors if you decide to switch to another pre-build material theme or use a custom theme (as described in Theming your Angular Material app page).

How to use mat-form-field in material angular app?

The <mat-form-field> is used to wrap several Angular Material components and apply common Text field styles such as the underline, floating label, and hint messages. Here, we have imported all the components in our Material Angular App. We can add other elements in the future if we need them.


3 Answers

Drop Down:

The mat-list-option has mat-option.mat-active which triggered when option is active and mat-option.mat-selected when an option is selected. Add the following to your CSS to modify the active or selected styles, depends on your need.

.mat-option.mat-active {
  background: blue !important;
}

.mat-option.mat-selected {
  background: red !important;
}

Working Demo

Selection List:

The selection list has aria-selected attribute and by default it is false. It changes to true if you select the item. All you need is to set CSS as below:

.mat-list-option[aria-selected="true"] {
  background: rgba(200, 210, 90, 0.7);
}

Working Demo

like image 63
Maihan Nijat Avatar answered Nov 09 '22 00:11

Maihan Nijat


You can use aria-selected="true" attribute from mat-list-option tag to target the selected option,
and provide corresponding css properties for the same.

mat-list-option[aria-selected="true"] {
  background: rgba(0, 139, 139, 0.7);
}

Stackblitz Working Demo

like image 32
Abhishek Kumar Avatar answered Nov 08 '22 23:11

Abhishek Kumar


The accepted answer works fine, but it uses a hardcoded color value (background: rgba(0, 139, 139, 0.7)). This approach will actually break your styles and colors if you decide to switch to another pre-build material theme or use a custom theme (as described in Theming your Angular Material app page).

So, if you use SCSS, you can use the following code in your component's style file:

@import '~@angular/material/theming';

mat-list-option[aria-selected="true"] {
  background: mat-color($mat-light-theme-background, hover, 0.12);
}

The above code is adapted from mat-select options - in this way, you will have a consistent look in the entire app: .mat-option.mat-selected:not(.mat-option-multiple) { background: mat-color($background, hover, 0.12);}

Demo: https://stackblitz.com/edit/material-selection-list-5-0-0-selection-color-change-qaq1xr

Or, if you use a dark theme, change code as follows:

mat-list-option[aria-selected="true"] {
  background: mat-color($mat-dark-theme-background, hover, 0.12);
}

Demo: https://stackblitz.com/edit/material-selection-list-5-0-0-selection-color-change-w8beng

If you just want to use a custom color, I suggest to pick one from Material Specs, that are also exposed in Angular Material Design scss.

$primaryPalette: mat-palette($mat-green);

mat-list-option[aria-selected="true"] {
  background: mat-color($primaryPalette, 500);
}

Demo: https://stackblitz.com/edit/material-selection-list-5-0-0-selection-color-change-tt3nyj

like image 39
andreivictor Avatar answered Nov 08 '22 23:11

andreivictor