ข้อมูลอ้างอิงHTML

HTML โดยตัวอักษร HTML ตามหมวดหมู่ รองรับ HTML Browser แอตทริบิวต์ HTML HTML Global Attributes เหตุการณ์ HTML HTML สี HTML Canvas HTML เสียง/วิดีโอ ชุดอักขระ HTML HTML Doctypes การเข้ารหัส URL HTML รหัสภาษา HTML รหัสประเทศ HTML ข้อความ HTTP วิธี HTTP ตัวแปลง PX เป็น EM แป้นพิมพ์ลัด


HTML <th>แท็ก


ตัวอย่าง

ตาราง HTML อย่างง่ายที่มีสามแถว เซลล์ส่วนหัวสองเซลล์ และเซลล์ข้อมูลสี่เซลล์:

<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง


ความหมายและการใช้งาน

แท็ ก<th>กำหนดเซลล์ส่วนหัวในตาราง HTML

ตาราง HTML มีเซลล์สองประเภท:

  • เซลล์ส่วนหัว - มีข้อมูลส่วนหัว (สร้างด้วย <th>องค์ประกอบ)
  • เซลล์ข้อมูล - มีข้อมูล (สร้างด้วย องค์ประกอบ <td> )

ข้อความใน<th>องค์ประกอบจะเป็นตัวหนาและอยู่กึ่งกลางโดยค่าเริ่มต้น

ข้อความในองค์ประกอบ <td> เป็นแบบปกติและจัดชิดซ้ายโดยค่าเริ่มต้น


รองรับเบราว์เซอร์

Element
<th> Yes Yes Yes Yes Yes

คุณลักษณะ

Attribute Value Description
abbr text Specifies an abbreviated version of the content in a header cell
colspan number Specifies the number of columns a header cell should span
headers header_id Specifies one or more header cells a cell is related to
rowspan number Specifies the number of rows a header cell should span
scope col
colgroup
row
rowgroup
Specifies whether a header cell is a header for a column, row, or group of columns or rows

คุณสมบัติสากล

แท็ ก<th>ยังสนับสนุน Global Attributes ในHTML


คุณสมบัติของเหตุการณ์

แท็ ก<th>ยังสนับสนุนแอตทริบิวต์เหตุการณ์ใน HTML



ตัวอย่างเพิ่มเติม

ตัวอย่าง

วิธีจัดแนวเนื้อหาภายใน <th> (ด้วย CSS):

<table style="width:100%">
  <tr>
    <th style="text-align:left">Month</th>
    <th style="text-align:left">Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

ตัวอย่าง

วิธีเพิ่มสีพื้นหลังให้กับเซลล์ส่วนหัวของตาราง (ด้วย CSS):

<table>
  <tr>
    <th style="background-color:#FF0000">Month</th>
    <th style="background-color:#00FF00">Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
 </table>

ตัวอย่าง

วิธีกำหนดความสูงของเซลล์ส่วนหัวของตาราง (ด้วย CSS):

<table>
  <tr>
    <th style="height:100px">Month</th>
    <th style="height:100px">Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

ตัวอย่าง

วิธีระบุไม่ให้ตัดคำในเซลล์ส่วนหัวของตาราง (ด้วย CSS):

<table>
  <tr>
    <th>Month</th>
    <th style="white-space:nowrap">My Savings for a new car</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

ตัวอย่าง

วิธีจัดแนวเนื้อหาในแนวตั้งภายใน <th> (ด้วย CSS):

<table style="width:50%;">
  <tr style="height:100px">
    <th style="vertical-align:bottom">Month</th>
    <th style="vertical-align:bottom">Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

ตัวอย่าง

วิธีกำหนดความกว้างของเซลล์ส่วนหัวของตาราง (ด้วย CSS):

<table style="width:100%">
  <tr>
    <th style="width:70%">Month</th>
    <th style="width:30%">Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
</table>

ตัวอย่าง

วิธีสร้างส่วนหัวของตาราง:

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Phone</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>[email protected]</td>
    <td>123-45-678</td>
  </tr>
</table>

ตัวอย่าง

วิธีสร้างตารางพร้อมคำบรรยาย:

<table>
  <caption>Monthly savings</caption>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

ตัวอย่าง

วิธีกำหนดเซลล์ตารางที่มีมากกว่าหนึ่งแถวหรือหนึ่งคอลัมน์:

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th colspan="2">Phone</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>[email protected]</td>
    <td>123-45-678</td>
    <td>212-00-546</td>
  </tr>
</table>

หน้าที่เกี่ยวข้อง

กวดวิชา HTML: ตาราง HTML

การอ้างอิง HTML DOM: TableHeader Object

กวดวิชา CSS: ตารางการจัดสไตล์


การตั้งค่า CSS เริ่มต้น

เบราว์เซอร์ส่วนใหญ่จะแสดง<th>องค์ประกอบด้วยค่าเริ่มต้นดังต่อไปนี้:

th {
  display: table-cell;
  vertical-align: inherit;
  font-weight: bold;
  text-align: center;
}