如何使用Angular Flex Layout垂直居中?

Chr*_*ian 6 css css3 flexbox angular-flex-layout angular

我已经创建了一个简单的登录组件,我想垂直居中,但我不知道如何使用Angular Flex Layout库实现这一目标.

app.component.html

<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)

login.component.html

<div fxLayout fxLayoutAlign="center center">
    <mat-card fxFlex="30%">
        <mat-card-title>Login</mat-card-title>

        <mat-card-content fxLayout="column">
            <mat-form-field>
                <input matInput placeholder="Username">
            </mat-form-field>
            <mat-form-field>
                <input matInput placeholder="Password">
            </mat-form-field>
        </mat-card-content>

        <button mat-raised-button color="accent">Login</button>
    </mat-card>
</div>
Run Code Online (Sandbox Code Playgroud)

styles.scss

body{
    margin: 0;
    background-color: #eff2f5;
}
Run Code Online (Sandbox Code Playgroud)

截图: 在此输入图像描述

小智 10

您必须指定 fxLayout 的高度(已编辑)

<div fxLayout="row" fxLayoutAlign="center center" class="row-height">
    <mat-card fxFlex="30%">
        <mat-card-title>Login</mat-card-title>

        <mat-card-content fxLayout="column">
            <mat-form-field>
                <input matInput placeholder="Username">
            </mat-form-field>
            <mat-form-field>
                <input matInput placeholder="Password">
            </mat-form-field>
        </mat-card-content>

        <button mat-raised-button color="accent">Login</button>
    </mat-card>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.row-height {
    height: 100%
}
Run Code Online (Sandbox Code Playgroud)


Kai*_*aja 9

如果容器元素与其内容的高度相同,则使用flexbox垂直居中元素无效.<div>在您的示例中使用最高级别占用所有可用的垂直空间height: 100%(或者某些其他Angular Flex Layout特定解决方案,如果可用的话 - 可能fxFlexFill)应将其内容放在您想要的位置.

  • 我添加了style =“ min-height:100vh” (2认同)

Eri*_*cky 7

如果父元素的高度已知,则只需执行以下操作fxLayoutAlign="center center"

<section class="intro-section">
  <div
   fxLayout="row"
   fxLayout.xs="column" 
   fxFlexFill
   fxLayoutAlign="center center"
  >
    <div fxFlex="50">
      <h1>MAYABI PORTFOLIO MULTIPURPOSE THEME</h1>
      <p>lorem ipsum dolor sit amt, consectet adop adipisicing elit, sed do eiusmod 
teporara incididunt ugt labore.</p>
    </div>
    <div fxLayout="50">
      hello
   </div>
  </div>
</section>

.intro-section {
   height: 500px;
}
Run Code Online (Sandbox Code Playgroud)