简体   繁体   English

为角材料设置输入 mat-form-fields 的宽度

[英]Set width on input mat-form-fields for angular material

I am so very new to Angular, and I am trying to create a form with longer-than-default input fields.我对 Angular 非常陌生,我正在尝试创建一个比默认输入字段更长的表单。 This is my current code:这是我当前的代码:

person.component.html

<form class="new-person-form">
    <mat-card>
        <mat-form-field>
            <input matInput placeholder="Name">
        </mat-form-field>
        <mat-form-field>
            <input matInput placeholder="Birthday">
        </mat-form-field>
        <mat-checkbox>Active</mat-checkbox>
    </mat-card>
</form>

person.component.css

.mat-form-field {
    padding: 10px;
}

.mat-checkbox {
    padding: 10px;
}

person.component.html

<form class="new-person-form">
    <mat-card fxLayout="row">
        <mat-form-field>
            <input matInput placeholder="Name" fxFlex="50">
        </mat-form-field>
        <mat-form-field>
            <input matInput placeholder="Birthday" fxFlex="25">
        </mat-form-field>
        <mat-checkbox fxFlex="25">Active</mat-checkbox>
    </mat-card>
</form>

person.component.css

.mat-form-field {
    padding: 10px;
}

.mat-checkbox {
    padding: 10px;
}

And this is the result:这是结果:

|                                                                          |
|   Name___________  Birthday_______  [] Active                            |
|                                                                          |

I'm trying to lengthen Name to be about 50% of the page, so something like this:我试图将Name加长到页面的 50% 左右,所以是这样的:

|                                                                          |
|   Name___________________________  Birthday_______  [] Active            |
|                                                                          |

I'm not super great at CSS, so I think FlexLayout might be what I need, but so far I can't get it to work correctly.我不太擅长 CSS,所以我认为 FlexLayout 可能是我需要的,但到目前为止我无法让它正常工作。

This will change the width of matform field这将改变 matform 字段的宽度

<mat-form-field [style.width.px]=327>
  <input matInput placeholder="Template Name" value="{{templateName}}">
</mat-form-field>

The following will make the field 50% of the Viewport Width by using style="width:50vw"下面将使用style="width:50vw"使字段50% Viewport Width50%

<mat-form-field style="width:50vw" ...>
   ...
</mat-form-field>

Using fxLayout you can create a flexbox.使用 fxLayout 你可以创建一个 flexbox。 For making each element in flexbox to consume full row you should use fxFlexFill.为了使 flexbox 中的每个元素都占用整行,您应该使用 fxFlexFill。 For making an element consuming the remaining space you can use fxFlex.要使元素消耗剩余空间,您可以使用 fxFlex。

You can also use fxLayoutGap instead of padding in styles.您还可以使用 fxLayoutGap 代替样式中的填充。 Also if you want the elements to go to the next line when there is no more space you can use fxLayout="row wrap".此外,如果您希望元素在没有更多空间时转到下一行,您可以使用 fxLayout="row wrap"。

Here are some samples of what you were looking for:以下是您正在寻找的一些示例:

    <form class="new-person-form">
        <h3>full Rows:</h3>
        <mat-card fxLayout="row wrap">
            <mat-form-field fxFlexFill>
                <input matInput placeholder="Name" fxFlex="50">
        </mat-form-field>
        <mat-form-field fxFlexFill>
            <input matInput placeholder="Birthday" fxFlex="25">
        </mat-form-field>
        <mat-checkbox fxFlex="25">Active</mat-checkbox>
      </mat-card>
      <h3>50-25-25:(checkbox consumes same space as Birthday)</h3>
      <mat-card fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="15px">
          <mat-form-field fxFlex="50">
              <input matInput placeholder="Name">
          </mat-form-field>
          <mat-form-field fxFlex="25">
              <input matInput placeholder="Birthday">
          </mat-form-field>
          <mat-checkbox fxFlex="25">Active</mat-checkbox>
      </mat-card>
      <h3>50-remaining-required:(Birthday consumes remaining space)</h3>
      <mat-card fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="15px">
          <mat-form-field fxFlex="50">
              <input matInput placeholder="Name">
          </mat-form-field>
          <mat-form-field fxFlex>
              <input matInput placeholder="Birthday">
          </mat-form-field>
          <mat-checkbox>Active</mat-checkbox>
      </mat-card>
    </form>

I've also created aworking example in stack blitz .我还在stack blitz 中创建了一个工作示例

you can use that css tricks你可以使用那个 css 技巧


.mat-form-field{
   width:350px !important;
}

Forcing the styles should do the trick as强制样式应该可以解决问题

.mat-input-wrapper{
  width:400px !important;
}

OR或者

<mat-form-field style="width:400px !important" ...>
   ...
</mat-form-field>

Try adding a class if you want to handle not all the input fields on the page.如果您不想处理页面上的所有输入字段,请尝试添加一个类。

Some of the other answers have already sort of suggested this, but the key for me was to set the width on the mat-form-field instead of on the input element.其他一些答案已经暗示了这一点,但对我来说,关键是在mat-form-field而不是在input元素上设置宽度。 When I tried to set the input element's width property (or use CSS width on the input element) it had no effect at all.当我尝试设置input元素的width属性(或在input元素上使用 CSS width )时,它根本没有效果。

Add a class mat-form-field in order to decrease width of mat-input添加一个类mat-form-field以减少mat-input宽度

<mat-form-field class="reduce-width">
   <mat-label>Label</mat-label>
     <input required matInput type="text"[formControl]="controlName">
</mat-form-field>

.reduce-width {
  width: 256px;
  margin-left: 17px;
}

You can use the material documentation to his, just put the col size. 您可以使用材料文档,只需要输入col大小即可。 Read this to undestand: https://material.io/design/layout/responsive-layout-grid.html#columns-gutters-margins 请阅读以下内容以了解它们: https ://material.io/design/layout/sensitive-layout-grid.html#columns-gutters-margins

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM