【问题标题】:"angular-flex-layout" not adding margin for "mat-form-fields" on mobile screens“angular-flex-layout”没有为移动屏幕上的“mat-form-fields”添加边距
【发布时间】:2019-11-14 12:04:44
【问题描述】:

我使用 angular material 和 flex-layout 创建了一个响应式表单。 它在桌面和 ipad 屏幕上运行良好。

对于移动设备,我想将表单域居中对齐,并在屏幕的左侧和右侧添加边距。有没有办法在 flex-layout 中做到这一点?

如果不是,如何使用 ngClass.xs 使用自定义 CSS 来完成?我尝试将宽度设置为 90%,但它没有正确居中并且看起来很奇怪。

<div class="container" fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="2%" fxLayoutAlign="center">

  <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
    <mat-label>Field 1</mat-label>
    <input matInput placeholder="Field 1">
    <mat-icon matSuffix>place</mat-icon>
  </mat-form-field>

  <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
    <mat-label>Field 2</mat-label>
    <input matInput placeholder="Field 2">
    <mat-icon matSuffix>business</mat-icon>
  </mat-form-field>

  <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
    <mat-label>Field 3</mat-label>
    <input matInput placeholder="Field 3">
    <mat-icon matSuffix>send</mat-icon>
  </mat-form-field>

  <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
    <mat-label>Field 4</mat-label>
    <input matInput placeholder="Field 4">
  </mat-form-field>

</div>

【问题讨论】:

    标签: css angular angular-flex-layout angular-material-7


    【解决方案1】:
    <div class="container" fxLayout="row" fxLayout.xs="column" fxLayoutGap="20px" fxLayoutAlign="center">
    
      <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
        <mat-label>Field 1</mat-label>
        <input matInput placeholder="Field 1">
        <mat-icon matSuffix>place</mat-icon>
      </mat-form-field>
    
      <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
        <mat-label>Field 2</mat-label>
        <input matInput placeholder="Field 2">
        <mat-icon matSuffix>business</mat-icon>
      </mat-form-field>
    
      <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
        <mat-label>Field 3</mat-label>
        <input matInput placeholder="Field 3">
        <mat-icon matSuffix>send</mat-icon>
      </mat-form-field>
    
      <mat-form-field appearance="standard" fxFlex="22%" fxFlex.xs="90%">
        <mat-label>Field 4</mat-label>
        <input matInput placeholder="Field 4">
      </mat-form-field>
    
    </div>
    

    将 fxLayoutGap.gt-xs="2%" 更改为 fxLayoutGap="20px"。

    【讨论】:

      【解决方案2】:

      最简单最方便的方法是在弹性容器上设置ngClass.xs

      <div class="container" fxLayout="row" fxLayout.xs="column" 
           fxLayoutGap.gt-xs="2%"  fxLayoutAlign="center" ngClass.xs="mobileContainer">
      

      .mobileContainer 在哪里

      .mobileContainer mat-form-field {
        margin: 8px 16px;
      }
      

      这是一个简单的演示https://stackblitz.com/edit/angular-dufpaa

      作为旁注;

      你不需要在mat-form-field 元素上使用fxFlex.xs="90%",因为fxLayout.xs="column" 容器在xs 屏幕上有列布局,而fxFlex.xs="90%" 在列布局上意味着“占据父母身高的90%”。因为 flex-shrink 默认为 1 这不会产生任何影响。

      【讨论】:

      • 谢谢,这很好用。如果fxLayoutGap.gt-xs="2%"能自动应用到手机屏幕上就好了。
      • 我很高兴它有帮助。 fxLayoutGap.gt-xs 表示“大于 xs 的屏幕”。如果您希望它也应用于xs 屏幕,则只需删除响应式选择器并将其用作fxLayoutGap="2%"
      • 我的意思是,如果它自动添加margin-leftmargin-right 那就太好了。而不是用 CSS 做这个 hack。
      • 对不起,我弄错了。 fxLayoutGap 是弹性项目之间的空间。所以不可能用fxLayoutGap 实现你想要的。恕我直言,ngClass.xs 的东西不是黑客,而是如何做到这一点。
      猜你喜欢
      • 2017-10-26
      • 2020-09-11
      • 2019-06-01
      • 1970-01-01
      • 2018-12-05
      • 2020-03-16
      • 1970-01-01
      • 2018-06-17
      • 2017-06-14
      相关资源
      最近更新 更多