33

The mat-list component in angular/material2 applies a top and bottom padding of 16px. I'd like to make this 0px. I've tried applying a style with a higher specificity but it isn't working (or I'm doing it wrong).The style I'm trying to override is:

mat-list-item-content style applying 16px padding

I'm trying to override this with:

.list .mat-list .mat-list-item .mat-multi-line .mat-list-item-content {
  padding-top: 0;
  padding-bottom: 0;
}
<div class="list">
  <mat-list>
    <mat-list-item *ngFor="let item of queue">
      <h1 matLine>{{ item.id }}: {{ item.status}} {{ item.statusDate }}</h1>
      <p matLine>{{ item.name }}</p>
      <p matLine>for {{ item.customer }}</p>
      <div matLine>
        <button mat-icon-button (click)="openTab(item)">
          <mat-icon fontIcon="icon-open"></mat-icon>
        </button>
        <button *ngIf="showAssignToMe" mat-icon-button (click)="assignToMe(item)">
          <mat-icon fontIcon="icon-assign_to_me"></mat-icon>
        </button>
        <button mat-icon-button (click)="notes(item)">
          <mat-icon fontIcon="icon-comment"></mat-icon>
        </button>
      </div>
    </mat-list-item>
  </mat-list>
</div>
Mel
  • 5,837
  • 10
  • 37
  • 42
Craig
  • 932
  • 1
  • 11
  • 21

13 Answers13

33

None of the previous proposions worked for me on Angular 6, 7 & 8

What I proposed is a deprecated solution (https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep) but it still continues to work:

:host /deep/ .mat-list-item-content {
  padding: 0!important;
}
Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49
13

In Angular Material 6, the blank space between list items is not because of padding but due to .mat-list-item height. You can get a more compact list with:

.mat-list .mat-list-item {
  height: 50px; /* default is 72px */
}
Mel
  • 5,837
  • 10
  • 37
  • 42
10

Another option is to turn off the components ViewEncapsulation.

@Component({
    selector: 'list',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.css'],
    encapsulation: ViewEncapsulation.None
})
Ian Hoar
  • 1,174
  • 1
  • 19
  • 40
9

I had the same problem. Solved it like this

:host ::ng-deep .mat-list-item-content { padding: 0!important; }

Denis Murimi
  • 91
  • 1
  • 1
5

I know this is an older question, but I was banging my head trying to figure out how to override padding for the list item text next to a checkbox. The style class I was trying to change was

.mat-list-base .mat-list-item.mat-list-option .mat-list-item-content .mat-list-text

which had a left padding of 16px.

The trick is: it has to go in the root stylesheet. If you don't want it to apply everywhere, add another class to increase specificity. In my case, I added .less-padding like so:

.mat-list-base .less-padding.mat-list-item.mat-list-option .mat-list-item-content .mat-list-text { padding-left: 8px; }

Add the class to the <mat-list-option> tag in your HTML

Joe Moon
  • 145
  • 1
  • 7
  • This is true and works, but is strongly discouraged by the Materials guidelines since it will likely break during a future upgrade of Materials: https://material.angular.io/guide/customizing-component-styles – Daniel Methner Mar 17 '23 at 16:33
4
.mat-list-item{
  max-height: 25px;
}
Annanya Jain
  • 49
  • 1
  • 1
2

Just got it by doing this:

.mat-list-base .mat-list-item .mat-list-item-content {
    padding: 0px !important
}
Welyngton Dal Prá
  • 758
  • 1
  • 10
  • 19
1

All you have to do is to remove space between .mat-list-item and .mat-multi-line because both classes are applied to the same element. In other words use selector:

.list .mat-list .mat-list-item.mat-multi-line .mat-list-item-content
Wilhelm Olejnik
  • 2,382
  • 3
  • 14
  • 21
1

A fairly reusable method is getting the selectors from DevTools or whatever browser debug tool you are using.

For example, In Chrome: Right-click and inspect the element you want to remove the padding from i.e the <mat-list-item> element. In the styles pane filter by padding, or whichever property you are trying to change. You'll see the selectors that are setting padding.

Youll need to copy each one into your CSS/SCSS and add the properties you need. For example, I copied from the styles pane:

.mat-list-base[dense] .mat-list-item .mat-list-item-content, .mat-list-base[dense] .mat-list-option .mat-list-item-content

Use this in your stylesheet. You need to do this for each selector block in the style pane. From the section you've just copied, click the orange tick next to padding. You'll see the padding in next selector section below have its strikethroughs removed. Copy that sections selectors to your style sheet. Repeat through this process until you've got all the selectors needed.

My Style sheet ended up looking like this for removing the padding on a <mat-list-item>


.mat-list-base[dense] .mat-list-item .mat-list-item-content, .mat-list-base[dense] .mat-list-option .mat-list-item-content{
    padding: 0 !important;
    
}
.mat-list-base .mat-list-item .mat-list-item-content, .mat-list-base .mat-list-option .mat-list-item-content {
    padding: 0 !important;
}
MR-DS
  • 92
  • 3
1

To add on top of @ian-hoar's answer, once you have turned off View Encapsulation asbelow:

@Component({
selector: 'list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css'],
encapsulation: ViewEncapsulation.None
})

you don't have to rely on ::ng-deep or :host ::ng-deep etc as they are deprecated.

For instance, if you want to override padding for mat-list-item, all you have to do is

.list .mat-list-base .mat-list-item .mat-list-item-content {
  padding: 0 !important;
}
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
0

HTML

<mat-selection-list ...
    <mat-list-option formfilters *ngFor="let ...

SCSS (low level scss like src/styles.scss)

.mat-list-option[formfilters] {
    .mat-list-item-content {
        padding: 2px !important;
    }
}
0

You can override the styling in your styles.sccs file.

For example,

.mat-list-item-content {
  padding: 0 !important;
}

Note the use of !important, without !important the style will be overriden by the theme.

Joel Stevick
  • 1,638
  • 2
  • 16
  • 22
  • Why is this working if I add it to the styles.scss but not if i add it to the stylesheet of the component? – Silvan Jan 16 '23 at 08:33
-1

this worked for me

<mat-action-list *ngFor="let item of dataSource" style="padding-top: 0%;">
Rajan Garg
  • 19
  • 2
  • this is not the good approach. We should not encourage inline-CSS unless we dont have any option. In-line CSS is just like a patch; – Bimlendu Kumar Oct 07 '20 at 14:46