How to vertically align a mat-checkbox and mat-form-field in Angular Material

My goal is to display text Fields and checkboxes side by side, similar to the layout shown in this image: https://i.sstatic.net/7QyLy.png

However, I am facing an issue with aligning <mat-form-field> and <mat-checkbox> vertically. How can I resolve this alignment problem?

Below is the code snippet:

<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 attempted using <mat-form-field>, but according to Angular Material documentation, <mat-checkbox> cannot be nested within <mat-form-field>.

Answer №1

When working with checkboxes, it's important to manually position them to ensure proper alignment of elements. One way to achieve this is by using the align-self: center property for mat-checkbox.

Keep in mind that the mat-form-field component comes with custom paddings and margins. To achieve the perfect fit, you may need to adjust the positioning by inspecting the element and adding a few pixels at the top. The exact amount may vary depending on the font size being used.

I hope this advice proves helpful in your project. Best of luck!

Answer №2

This is my approach to handling the situation:

<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>

Answer №3

Discovered a clever solution by utilizing CSS and isolating the mat-checkbox text within its own label tag with a relative position

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

The following CSS snippet in the main stylesheet appears to be effective for some scenarios:

.mat-checkbox.mat-accent .mat-checkbox-inner-container {

margin: 2px 8px auto 0; }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose the option to retrieve information from the database

Within my database, there is a table named "train_information" which contains the following fields: train_id country_id user_id train_name tare_weight number_of_bogies number_of_axles wheel_diameter_min wheel_diameter_max In addition, I have developed t ...

Is there a way to include clickable links within my dropdown menu?

I'm having an issue trying to create a dropdown menu with links on my website, but it doesn't seem to be working. <!doctype html> <html> <head> <style> a.mainhrf ul.mainul { display: none; } a. ...

table with stationary header

I have been attempting to keep the header fixed so that, when scrolling down to view other data, the <th> elements always remain visible. I tried following the instructions in this post, but when adding position: block to the styles for <th> an ...

Using InnerHTML in Javascript within the Quasar/VueJS framework is unsupported

I am looking to dynamically create tables based on the items inside the 'counts' array below. The number of tables and their contents can change. Here is my divContainer, where the innerHTML will be appended: <div id="divContainer" style="pa ...

Finding the device ID of both a computer and browser in Angular 5: How can it be done?

https://i.sstatic.net/bnUK6.pngCurrently, I am actively involved in the development of a web banking application using Angular 5. One of the tasks at hand is to identify the device ID of a computer and retrieve the browser name. I attempted to tackle this ...

Bizarre Incident Management

My latest project involves a simplistic website featuring arrow images (png) positioned on the left and right sides with fixed placement, allowing users to navigate to the next page. However, an issue arises where the cursor seems unable to select the an ...

The select dropdown default choice is not appearing as expected

My template is not showing the default select option that I have set for one select element. I attempted to achieve this with the following code: <div class="select"> <select (change)="calculaMes(mesEscolhido)" [(ngModel)]="mesEscolhido" na ...

Having trouble getting CSS Image Hover to work correctly?

I'm having trouble implementing a hover effect that changes the color of an image to blue when it is hovered over by the mouse. I've defined a class for the images and styled it in my CSS, but the effect isn't working as intended. I've ...

Rearranging anchor tag order with the power of JQuery

Imagine I have five anchor tags <a id = "A" class = "Home">Home</a> <a id = "B" class = "Maps">Maps</a> <a id = "C" class = "Plans">Plans</a> <a id = "D" class = "Community">Community</a> <a id = "E" clas ...

What's the best way to insert values into data binding within a Typescript/ Angular mat Table?

Objective: Create a dynamic table using JSON data <mat-table class="mat-elevation-z8" *ngIf="carrierRates" [dataSource]="carrierRates"> <ng-container *ngFor="let columnName of columnsList" matColumn ...

Is it possible to showcase two modals on a single page, positioning one to the left and the other to the right using Bootstrap?

Can someone help me learn how to display two modals on the same screen, one on the left and one on the right? I am new to bootstrap and would appreciate some guidance! ...

Designing in Ionic 3.x: Implementing Ion-Icon with a gradient backdrop (ion-chip)

Is there a way to add a gradient background to an icon? I attempted to nest the ion-icon within an ion-chip, like so: <ion-chip class="my-chip"> <ion-icon name="basket></ion-icon> </ion-chip Then, in the .css file: ...

What is the method for placing text underneath your background image?

click here for image I am trying to set a full viewport background image by applying it to the body tag. Currently, I have successfully achieved this with the following CSS code: body { background-image: url(/picture.jpg); } However, I also want to displ ...

Error occurs when attempting to access property on type 'unknown' without apparent cause

My current project involves an Angular application where I need to fetch data from a Rest API. There's a service that successfully retrieves the data and a component designed to display this data. However, during compilation of my application, I encou ...

Scrapy specializes in crawling, not just scraping

I've been working on a Python and Scrapy project on a website, but I keep encountering this error: DEBUG: Crawled (200) <GET http://careers.kfc.com.au/apply/?postcode=2000> (referer: None) I can't figure out why it's happening. The ...

Having trouble connecting Heroku to my asset javascript and css files within my Rails application

After deploying my app on Heroku, I encountered an issue where my scripts and CSS files are not properly linked. While everything works fine locally, the same does not hold true in the production environment. This is how I am rendering my scripts and CSS ...

Troubleshooting Issue: Relative Paths Fail to Work with routerLink in Angular 6

I seem to be encountering a problem with the Angular app I'm currently developing. It appears that when using relative paths with routerLink throughout the application, it fails to work properly. There are no errors thrown and the navigation does not ...

Issue with running "ng serve" command on command prompt

Upon running the command "ng serve" in cmd for Angular2, I encountered the following errors: "unknown browser query basedir=$(dirname $(echo $0 | sed -e s,\,/,g))" After multiple attempts to resolve the issue, I found the error message: Browsersl ...

Discovering an array containing a specific value and transforming it to another array in Angular 8

I have an array called this.data which contains a list of platforms. Each platform has its own set of section lists, where a section list consists of values like sectionName, sectionid, and sectionVal. Now, my goal is to replace the old sectionList with a ...

Combining portions of objects in Angular2

I want to combine two identical type observables in Angular2 and return an observable of the same type in a function. My goal is to: transform this setup: obs1.subscribe(obs => console.log(obs)) (where I receive): { count: 10, array: <some ...