[英]How to center mat-card in angular materials?
我有一個使用角材料設計的登錄卡,每當我在頁面上運行它時,內容都會粘在頁面的左側。
我嘗試使用flex
並使用layout-align="center center"
但我仍然無法將卡片帶到頁面的中心
我究竟做錯了什么 ?
這是我的 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>
這是內容仍未居中:
通過使用非常簡單的 Flex 修復它
.main-div{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
並用一個類包裝所有東西
<!-- 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>
請嘗試以下 css 方法。
.example-card{
display:inline-block;
}
然后為“mat-card”創建一個封面div。 為封面 div 指定一個類名,如“mat-card-cvr”。
.mat-card-cvr{
width: 100%;
text-align:center;
}
2.第二種方式。
.example-card{
margin: 0 auto;
width: 40%;
}
.example-card{
margin: 0 auto;
width: 40%;
display: inline-block;
vertical-align: middle;
}
然后為“mat-card”創建一個封面div。 為封面 div 指定一個類名,如“mat-card-cvr”。
.mat-card-cvr{
width: 100%;
height: 500px;
line-height: 500px;
text-align: center;
}
.mat-card-cvr{
top:50%;
width:100%;
text-align:center;
position:fixed;
}
.example-card{
margin:0 auto;
display:inline-block;
}
這讓我感到驚奇:
.btn-center{
margin: 0 auto;
display: flex;
}
材料提供 mat-grid-list 來排列行和列。 像這樣使用它:
<mat-grid-list cols="1" rows="1" rowHeight="2:1">
<mat-grid-tile>
<mat-card>
Your form here
</mat-card>
</mat-grid-tile>
</mat-grid-list>
有關更多信息,請參閱文檔https://material.angular.io/components/grid-list/overview
我試過 OP answer 但我也必須控制寬度,所以必須在 div 中使用 div 然后使用 mat-card
.centering-div{
display: flex;
justify-content: center;
}
.inner-container {
width: 80%;
max-width: 100%;
overflow: auto;
}
.my-card {
max-width: 100%;
color: rgb(63, 81, 181);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.