Heena
Heena

Reputation: 1773

Unable to position mat-card at he centre of my window using flex layout

I am new to Flex Layout and just started studying how to use angular material with flex layout.

I have implemented a simple login form in angular 2 using angular material. I want to use flex layout with angular material.

I used mat-card in which i have aligned my form elements. But i want to position mat-card at the centre of my window.

This is my code

myform.component.html

<div fxlayout="column" style="background: white;">

<mat-card fxFlex>   
            <form  (ngSubmit)="onSubmit(user.username,user.password)" #myForm="ngForm">

                                <p style="font-size: 30x;">Space Study</p>
                                <p style="font-size: 14px;">Sign In</p>
                                <mat-input-container class="form-row">
                                    <span matTooltip="Username">
                                        <input matInput placeholder=" " id="username" [(ngModel)]="user.username" name="username">
                                    </span>
                                    <span matPrefix><mat-icon>person&nbsp;&nbsp;</mat-icon></span>
                                </mat-input-container>

                                <mat-input-container class="form-row">
                                    <span matTooltip="Password">
                                        <input matInput placeholder=" " id="password" [(ngModel)]="user.password" name="gpassword" [type]="hide ? 'password' : 'text'">
                                    </span>
                                    <span matPrefix><mat-icon>remove_red_eye&nbsp;&nbsp;</mat-icon></span>
                                </mat-input-container>

                    <button mat-raised-button color="primary" type="submit" mat-raised-button>Login</button>

                    <mat-progress-bar mode="indeterminate" *ngIf="showprogressbar"></mat-progress-bar>
            </form>

</mat-card>

</div>

myform.component.css

mat-card{
    width: 350px;
    height: 400px;
    align-self: center; 
}

.form-row{
    display: flex;
}

.form-row input{
    flex: 1;
}   

Here is my output window

enter image description here

Can anybody please tell me how can i place my mat-card in the centre of my screen..?

Upvotes: 2

Views: 10762

Answers (4)

Diego Mart&#236;nez
Diego Mart&#236;nez

Reputation: 24

the correct way to center one tag , into a container:

<div fxLayoutAlign="center center" fxFill> ... content </div>
  1. By default the div get's a full width
  2. WE use a fxLAyout align to center first horizontal and the second one is vertical, because we use a fxLayout="row" by default.
  3. We need to use fxFill, this property say's , if you use row in fxLayout , the width its 100% but the height its only te content. I use fxFill, to fill all the content, it's says that we cover 100% of the height of the patern tag, but if it's your login, the patern tag its body, that starts with min-height: 100% , if you chose to use fxLayout = "column", fxFill, will will all the width, because if you use FxLayout = columns the fxFill = rows and vicebersa.

Upvotes: 1

Rohit Sharma
Rohit Sharma

Reputation: 169

demo screenshotYou can do in this way.

app.component.ts

<section fxLayout="column" fxLayoutAlign="center center" fxLayoutGap="10px">
  <mat-card>
    <form (ngSubmit)="onSubmit(f)" #f="ngForm">
      <mat-form-field>
        <input type="text" matInput placeholder="Enter something">
      </mat-form-field>
      <mat-form-field>
        <input type="text" matInput placeholder="Enter something">
      </mat-form-field>
      <button mat-raised-button color="primary">Submit</button>
    </form>
  </mat-card>
</section>

app.component.css

mat-card {
  width: 350px;
  height: 200px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

section {
  vertical-align: middle;
  height: 100%;
}

style.css

html,
body {
  height: 100%;
  margin-left: 0;
  margin-right: 0;
  margin-top: 0;
  margin-bottom: 0;

}

Upvotes: 1

Sathwik Gangisetty
Sathwik Gangisetty

Reputation: 278

Try adding display:block;margin-left:auto; margin-right:auto; to mat-card class in css and remove align-self property, like this:

mat-card{
    width: 350px;
    height: 400px;
    display: block;
    margin-left:auto;
    margin-right:auto;  
    vertical-align:middle;
}

Upvotes: 5

Tomasz Kula
Tomasz Kula

Reputation: 16837

<div fxLayout="row" fxLayoutAlign="center center">
   <mat-card fxFlex> </mat-card>
</div>

You might also have to add

:host {
  display: block;
  height: 100%;
}

Upvotes: 3

Related Questions