如何在 Angular 9 中将 <mat-card> 置于屏幕中央
How can i center a <mat-card> in the middle of the screen in Angular 9
* 我有一个 <mat-card>
我想在屏幕中间居中,但我试过但没有居中。它在 toggle-device-toolbar
中应该看起来不错,因为它适用于手机,我需要一些关于 this.Im 使用 angular-material
和 ng-bootstrap
*
的帮助
这是<mat-card>
的代码
<div class="container ">
<mat-card>
<mat-radio-group aria-label="Select an option" [(ngModel)]="radio_btn">
<mat-radio-button [value]="true" >Admin</mat-radio-button>
<mat-radio-button [value]="false">User</mat-radio-button>
</mat-radio-group>
<div class="row justify-content-center" *ngIf="radio_btn==true;else form2">
<form class="example-form " [formGroup]="loginForm" (ngSubmit)="send()">
<mat-form-field class="example-full-width">
<input matInput formControlName="Identifier" placeholder="User" >
</mat-form-field><br>
<div *ngIf="loginForm.get('Identifier').hasError('Identifier') && loginForm.get('Identifier').touched">Introduce an email</div><br>
<mat-form-field class="example-full-width">
<input matInput formControlName="Password" placeholder="Password" type="password">
</mat-form-field><br>
<div *ngIf="loginForm.get('Password').hasError('Password') && loginForm.get('Password').touched">Introduce the correctly password</div><br>
<button mat-raised-button [disabled]="loginForm.invalid" class="colour_button " type="submit">Login 1</button>
</form>
</div>
</mat-card>
</div>
这是现在的样子
你可以用 Flexbox
在你要添加的组件样式中:
.container {
display: flex;
align-items: center;
justify-content: center;
}
此外,如果您将 grid
更改为 flex
,它也应该有效。
.container {
display: grid;
align-items: center;
justify-content: center;
}
我就是这样解决的
This is my css file
.center{
width: 75%;
margin: 10px auto;
}
.main-div{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
This is my html file
<div class="main-div">
<mat-card class="z-depth center" flex="50" >
<mat-radio-group aria-label="Select an option" [(ngModel)]="radio_btn">
<mat-radio-button [value]="true" >Admin</mat-radio-button>
<mat-radio-button [value]="false">User</mat-radio-button>
</mat-radio-group>
<div *ngIf="radio_btn==true;else form2">
<form class="example-form " [formGroup]="loginForm" (ngSubmit)="send()">
<mat-form-field class="example-full-width">
<input matInput formControlName="Identifier" placeholder="User" >
</mat-form-field><br>
<div *ngIf="loginForm.get('Identifier').hasError('Identifier') && loginForm.get('Identifier').touched">Introduce an email</div><br>
<mat-form-field class="example-full-width">
<input matInput formControlName="Password" placeholder="Password" type="password">
</mat-form-field><br>
<div *ngIf="loginForm.get('Password').hasError('Password') && loginForm.get('Password').touched">Introduce the correctly password</div><br>
<button mat-raised-button [disabled]="loginForm.invalid" class="colour_button " type="submit">Login 1</button>
</form>
</div>
</mat-card>
</div>
* 我有一个 <mat-card>
我想在屏幕中间居中,但我试过但没有居中。它在 toggle-device-toolbar
中应该看起来不错,因为它适用于手机,我需要一些关于 this.Im 使用 angular-material
和 ng-bootstrap
*
这是<mat-card>
<div class="container ">
<mat-card>
<mat-radio-group aria-label="Select an option" [(ngModel)]="radio_btn">
<mat-radio-button [value]="true" >Admin</mat-radio-button>
<mat-radio-button [value]="false">User</mat-radio-button>
</mat-radio-group>
<div class="row justify-content-center" *ngIf="radio_btn==true;else form2">
<form class="example-form " [formGroup]="loginForm" (ngSubmit)="send()">
<mat-form-field class="example-full-width">
<input matInput formControlName="Identifier" placeholder="User" >
</mat-form-field><br>
<div *ngIf="loginForm.get('Identifier').hasError('Identifier') && loginForm.get('Identifier').touched">Introduce an email</div><br>
<mat-form-field class="example-full-width">
<input matInput formControlName="Password" placeholder="Password" type="password">
</mat-form-field><br>
<div *ngIf="loginForm.get('Password').hasError('Password') && loginForm.get('Password').touched">Introduce the correctly password</div><br>
<button mat-raised-button [disabled]="loginForm.invalid" class="colour_button " type="submit">Login 1</button>
</form>
</div>
</mat-card>
</div>
这是现在的样子
你可以用 Flexbox
在你要添加的组件样式中:
.container {
display: flex;
align-items: center;
justify-content: center;
}
此外,如果您将 grid
更改为 flex
,它也应该有效。
.container {
display: grid;
align-items: center;
justify-content: center;
}
我就是这样解决的
This is my css file
.center{
width: 75%;
margin: 10px auto;
}
.main-div{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
This is my html file
<div class="main-div">
<mat-card class="z-depth center" flex="50" >
<mat-radio-group aria-label="Select an option" [(ngModel)]="radio_btn">
<mat-radio-button [value]="true" >Admin</mat-radio-button>
<mat-radio-button [value]="false">User</mat-radio-button>
</mat-radio-group>
<div *ngIf="radio_btn==true;else form2">
<form class="example-form " [formGroup]="loginForm" (ngSubmit)="send()">
<mat-form-field class="example-full-width">
<input matInput formControlName="Identifier" placeholder="User" >
</mat-form-field><br>
<div *ngIf="loginForm.get('Identifier').hasError('Identifier') && loginForm.get('Identifier').touched">Introduce an email</div><br>
<mat-form-field class="example-full-width">
<input matInput formControlName="Password" placeholder="Password" type="password">
</mat-form-field><br>
<div *ngIf="loginForm.get('Password').hasError('Password') && loginForm.get('Password').touched">Introduce the correctly password</div><br>
<button mat-raised-button [disabled]="loginForm.invalid" class="colour_button " type="submit">Login 1</button>
</form>
</div>
</mat-card>
</div>