简体   繁体   English

垂直对齐 mat-checkbox 和 mat-form-field | Angular 材料 | Angular

[英]Vertically Align mat-checkbox and mat-form-field | Angular Material | Angular

I am trying to implement text Fields and checkbox side by side.我正在尝试并排实现文本字段和复选框。 Something like this.像这样的东西。 在此处输入图像描述

But, the problem is <mat-form-field> and <mat-checkbox> is not vertically aligning.但是,问题是<mat-form-field><mat-checkbox>没有垂直对齐。 How do I go forward by aligning both of them vertically?如何通过垂直对齐它们来向前 go ?

Here is the code:这是代码:

<mat-card>
    <mat-card-title>
        Family Information
    </mat-card-title>
    <form [formGroup]="familyInfo">
        <div fxLayout="row" fxLayoutAlign="flex-start" fxLayoutGap="40px">
            <mat-form-field fxFlex="10%">
                <mat-label>Contact Name</mat-label>
                <input matInput>
            </mat-form-field>
            <mat-form-field fxFlex="10%">
                <mat-label>Relation Type </mat-label>
                <mat-select>
                    <mat-option *ngFor="let relationType of relationTypes" [value]="relationType" color="accent">
                        {{relationType}}
                    </mat-option>
                </mat-select>
            </mat-form-field>
            <mat-form-field fxFlex="10%">
                <mat-label>Gender </mat-label>
                <mat-select>
                    <mat-option *ngFor="let gender of genders" [value]="gender" color="accent">
                        {{gender}}
                    </mat-option>
                </mat-select>
            </mat-form-field>
            <mat-form-field fxFlex="10%">
                <mat-label>Date Of Birth</mat-label>
                <input matInput [matDatepicker]="dateOfBirth" formControlName="dateOfBirth">
                <mat-datepicker-toggle matSuffix [for]="dateOfBirth"></mat-datepicker-toggle>
                <mat-datepicker #dateOfBirth></mat-datepicker>
            </mat-form-field>

            <mat-checkbox formControlName="isDependent">Is Dependent</mat-checkbox>

            <div fxFlex="100%"></div>
            <mat-card-actions>
                <button mat-raised-button color="primary" type="submit" class="align-right-button">
                    Save
                </button>
            </mat-card-actions>
        </div>

I tried using the <mat-form-field> , but as per angular material docs.我尝试使用<mat-form-field> ,但根据 angular 材料文档。 <mat-checkbox> cannot be a part of <mat-form-field> <mat-checkbox>不能是<mat-form-field>的一部分

You have to position the checkbox manually.您必须手动 position 复选框。 There is no other way to line the elements.没有其他方法可以排列元素。 Something like align-self: center for mat-checkbox.类似align-self: center for mat-checkbox。

Please be aware that mat-form-field has some custom paddings/margins.请注意mat-form-field有一些自定义填充/边距。 So, to have the perfect fitment you will need to inspect the element and add a couple px at the top.因此,要拥有完美的配件,您需要检查元素并在顶部添加几个 px。 Depending on font-size it differs.根据字体大小,它会有所不同。

Hopefully, it will help.希望它会有所帮助。 Good luck!祝你好运!

This is how I deal with it:这就是我的处理方式:

<mat-checkbox class="checklist-leaf-node" style="width: 100%;" #checkBox formControlName="isDependent"> Is Dependent</mat-checkbox>
<mat-form-field style="width: 0%;">
   <input matInput class="pointer" type="text">
</mat-form-field>

found a workaround using css and separating the mat-checkbox text into its own label tag with position relative找到了一种解决方法,使用 css 并将 mat-checkbox 文本分离到其自己的 label 标记中,position 相对

<mat-checkbox style="position:relative;top:-20px;left:15px;">
    </mat-checkbox>
    <label style="position:relative;left:27px;top:-10px;">
      some long text
    </label>

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

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