ค้นหาเว็บไซต์

html - ฉันจะจัดแนวไอคอน Material และข้อความหัวข้อให้อยู่ในบรรทัดเดียวกันได้อย่างไร?


ฉันกำลังทำงานบนเว็บเพจ Angular Material ฉันเพิ่ม <mat-icon> ข้างข้อความหัวเรื่องใน <mat-table> หลังจากที่ฉันเพิ่ม mat-icon ไอคอนและข้อความไม่ได้จัดเรียงอย่างสมบูรณ์ ไอคอนไม่ได้อยู่ตรงกลางแนวตั้งและดูสูงไปเล็กน้อย ข้อความหัวเรื่องไม่ได้จัดเรียงกับข้อความในคอลัมน์อื่น ๆ และดูต่ำไปเล็กน้อย

สิ่งที่ใกล้เคียงที่สุดที่ฉันทำได้คือเมื่อฉันย้ายไอคอนแผ่นลงไปข้างในแท็ก พร้อมกับข้อความ

นี่คือโค้ดของฉัน

  <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 >

คำตอบ #1

ความใส่ใจในรายละเอียดของคุณทำให้นึกถึงลูกค้าคนหนึ่งที่ฉันมีที่ทำงาน

เพื่อให้สิ่งนี้เกิดขึ้น เราใช้ position:absolute แต่ห่อมันภายใน div ที่มี position:relative เพื่อว่าเราจะไม่หลุดออกจากกริด

css ที่เกี่ยวข้อง:

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

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>

มี stackblitz ทำงานสมบูรณ์ให้ใช้งานที่นี่

บทความที่เกี่ยวข้อง