5

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?

Sergino
  • 10,128
  • 30
  • 98
  • 159

3 Answers3

8

try this

mat-icon{
  display: none;
}

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

you do not need + adjacent selectors

demo

Just code
  • 13,553
  • 10
  • 51
  • 93
2

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,396
  • 2
  • 18
  • 26
0

try this

  mat-icon {
      display: none;
    }

    mat-list-item:hover > mat-icon {
      display: block;
    }
Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55