Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center mat-card in angular materials?

I have a login card that I designed using angular materials, whenever I run it on the page the content sticks to the left of the page.

I tried using flex and using layout-align="center center" but I still cannot bring the card to the center of the page

What am I doing wrong ?

Here is my html:

<mat-card class="example-card" layout="row" layout-align="center center">
  <mat-card-header>
    <div class="login-box-header" layout="row" layout-align="center center">
        <img src="https://image.ibb.co/hDqa3p/codershood.png">
    </div>
  </mat-card-header>
  <mat-card-content>
    <form class="example-form">
      <table class="example-full-width" cellspacing="0">
        <tr>
          <td>
            <mat-form-field class="example-full-width">
            <input matInput placeholder="Username" [(ngModel)]="username" name="username" required>
            </mat-form-field>
          </td>
        </tr>
        <tr>
        <td><mat-form-field class="example-full-width">
          <input matInput placeholder="Password" [(ngModel)]="password"type="password" name="password" required>
        </mat-form-field></td>
      </tr></table>
    </form>
    <mat-spinner [style.display]="showSpinner ? 'block' : 'none'"></mat-spinner>
  </mat-card-content>
  <mat-card-actions>
    <button mat-raised-button (click)="login()" color="primary">Login</button>
  </mat-card-actions>
</mat-card>

and here is the content still not centered:

enter image description here

like image 942
Chief Madog Avatar asked Oct 14 '18 06:10

Chief Madog


2 Answers

Fixed it by using Very simple Flex

.main-div{
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

and wrapping everything with a class

<!-- Classes main-div and example-card are catagorized in .css file -->
<div class="main-div">

<mat-card class="example-card">
  <mat-card-header>
    <div class="login-box-header">
        <!-- Src image is temporary hosted in image.ibb-->
        <img src="https://image.ibb.co/hDqa3p/codershood.png">
    </div>
  </mat-card-header>
  <mat-card-content>
    <form class="example-form">
      <table class="example-full-width" cellspacing="0">
        <tr>
          <td>
            <mat-form-field class="example-full-width">
            <input matInput placeholder="Username" [(ngModel)]="username" name="username" required>
            </mat-form-field>
          </td>
        </tr>
        <tr>
        <td><mat-form-field class="example-full-width">
          <input matInput placeholder="Password" [(ngModel)]="password"type="password" name="password" required>
        </mat-form-field></td>
      </tr></table>
    </form>
    <mat-spinner [style.display]="showSpinner ? 'block' : 'none'"></mat-spinner>
  </mat-card-content>
  <mat-card-actions>
    <button mat-raised-button (click)="login()" color="primary">Login</button>
  </mat-card-actions>
</mat-card>
like image 126
Chief Madog Avatar answered Oct 01 '22 07:10

Chief Madog


Please try below css methods.

  1. first way.
.example-card{
    display:inline-block;
}

Then create one cover div for "mat-card". Give a class name for cover div like "mat-card-cvr".

.mat-card-cvr{
   width: 100%;
   text-align:center;
}

2.second way.

.example-card{
    margin: 0 auto;
    width: 40%;
}
  1. Third way.
.example-card{
    margin: 0 auto;
    width: 40%;
    display: inline-block;
    vertical-align: middle;
}

Then create one cover div for "mat-card". Give a class name for cover div like "mat-card-cvr".

.mat-card-cvr{
    width: 100%;
    height: 500px;
    line-height: 500px;
    text-align: center;
}
  1. Fourth way.
.mat-card-cvr{
    top:50%;
    width:100%;
    text-align:center;
    position:fixed;
}
    
.example-card{
    margin:0 auto;
    display:inline-block;
}
like image 34
stalinrajindian Avatar answered Oct 01 '22 08:10

stalinrajindian