Creating unique styles for mat-option with Active and Hover effects in Angular Materials

Struggling to find a CSS solution for changing the text color of the active option and on hover. The lack of documentation on styling angular materials in CSS is proving to be an issue.

I've tried using

.mat-option.mat-selected {
  background: red;
  color:black;
}

The problem is that while the last selected item has a red background, the text color remains white instead of black. I need the text color to be black and the background to turn white when hovering over an option.

This is the mat-option I'm attempting to style

<mat-form-field class="example-full-width" appearance="outline">
         <mat-label id="placeholder">Find User</mat-label>
       <input type="text" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto"  type="text" name="userName" required minlength="3">
              <mat-autocomplete #auto="matAutocomplete" name="userName">
         <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
           {{option}}

         </mat-option>
       </mat-autocomplete>
   <!-- {{myControl.value}} -->

     </mat-form-field>

Answer №1

mat-list-option is given the class mat-option.mat-active when it's active, and mat-option.mat-selected when selected.

To style this behavior, include the following CSS in your stylesheet:

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

If you want to style the hover state, use the following code:

.mat-option:hover:not(.mat-option-disabled)

Answer №2

All the Material UI styles are located in the node_modules folder, making it easy to customize the Material CSS to suit your needs.

1) The most effective method is to modify the CSS file within the node_modules directory. For example, if you wish to change the hover style of a mat-select element, locate the appropriate CSS file. If you have installed the indigo-pink theme, you can find the necessary CSS file here:

node_modules/@angular/material/prebuilt-themes/indigo-pink.css

The class to target is

.mat-option:focus:not(.mat-option-disabled)

For instance:

.mat-option:focus:not(.mat-option-disabled){background:#3f51b5;color:white}

2) If altering the module's CSS directly is not desired, you can add custom CSS styles to the end of the styles array in the angular.json configuration file.

angular.json

   "styles": [
      "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
      "src/styles.scss"
   ],

The last CSS file referenced is styles.scss, where your custom styles should be placed. Remember to use "!important" for specificity.

3) When working with a customized Angular Material theme, look for this file:

./node_modules/@angular/material/_theming.scss

Edit the CSS within this file like so: .mat-option { color: mat-color($foreground, text);

&:hover:not(.mat-option-disabled),
&:focus:not(.mat-option-disabled) {
  background: #607d8b;
  color: white
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to track all Angular form controls that have switched between being enabled and disabled?

Summary: When a FormGroup contains a nested FormControl that changes from disabled to enabled or vice versa, the FormGroup is not marked as dirty. Details: How can you identify all form controls within a FormGroup that have switched between being enabled ...

Tips for resolving issues with dropdown menu placement

Here is the current state of my HTML and CSS code: <div class="header"> <div class="container-fluid"> <div class="row"> <div class="col-1"></div> <div class="col-10"></div> <div class="col-1 dropd ...

How can I eliminate the empty spaces surrounding an image?

How can I remove the extra space around an image that is placed inside a bootstrap column? On the desktop version, I was able to solve the issue by setting -1rem margins on the left and right. I tried adjusting the body margin and padding to 0, as well as ...

Issue with displaying image element over another element

I recently encountered a unique challenge with my SVG element, which acts as a container for a chessboard I developed. In certain key moments of the game, such as when a pawn is promoted and a player needs to choose a new piece, I found it necessary to hav ...

Tips for passing an array between components in Angular 2

My goal is to create a to-do list with multiple components. Initially, I have 2 components and plan to add more later. I will be sharing an array of tasks using the Tache class. Navbar Component import { Component } from '@angular/core'; impor ...

Angular reactive forms can be customized to include a patched version of the matTime

I have an angular mattimepicker in my project. When trying to use it in a reactive form, I am encountering issues with patching the value to the edit form <h1>Reactive Form</h1> <form [formGroup]="form"> <mat-form-fie ...

Having trouble navigating to the bottom of a VUEJS app?

I've been working on developing a chatbot app that utilizes a REST API to stream content. While the functionality of the app is running smoothly, I have encountered an issue with the scroll to bottom feature. Instead of automatically scrolling to disp ...

The issue with the CSS combination of :after and :hover:after across different HTML elements

Encountering an issue while attempting to create a rollover effect using CSS :after and :hover pseudo-elements. Check out the clock and facebook icons on the right side by visiting: See below for the CSS code: .icon { background: url('. ...

Error: The DatePipe in Angular is throwing an InvalidPipeArgument when trying to convert the object "[object Object]" into a date using data from PHP

I have an app built using Angular 9 and an API built with PHP. The PHP controller returns a list of objects with a property called finishDate. In PHP, finishDate is assigned to a DateTime object: $timeSheetsOrderDTO1 = new TimeSheetsOrderDTO; $timeSheetsOr ...

Creating a keyboard and mouse accessible drop-down menu

I have a project for a client that necessitates a unique drop-down menu, which can be accessed by clicking or using the keyboard to navigate to it. Currently, my solution is almost flawless, except for one issue: when you click the drop-down menu for the ...

Creating an appealing navbar in React by skillfully integrating the image logo and brand name for a

Having some trouble positioning my logo on the top left corner of the navbar. It keeps ending up in an awkward spot alongside the brand name. https://i.stack.imgur.com/XZjV9.png If anyone has any tips on how to properly align the logo and brand on the na ...

Formula for full width skew in three adjacent divs using SkewX() function

.container { height: 500px; width: 500px; position: relative; background: black; } .box { transform: skewX(20deg); width: 33%; height: 100%; position: absolute; } .first-box { background: purple; left: 0; } .second-box { background ...

How can I position my navbar above my background image using Bootstrap?

I am a newcomer to using bootstrap and I am attempting to create the following design: https://i.sstatic.net/EV9A1.png In the design, the navbar is situated on top of the background image. However, with my current setup, this is what I have: <!DOCTYPE ...

Concerns with combining key value pairs in Typescript Enums

Could you help me figure out how to properly implement an enum in my drop-down so that I can only display one value at a time? Currently, I am seeing both the key and the value in the list. This is my enum: export enum VMRole { "Kubemaster" = 0, "Kub ...

Modifying the color of a non-active tab - Material UI Tabs

Is there a way to customize the color of inactive tabs in Material UI tabs? I have noticed that they currently appear black, as shown in the screenshot here: screenshot Thank you! ...

Combining an image seamlessly with the background color in HTML

I'm having an issue with my HTML page where the background color is red. I've posted two images - one blends in perfectly with the background while the other creates white patches. How can I fix this problem? Check out the Fiddle Link for refere ...

Tips for integrating tooltips in a dynamic highcharts chart

This image shows the desired final output View Highcharts here and I am looking to merge the date and stock value in a single tooltip, how can this be achieved? highcharts ...

Break up in the middle of a sentence: Material UI

Currently facing an issue where my sentences break in the middle. Working with Material UI requires manual styling of different components like MUI. The problem I'm encountering can be seen below. I tried removing the display: flex, but then the squar ...

Utilizing jQuery for Summation Calculation

How can I use jQuery to calculate the total sum of prices for all products in a table with the column name "price"? Once calculated, how do I store this sum in a variable? <table id ="tab"> <th>Product</th><th>Price</th> ...

Is it possible to modify the shrinking behavior of Flexbox?

My goal is to mimic the layout of a specific webpage using flexbox in my version. However, I've noticed that my version, utilizing flexbox, adjusts to smaller screen sizes differently than the original. The original layout I'm trying to replicate ...