Search code examples
cssflexboxtailwind-css

Flex and Tailwind CSS rows auto stretching


I am really struggling with a two column forms. I am using flex and tailwind css. Here is the part of my html causing the trouble

                <div class="w-full flex flex-row flex-wrap">
                    <div class="w-full flex flex-row flex-wrap md:w-1/2 p-2">
                        <div class="flex flex-wrap w-full -mx-2">
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
                                    <mat-label>First Name</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('memberFirstName').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
                                    <mat-label>Last Name</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                        </div>
                        <div class="flex flex-wrap w-full -mx-2">
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
                                    <mat-label>Address1</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('address1').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
                                    <mat-label>Address2</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('address2').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                        </div>
                        <div class="flex flex-wrap w-full -mx-2">
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
                                    <mat-label>City</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('city').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
                                    <mat-label>PostCode</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('postcode').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                        </div>
                    </div>
                    <div class="w-full flex flex-row flex-wrap md:w-1/2 p-2">
                        <div class="flex flex-wrap w-full -mx-2">
                            <div class="md:w-1/2 px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
                                    <mat-label>Membership Number</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
                                    <mat-label>Joining Date</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                        </div>
                        <div class="flex flex-wrap w-full -mx-2">
                            <div class="md:w-1/2 px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
                                    <mat-label>Gender</mat-label>
                                    <mat-select>
                                        <mat-option value="select1-1">Male</mat-option>
                                        <mat-option value="select1-2">Female</mat-option>
                                    </mat-select>
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:map-pin'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('gender').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                            <div class="md:w-1/2 w-full px-2">
                                <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
                                    <mat-label>Date of Birth</mat-label>
                                    <input matInput />
                                    <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
                                    <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
                                </mat-form-field>
                            </div>
                        </div>
                    </div>
                </div>

Here what happens is on the left side there are 3 rows. But on right side there are only 2 rows. But right side adjusts its height to adjust with left side causing some misalignment. Please check the below screen shot.

I tried specifying items-start to the right column. Doesnt make any difference.

enter image description here

I need the gender and date of birth things to just align with the second row on the left. I am happy to leave that 3 row empty. If I add a third row to the right, its aligning correctly which I dont want.


Solution

  • Apply align-items: flex-start to the root element that wraps the two columns. This will mean that each column will only be as high as the content within:

    <script src="https://cdn.tailwindcss.com/3.3.5"></script>
    
    <div class="w-full flex flex-row flex-wrap items-start">
      <div class="w-full flex flex-row flex-wrap md:w-1/2 p-2">
        <div class="flex flex-wrap w-full -mx-2">
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
              <mat-label>First Name</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('memberFirstName').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
              <mat-label>Last Name</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
        </div>
        <div class="flex flex-wrap w-full -mx-2">
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
              <mat-label>Address1</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('address1').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
              <mat-label>Address2</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('address2').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
        </div>
        <div class="flex flex-wrap w-full -mx-2">
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
              <mat-label>City</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('city').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="fuse-mat-dense w-full">
              <mat-label>PostCode</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('postcode').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
        </div>
      </div>
      <div class="w-full flex flex-row flex-wrap md:w-1/2 p-2">
        <div class="flex flex-wrap w-full -mx-2">
          <div class="md:w-1/2 px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
              <mat-label>Membership Number</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
              <mat-label>Joining Date</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
        </div>
        <div class="flex flex-wrap w-full -mx-2">
          <div class="md:w-1/2 px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
              <mat-label>Gender</mat-label>
              <mat-select>
                <mat-option value="select1-1">Male</mat-option>
                <mat-option value="select1-2">Female</mat-option>
              </mat-select>
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:map-pin'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('gender').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
          <div class="md:w-1/2 w-full px-2">
            <mat-form-field [ngClass]="formFieldHelpers" class="w-full fuse-mat-dense">
              <mat-label>Date of Birth</mat-label>
              <input matInput />
              <mat-icon class="icon-size-5" matPrefix [svgIcon]="'heroicons_solid:user-circle'"></mat-icon>
              <mat-error *ngIf="membershipForm.get('memberLastName').invalid"> Required </mat-error>
            </mat-form-field>
          </div>
        </div>
      </div>
    </div>