Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. enter image description here

But, the problem is <mat-form-field> and <mat-checkbox> is not vertically aligning. How do I go forward by aligning both of them vertically?

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-checkbox> cannot be a part of <mat-form-field>

like image 425
abhigyan nayak Avatar asked Nov 06 '22 06:11

abhigyan nayak


People also ask

How do I keep my mat-checkbox checked by default?

To set mat-checkbox checked by default we use checked attribute or [ngModel] as shown below. We can set the IsChecked property to true in constructor.

What is Mat-checkbox?

<mat-checkbox> supports an indeterminate state, similar to the native <input type="checkbox"> . While the indeterminate property of the checkbox is true, it will render as indeterminate regardless of the checked value. Any interaction with the checkbox by a user (i.e., clicking) will remove the indeterminate state.

How do I uncheck mat-checkbox in angular 6?

Angular Material <mat-checkbox> has a property checked that is used to check and uncheck the checkbox dynamically. At runtime we can check and uncheck checkbox by passing true and false value to checked property using property binding.

How to change color of mat-checkbox?

mat-checkbox ouput html mat-checkbox will generate a span element with the class . mat-checkbox-background through which we can control the background color of checkbox. Now all we have to do is adding background-color css to the . mat-checkbox-background class.


1 Answers

You have to position the checkbox manually. There is no other way to line the elements. Something like align-self: center for mat-checkbox.

Please be aware that mat-form-field has some custom paddings/margins. So, to have the perfect fitment you will need to inspect the element and add a couple px at the top. Depending on font-size it differs.

Hopefully, it will help. Good luck!

like image 116
Ivan Vilch Avatar answered Nov 15 '22 06:11

Ivan Vilch