Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the css for Angular Material paginator?

I want to modify the default look of Angular Material Paginator. For example, I want to change the justify-content property to flex-start.

.mat-paginator-container {
display: flex;
align-items: center;
justify-content: flex-end;
min-height: 56px;
padding: 0 8px;
flex-wrap: wrap-reverse;
width: 100%;}

What I did was- I pasted the css below in styles.css file

.mat-paginator-container {
display: flex;
align-items: center;
justify-content: flex-start;
min-height: 56px;
padding: 0 8px;
flex-wrap: wrap-reverse;
width: 100%;}

But that didn't work. I also tried the css in the component's css file. But that also didn't work. So how can I modify the css ?

UPDATE

Turns out that I had to use !important and it worked!. I was avoiding that that but had no other choice. Any better solutions, then please let me know.

like image 218
Saif Ali Avatar asked Dec 10 '18 06:12

Saif Ali


People also ask

How do you customize the mat Paginator dropdown position?

Just provide MatSelectConfig service at desired level (Component, Module or root) setting disableOptionCentering to true , using MAT_SELECT_CONFIG Injection Token.

What is Paginator angular?

The paginator displays a dropdown of page sizes for the user to choose from. The options for this dropdown can be set via pageSizeOptions. The current pageSize will always appear in the dropdown, even if it is not included in pageSizeOptions.


2 Answers

you can use ::ng-deep to change the style of mat-paginator

:host ::ng-deep.mat-paginator .mat-paginator-container{
  justify-content: flex-start;
}

Without using ::ng-deep( for Angular 8+ )

Turn off encapsulation of your component inside which you change the padding.

You can do this by

 import {Component,ViewEncapsulation} from '@angular/core';
 @Component({
   selector: 'example',
   templateUrl: 'example.component.html',
   styleUrls: ['example.component.css'],
   encapsulation : ViewEncapsulation.None,
 })
 export class ExampleComponent {}

Wrap the component you want to style in a custom class. So it wont affect any other mat-paginator components.

like image 170
Akhi Akl Avatar answered Sep 28 '22 01:09

Akhi Akl


By adding !important in css, I didn`t succeed. So I did this in app.component.css file:

.mat-paginator {
     display: flex;
     justify-content: center;
 }

But you might complain that probably you don`t want the paginator to be "flex". You can put the hole paginator in a div tag to make sure that it will be shown as a block:

In app.component.html:

  ...
     <div class="container">
         <mat-paginator>
              ......
         </mat-paginator>
     </div>
  ... 


PS: Adding !important is NOT a good idea.

like image 29
Omid Ataollahi Avatar answered Sep 28 '22 00:09

Omid Ataollahi