【问题标题】:How to center mat-card in angular materials?如何在角材料中将垫卡居中?
【发布时间】:2019-03-18 21:31:23
【问题描述】:

我有一张使用棱角材料设计的登录卡,每当我在页面上运行它时,内容都会粘在页面的左侧。

我尝试使用flexlayout-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>

这里的内容仍然没有居中:

【问题讨论】:

    标签: html css angular angular-material


    【解决方案1】:

    使用非常简单的 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>
    

    【讨论】:

      【解决方案2】:

      请尝试以下 css 方法。

      1. 第一种方式。
      .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%;
      }
      
      1. 第三条路。
      .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;
      }
      
      1. 第四条路。
      .mat-card-cvr{
          top:50%;
          width:100%;
          text-align:center;
          position:fixed;
      }
          
      .example-card{
          margin:0 auto;
          display:inline-block;
      }
      

      【讨论】:

      • 这只是水平居中,而不是垂直居中
      • 你的问题中没有提到垂直对齐?
      • 你能分享你的链接吗?我会尽力而为。
      • 我说的是页面中心
      • 请尝试第四种方式。
      【解决方案3】:

      这让我很惊讶:

      .btn-center{
         margin: 0 auto;
         display: flex;
      }
      

      【讨论】:

        【解决方案4】:

        Material 提供了 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

        【讨论】:

        • 这应该是公认的答案
        【解决方案5】:

        我尝试了 OP 答案,但我也必须控制宽度,所以必须在 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);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-02-19
          • 1970-01-01
          • 2017-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-19
          • 2019-04-17
          • 2019-03-11
          相关资源
          最近更新 更多