39

we are using Angular Material table for our app:

https://material.angular.io/components/table/overview

<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>

could you please to show how to highlight a row on mouse hover?

Vladyslav Plakhuta
  • 1,379
  • 2
  • 10
  • 15

9 Answers9

93

Add some CSS with the :hover selector to the mat-row elements:

.mat-row:hover {
  background-color: red;
}

StackBlitz demo

p4r1
  • 2,490
  • 16
  • 18
7

You can do this styling the component in your theme file:

 @mixin newName-theme($dark-theme)

    mat-table tbody tr:hover{
    cursor: pointer;
    background: #b4b4b433;
  }

 @include newName-theme($dark-theme);

you can find more examples here

Sandy Veliz
  • 690
  • 9
  • 16
  • This is better because it allows you to easily set different hover colors for different themes you may use in the same project. But I had to use `tr.mat-row:hover` – Carl Kroeger Ihl Sep 13 '19 at 13:48
6

in my case .mat-row:hover didn't work, it was highlighting the whole table. this works for me:

  tr.mat-row:hover {
      background-color: yellow;
  }
Sebastian Castaldi
  • 8,580
  • 3
  • 32
  • 24
4

Angular Material currently has that feature here but if you want to stylize more, here is my solution

  • 2
    It would be nice if you can quote some documentation. answer containing mostly link may be deleted – alt255 Aug 17 '19 at 20:09
  • 1
    Actually, it doesn't, you can see the linked stackblitz have custom style of `tr.example-element-row:not(.example-expanded-row):hover { background: #777; }` applied. It have nothing to do with default material styling. – im.pankratov Feb 10 '20 at 11:23
2

Angular 15 changed this slightly. I'm now using:

  tr.mat-mdc-row:hover {
      background-color: yellow;
  }
Corey
  • 226
  • 1
  • 13
2

If you use Angular material > 15 and also .css style file for components, this could be helpful

.mat-mdc-table .mdc-data-table__row:hover{
 background-color: rgba(236, 236, 236, 0.514) !important;
 }
B.Habibzadeh
  • 490
  • 6
  • 10
0

This worked for me. I applied Bootstrap 4 class .table-hover to Angular material table.

<table class="table-hover" mat-table>...</table>
Anudeep
  • 289
  • 3
  • 8
0

For me neither of those worked in Angular 15, had to set background color for <table> and then change it for <mat-row> and <mat-header-row> (it seems that it already has hover but in same background color):

.table {
  background-color: whitesmoke;
}

.header-row {
  background-color: white;
}

.element-row {
  background-color: white;
}

and assign those classes to mentioned elements

Paweł Sosnowski
  • 173
  • 3
  • 12
0

Thanks to B.Habibzadeh post here

for me this was enough

.mat-mdc-row:hover {
  background-color: red !important
}

And when you use your own class for all rows, even this worked for me:

.myRowClass:hover {
  font-weight: bold;
  background-color: red !important
}

Hope this will help someone who has issues in new Angular version.

lagugula
  • 63
  • 8