5

Is there any way to make Angular Material with a sticky first column using CSS?.

Here is ready to edit Stackblitz code

I have tried to adapt this solution https://jsfiddle.net/zinoui/BmLpV/ but for some reasons, the first column is thrown out of the table itself and it loses styling.

<div class="zui-wrapper">
    <div class="zui-scroller">
        <table class="zui-table">
            <thead>
                <tr>
                    <th class="zui-sticky-col">Name</th>..........
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="zui-sticky-col">DeMarcus Cousins</td>
.......
                <tr>
            </tbody>
        </table>
    </div>
</div>


.zui-scroller {
    margin-left: 141px;
    overflow-x: scroll;
    overflow-y: visible;
    padding-bottom: 5px;
    width: 300px;
}
.zui-table .zui-sticky-col {
    border-left: solid 1px #DDEFEF;
    border-right: solid 1px #DDEFEF;
    left: 0;
    position: absolute;
    top: auto;
    width: 120px;
}
Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
Antoniossss
  • 31,590
  • 6
  • 57
  • 99

5 Answers5

4

With the angular material version 6 this has been made easy.

You can add sticky tag on columns that need to be sticky on the left of the table and stickyEnd tag for the ones on the right of the table.

here is a Stackblitz example

jmuhire
  • 2,091
  • 21
  • 19
2
td:first-child, th:first-child {
  position:sticky;
  left:0;
  z-index:1;
  background-color:grey;
}
Shafeeq Mohammed
  • 1,193
  • 17
  • 25
0

I have used stickyEnd to achieve this like below.

 <ng-container matColumnDef="12" class="white-bg" stickyEnd>
</ng-container>
Amol Suryawanshi
  • 2,108
  • 21
  • 29
0

.mat-table-sticky:first-child {
    border-right: 1px solid #e0e0e0;
}
<ng-container
            matColumnDef="name"
            sticky
>
</ng-container>

`

.mat-table-sticky:first-child {
    border-right: 1px solid #e0e0e0;
}

`


  • 1
    There is no explanation as to why this answers the question. Could you add some clarity please? In addition, please read our [tour page](https://stackoverflow.com/tour). – isakbob Nov 08 '19 at 16:04
-1

It worked for me using;

` :host ::ng-deep .mat-cell:first-child, .mat-header-cell:first-child {

position: sticky;

position: -webkit-sticky;

right:0;

z-index:1;

background:inherit;

border-left: 1px solid grey;

} `

vsg
  • 175
  • 2
  • 11