md-list-icon or md-button aligned to right? (Angular Material 2)

18,411

Solution 1

For what is described above the solution is pretty simple, simply omit mat-list-icon in your selector and Material will do the magic:

<mat-list>
  <mat-list-item *ngFor="let position of cart">
    <h3 mat-line> {{ position.name }} </h3>
    <p mat-line>
      <span> Line 1 </span>
    </p>
    <div> $2.00 </div>
    <button mat-icon-button (click)="remove(1)">
      <mat-icon class="mat-24">delete</mat-icon>
    </button>
  </mat-list-item>
</mat-list>

enter image description here

Solution 2

Actually, mat-list uses flex box. You can individually order the item using the CSS property 'order'. Just use a large enough number for the order

md-icon[md-list-icon]{
  order: 10;
}
<md-list>
  <md-list-item *ngFor="let position of cart">
    <h3 md-line> {{ position.name }} </h3>
    <p md-line>
      <span> Line 1 </span>
    </p>
    <md-icon md-list-icon>delete</md-icon>
  </md-list-item>
</md-list>

Solution 3

If anyone like me had this same problem and the suggested solutions didn't work out try this:

.icon-class {
    margin-left: auto;
}
<mat-list>
   <mat-list-item>
      {{"Im just a filler"}}
      <mat-icon class="icon-class">
          fiber_manual_record
      </mat-icon>
      <mat-divider></mat-divider>
    </mat-list-item>
<mat-list>

It worked for me, and when it did I got mad at myself for not figuring it out before

Share:
18,411

Related videos on Youtube

srokatonie
Author by

srokatonie

Symfony 2 enthusiast and developer.

Updated on September 16, 2022

Comments

  • srokatonie
    srokatonie over 1 year

    Any way how to make buttons/icons/checkboxes aligned to the right like in Material 1:

    Angular Material 1 list with actions https://material.angularjs.org/latest/demo/list

    I currently (Material 2) have:

    <md-list>
      <md-list-item *ngFor="let position of cart">
        <h3 md-line> {{ position.name }} </h3>
        <p md-line>
          <span> Line 1 </span>
        </p>
        <md-icon md-list-icon>delete</md-icon>
      </md-list-item>
    </md-list>
    

    Angular Material 2 list with actions

    • Cody Gray
      Cody Gray about 7 years
      Umm, doesn't Angular still use CSS for customization? That would mean this is a simple float: right;
    • srokatonie
      srokatonie about 7 years
      float:right is not working... After playing with it for a while I came up with a solution, I'll post it in a moment
  • Martin Schneider
    Martin Schneider almost 7 years
    you meant to omit md-list-icon!? ;)
  • Jules
    Jules about 6 years
    That's a terrible solution. Note that if you're using multiple icons (like in the example image shown) the icons will end up in reverse order. Also, screen readers will likely read out the controls in the wrong order. Don't do this. dir="rtl" is designed for cases when the content will be interpreted as the item on the right being logically first, not for visually rearranging things.
  • Saqib S
    Saqib S almost 6 years
    I agree with Jules. This is not the right solution for this kind of problem.
  • Miki
    Miki over 4 years
    @srokatonie mind to accept the answer? I had to scan all the other answers just in case.