Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material: mat-checkbox vertical alignment of checkbox

I can't seem to override the defaults to vertically align the checkbox relative to the checkbox label. Searched for a while but nothing has worked. See example image:

enter image description here

Should be like:

enter image description here

Have tried the following:

.mat-checkbox-inner-container {
    margin: none;
}

HTML:

<label><b>Home Type</b></label>
    <mat-checkbox>Entire place<br><small class="text-muted">Have a place to yourself</small></mat-checkbox>
    <mat-checkbox>Private room<br><small class="text-muted">Have your own room and share some common spaces</small></mat-checkbox>
    <mat-checkbox>Shared room<br><small class="text-muted">Stay in a shared space, like a common room</small></mat-checkbox>
like image 865
MadMac Avatar asked Nov 28 '18 02:11

MadMac


3 Answers

Similar to Craig's answer but as a descendant of .mat-checkbox.mat-accent to spare the need of using "!important"

.mat-checkbox.mat-accent .mat-checkbox-inner-container {
    margin: 2px 8px auto 0;
}

or using sass/less

.mat-checkbox.mat-accent {
   .mat-checkbox-inner-container {
      margin: 2px 8px auto 0;
   }
}
like image 74
Pat M Avatar answered Oct 28 '22 10:10

Pat M


Just run into the same problem. I used the following to solve it (unfortunately needs the dreaded !important):

.mat-checkbox-inner-container {
  margin: 2px 8px auto 0 !important;
}

I found I had to adjust the top margin (2px in this case) to get the layout beside the label to look correct.

like image 22
Craig Avatar answered Oct 28 '22 11:10

Craig


I was able to achieve this (I'm using Angular/Material 7.x) by resetting the margins the checkbox container uses.

Note that I have had to apply an additional translateY to account for the box's border-width, and reset the wrap on the label so the text doesn't try to stay on one line.

your.component.scss

:host {
  ::ng-deep {
    .mat-checkbox-inner-container {
      margin-top: initial;
      margin-bottom: initial;
      transform: translateY(2px); // value of checkbox border-width
    }
    .mat-checkbox-label {
      white-space: normal; // wrap label text
    }
  }
}

StackBlitz Fiddle

like image 3
1252748 Avatar answered Oct 28 '22 09:10

1252748