Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a multiline checkbox in Material (angular)

I have a checkbox I want to have a label with a multiline. How can you break the label of this?

 <mat-checkbox>a somewhat longer label!</mat-checkbox>

I tried with css

mat-checkbox span { white-space: pre-wrap; }

but that looks weird

like image 419
rst Avatar asked Dec 22 '25 15:12

rst


2 Answers

There is an existing issue about this on github, but here is a workaround.

Update: From Angular 15 and on:

To style the checkbox for the current component, just add the following to the component.css file:

:host::ng-deep.mat-mdc-checkbox .mdc-checkbox {
  margin-top: -0.5em !important;
}
:host::ng-deep.mat-mdc-checkbox .mdc-form-field {
  align-items: start !important;
}

If you want to apply it to all checkboxes of the entire app. Add the following to styles.css:

.mat-mdc-checkbox .mdc-checkbox {
  margin-top: -0.5em !important;
}
.mat-mdc-checkbox .mdc-form-field {
  align-items: start !important;
}

Prior to Angular version 15:

To style the checkbox for the current component, just add the following to the component.css file:

:host::ng-deep.mat-checkbox-layout{
white-space: normal !important;
}

:host::ng-deep.mat-checkbox-inner-container{
    margin-top: 4px;
}

If you want to apply it to all checkboxes of the entire app. Add the following to styles.css:

.mat-checkbox-layout{
white-space: normal !important;

}
.mat-checkbox-inner-container{
    margin-top: 4px;
}
like image 104
meblum Avatar answered Dec 24 '25 05:12

meblum


Follow the example, bootstrap text-wrap class used

<mat-checkbox>
   <div class="text-wrap">
    a somewhat longer label!
   </div>
 </mat-checkbox>
like image 22
Rodrigo Assunção Batista Avatar answered Dec 24 '25 03:12

Rodrigo Assunção Batista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!