angularformsangular-materialangular-material-5

How to add space between underline classes in mat-form-field elements?


I'm trying to add multiple form fields, I wanted to add 2 form fields in a row but I'm not able to add space between form fields ( not able to separate underline)

 <li>
    <mat-form-field>
      <input matInput [(ngModel)]="name" placeholder="What's your name?">
    </mat-form-field>
    <mat-form-field>
      <input matInput [(ngModel)]="name" placeholder="hello">
    </mat-form-field>
  </li>

Stackblitz: https://stackblitz.com/edit/angular-rqczqy

I want to have space between "what's your name" and "hello"

How can I achieve this? Any help would be greatly appreciated!


Solution

  • You can solve this using flexbox. I forked your stackblitz. Here one of many solutions : Inline matInput

    Template

    <li class="mat-form-field--inline">
        <mat-form-field>
          <input matInput [(ngModel)]="name" placeholder="What's your name?">
        </mat-form-field>
        <mat-form-field>
          <input matInput [(ngModel)]="name" placeholder="hello">
        </mat-form-field>
      </li>
    

    CSS

    .mat-form-field--inline {
        display: flex;
        flex-direction: row;
        align-items: center;
    }
    
    .mat-form-field--inline .mat-form-field {
        display: inline-block;
        width: 100%;
        vertical-align: middle;
    }
    
    .mat-form-field--inline .mat-form-field:nth-child(1) {
        margin-right: 10px;
    }