4

I have mat-list in my html template:

<mat-list *ngFor="let item of items">
  <mat-list-item><mat-icon>add</mat-icon> {{ item.title }}</mat-list-item>
</mat-list>

I want to display mat-icon only on mat-list-item hover. So I come up with this css:

mat-icon {
  display: none;
}

mat-list-item:hover + mat-icon {
  display: block;
}

But for some reason it is does not work

However if I do try to change the background color it is works:

mat-list-item:hover {
  background-color: #3f51b5;
}

It is probably something to do with mat-icon Thoughts?

sreginogemoh
  • 9,074
  • 25
  • 83
  • 142

3 Answers3

6

try this

mat-icon{
  display: none;
}

mat-list-item:hover mat-icon{
  display: block;
}

you do not need + adjacent selectors

demo

Just code
  • 13,105
  • 10
  • 49
  • 89
0

try this

  mat-icon {
      display: none;
    }

    mat-list-item:hover > mat-icon {
      display: block;
    }
Shailesh Ladumor
  • 6,474
  • 4
  • 37
  • 51
0

You can try this code here :

mat-icon{
  display: none;
}

mat-list-item:hover mat-icon{ display: block; }

OR

mat-list-item:hover > mat-icon{ display: block; }

You tried the item:hover + children it's not currect, beacus the + selector is the imedite sibling selector, it's not select children.

I give the two code here

  1. mat-list-item:hover mat-icon{ display: block; } that means any children inside the mat-list-item class select this.

  2. and the other hand mat-list-item:hover > mat-icon{ display: block; } this means the children but it's select the direct children as like ul > li it's select directed children, not children children.

this is the concept of the CSS selector You can learn more about CSS selectors: https://www.w3schools.com/cssref/css_selectors.asp or https://www.w3schools.com/cssref/trysel.asp

Thank you

Md. Abu Sayed
  • 2,176
  • 2
  • 12
  • 24