13

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>
3
  • Were you ever able to find a solution to this problem? Commented Apr 26, 2019 at 14:40
  • No, I haven't been back to revisit it. There are a couple of solutions below suggested which I will try when I get time. Please comment below them if they work for you. Commented Apr 27, 2019 at 1:34
  • Pat M's answer below worked if I added that styling to a global stylesheet, but did not work if I added that class to a component's stylesheet. I'm guessing Angular's view-encapsulation prevents the component's style from overriding the material styling. Hope this helps Commented Apr 29, 2019 at 16:52

10 Answers 10

11

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;
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Did you have to add this to a global stylesheet? I tried pasting this into the stylesheet for one of my components but it didn't work, and I would like to avoid making it a global style..
Unfortuntely, it seems that it can't be applied from within an angular framework component for example. Since the classes mat-accent & mat-checkbox-inner-container are generated by angular material (from <mat-checkbox>) after the angular component has been executed and it's style applied. I also need to use some material.scss globally for these cases.
Thanks, that's what I figured. I ended up adding it to a global sass sheet but wrapping it in my own class to avoid affecting the styles of other checkboxes throughout the app
4

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.

Comments

3

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

Comments

1

I know this is old, but below worked for me – I honestly didn't even know auto worked with vertical alignment, but I guess it does 🤷🏽‍♂️:

  .mat-checkbox-inner-container {
      margin-top: 0;
  }

Comments

1

My solution for "@angular/core": "^17.3.2"

::ng-deep {
  .mdc-form-field.mat-internal-form-field {
    align-items: flex-start;
  }

  .mdc-form-field>label {
    padding-top: 12px; /* maybe you have to adjust this for your needs */
  }
}

Setting the margin (as in the other answers here) did not work for my case. The mat-checkbox has this padding/ripple so it still would look like the box does not fit with the (first) line of the label.

The outer div has the properties display: inline-flex; align-items: center; for the checkbox itself an its label. So we simply change this behavior and add some custom padding.

Comments

1

You can also try using position absolute and moving it downwards depending on the size of the small text

 .mat-mdc-checkbox:has(.text-muted) {
    .text-muted {
      position: absolute;
      top: var(--mat-checkbox-label-text-size);
      margin-top: 16px; // this can be adjusted
    }
  }

Comments

0
<div>
  <h1>LABEL CHECKBOX</h1>
  <mat-checkbox class="mat-checkbox-layout"><strong>FINE</strong>Lorem ipsum dolor sit amet consectetur adipisicingelit. Optio vitae molestiae nemo magni odio voluptate corporis veniam sapiente earum fugiat rem,maiores et placeat qui iure, nobis ipsam sintenim.</mat-checkbox>
  <mat-checkbox class="mat-checkbox-layout"><strong>OK</strong>Lorem ipsum dolor sit amet consectetur adipisicing elit.Optio vitae molestiae nemo magni odio voluptate corporis veniam sapiente earum fugiat rem, maiores et placeat qui iure, nobis ipsam sintenim.</mat-checkbox>
</div>

And the CSS is this (I create css class identically with angular css class)

.mat-checkbox-layout {
  text-align: justify;
}

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

Comments

0
:host {
  ::ng-deep {
    .mat-checkbox-layout {
      align-items: end!important;
    }
    .mat-checkbox-label {
      white-space: normal; // wrap label text
    }
  }
}

1 Comment

Hello please add additional information as to how/why this answer helps solve the question. Please consider reviewing the answering guidelines stackoverflow.com/help/how-to-answer
0

In "@angular/material": "16.2.12", you need to use mdc format.

.mat-checkbox-inner-container does not appear to be used anymore.

Use this instead

.mdc-checkbox  {
    margin: 0px 8px auto !important;
}

Comments

-1

There is a fast solution which is not related to ng-material

you can put your "mat-checkbox" into "li" element and align your "ul" by setting the number of columns like this:

<ul style="columns:2">
    <li>
        <mat-checkbox>Entire place<br><small class="text-muted">Have a place to yourself</small></mat-checkbox>
    </li>
    <li>
        <mat-checkbox>Private room<br><small class="text-muted">Have your own room and share some common spaces</small></mat-checkbox>
    </li>
    <li>
        <mat-checkbox>Shared room<br><small class="text-muted">Stay in a shared space, like a common room</small></mat-checkbox>
    </li>
</ul>

1 Comment

That just puts it into 2 columns. I am trying to move the check box to align with the top of the text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.