How can I align a Material icon & header text on the same line?

c0micrage

I am working on an Angular Material web-page. I added a <mat-icon> next to the header text in <mat-table>. After I added the mat-icon, the icon and text are not perfectly aligned. The icon is not vertically centered and looks a little too high. The header text is not aligned with the other column text and looks a little low.

The closest I got was when I moved the mat-icon inside the tag with the text.

Here is my code

  <table mat-table [dataSource]="dataSource">
 
        <ng-container matColumnDef="column1">
           <th  mat-header-cell *matHeaderCellDef style="text-align: center !important" [attr.colspan]="2"><h2>Column 1</h2></th>
        </ng-container>

        <ng-container matColumnDef="column2">
          <th *matHeaderCellDef  style="text-align: center !important" [attr.colspan]="2">                       
            <h2><mat-icon class="pointer"  style="padding-bottom: 0px; padding-right: 1px; cursor: pointer;"         >arrow_left</mat-icon>Column 2</h2>
        </th>
      </ng-container>
      <tr mat-header-row *matHeaderRowDef="displayedGroupColumns; sticky: true"></tr>
      <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
    </table >

Akber Iqbal

Your attention to detail is reminiscent of a customer I have at work;

To make this happen, we use position:absolute, but enclose it inside a div with position:relative so that we don't fall off the grid

relevant css:

.myCustomHeading{position:relative;padding-top:4px;}
.myIcon{position:relative;}
.mySpan{position:absolute;padding-top:2px;}

relevant HTML:

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> <h2>No.</h2> </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef>
      <div class='myCustomHeading'>  
        <h2><mat-icon class="pointer myIcon" >arrow_left</mat-icon>
        <span class="mySpan">Column 2</span></h2>
      </div>
    </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

complete working stackblitz available here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I keep text on the same line as an icon?

How do I align text in the same line?

align material icon with the text

How may I align text to the left and text to the right in the same line?

How do I make the google material icon height the same as the text?

How I can align icon next to text of MaterialButton with defined padding?

How can I align a text with a font awesome 5 icon?

CSS - How to align text and icon in one line

How I can align the header text of the ExpandableListview to the extreme left?

How do I align a text on the same line as a button?

how do i align my th text in a same line

How do I align text and buttons on the same line?

How can I align span and img elements along the same line?

How can I align three elements in a legend tag in the same line?

Align material icon with text on Materialize

How can I align text under a <hr> line properly?

How to align text on the same line in flexbox?

React - How to align text and img in same line

Material UI Autocomplete - How do I add the same icon + text to the Textfield after I have selected

How to display text and icon on the same line

How to put the icon and the text in the same line

How can I put regular text and animated text on the same line?

How to align search icon and autocomplete component on same line

how to align heading in center and icon in right on the same line and is responsive

How can I use text-align-last except when I have only one line of text?

How can I align these 2 elements so that the text is vertically centered to the icon?

How to align the header and button on the same line using bootstrap 4

(bootstrap) How to align navigation on the same line as the header (logo)

How do I align the text in line with the picture?