【问题标题】:How can i center a <mat-card> in the middle of the screen in Angular 9如何在 Angular 9 的屏幕中间居中放置 <mat-card>
【发布时间】:2020-06-03 03:09:46
【问题描述】:

* 我有一个&lt;mat-card&gt;,我想在我尝试过的屏幕中间居中,但它没有居中。它在toggle-device-toolbar 中应该看起来不错,因为它适用于手机,我需要一些帮助。我正在使用angular-materialng-bootstrap*

这是&lt;mat-card&gt;的代码

<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>

这就是现在的样子

【问题讨论】:

  • 我想知道为什么要同时使用 Bootstrap 和 Material?无论如何,您可能必须使用 FlexBox 来对齐和证明内容居中。尝试在 .container div 中添加另一个 div。 ==>
    ...
  • 我同时使用这两种方法,因为我是使用材料设计的新手,我正在努力适应它。让我试试你说的
  • 关于这个话题有很多问题/答案。这是一个有用的:stackoverflow.com/questions/39697530/…

标签: css angular bootstrap-4 angular-material ng-bootstrap


【解决方案1】:

我就是这样解决的

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>

【讨论】:

    【解决方案2】:

    你可以用Flexbox做到这一点

    在你必须添加的组件样式中:

    .container {
      display: flex; 
      align-items: center; 
      justify-content: center;
    }
    

    此外,如果您将 grid 更改为 flex,它也应该可以工作。

    .container {
      display: grid; 
      align-items: center; 
      justify-content: center;
    }
    

    【讨论】:

    • 项目水平对齐但不垂直
    • 对不起,我没听懂。您是否只想水平对齐项目而不是垂直对齐?或者我证明的解决方案是否仅水平对齐项目而不是垂直对齐?
    • 我希望它水平和垂直对齐,但上面的代码只水平对齐
    • 你用display: 'grid'代替display: 'flex'试试?
    • 是的,在我的代码中我意识到它已经水平对齐并且仍然需要垂直对齐
    猜你喜欢
    • 2018-11-08
    • 2019-07-25
    • 2021-12-13
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多