แท็ก thead

แท็ก thead กำหนดส่วนบนของตาราง ใช้สำหรับจัดกลุ่มแถวในลักษณะที่ สามารถนำสไตล์ CSS ไปใช้กับกลุ่มใดกลุ่มหนึ่งได้ทันที ในหนึ่งตารางสามารถมีแท็ก thead ได้เพียงหนึ่งแท็กเท่านั้น

แท็ก thead มักใช้ร่วมกับ tbody และ tfoot เพื่อกำหนดส่วนบน ส่วนหลัก และส่วนล่างของตาราง HTML

ตัวอย่าง

มาเพิ่มสีแดงให้กับข้อความของกลุ่มแถว ที่รวมกันด้วยแท็ก thead โดยใช้คุณสมบัติ color:

html<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Surn</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>name1</td>
      <td>surn1</td>
      <td>200$</td>
    </tr>
    <tr>
      <td>name2</td>
      <td>surn2</td>
      <td>1000$</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>count: 2</td>
      <td>-</td>
      <td>total: 1200$</td>
    </tr>
  </tfoot>
</table>
cssthead {
  color: red;
}

table, td, th {
  border: 1px solid black;
}

Результат:

ตัวอย่าง

ในที่นี้ thead ไม่ได้อยู่ด้านบน และ tfoot ก็ไม่ได้อยู่ด้านล่าง อย่างไรก็ตามเบราว์เซอร์จะจัดวาง พวกมันในตำแหน่งที่ถูกต้อง:

html<table>
  <tfoot>
    <tr>
      <td>count: 2</td>
      <td>-</td>
      <td>total: 1200$</td>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>name1</td>
      <td>surn1</td>
      <td>200$</td>
    </tr>
    <tr>
      <td>name2</td>
      <td>surn2</td>
      <td>1000$</td>
    </tr>
  </tbody>
  <thead>
    <tr>
      <th>Name</th>
      <th>Surn</th>
      <th>Salary</th>
    </tr>
  </thead>
</table>
csstable, td, th {
  border: 1px solid black;
}

Результат:

ดูเพิ่มเติม

  • แท็ก tbody, tfoot,
    ซึ่งจัดกลุ่มแถวของตาราง
  • แท็ก caption,
    ซึ่งกำหนดชื่อเรื่องของตารางทั้งหมด
  • แอตทริบิวต์ rowspan และ colspan,
    ซึ่งรวมแถวและคอลัมน์ของตารางเข้าด้วยกัน