HTML <td>แท็ก
ตัวอย่าง
ตาราง HTML อย่างง่าย ที่มีสองแถวและสี่เซลล์ตาราง:
<table>
<tr>
<td>Cell A</td>
<td>Cell B</td>
</tr>
<tr>
<td>Cell C</td>
<td>Cell D</td>
</tr>
</table>
ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
แท็ ก<td>
กำหนดเซลล์ข้อมูลมาตรฐานในตาราง HTML
ตาราง HTML มีเซลล์สองประเภท:
- เซลล์ส่วนหัว - มีข้อมูลส่วนหัว (สร้างด้วย องค์ประกอบ <th> )
- เซลล์ข้อมูล - มีข้อมูล (สร้างด้วย
<td>
องค์ประกอบ)
ข้อความใน<td>
องค์ประกอบเป็นแบบปกติและจัดชิดซ้ายโดยค่าเริ่มต้น
ข้อความในองค์ประกอบ <th> เป็นตัวหนาและอยู่กึ่งกลางโดยค่าเริ่มต้น
รองรับเบราว์เซอร์
Element | |||||
---|---|---|---|---|---|
<td> | Yes | Yes | Yes | Yes | Yes |
คุณลักษณะ
Attribute | Value | Description |
---|---|---|
colspan | number | Specifies the number of columns a cell should span |
headers | header_id | Specifies one or more header cells a cell is related to |
rowspan | number | Sets the number of rows a cell should span |
คุณสมบัติสากล
แท็ ก<td>
ยังสนับสนุน Global Attributes ในHTML
คุณสมบัติของเหตุการณ์
แท็ ก<td>
ยังสนับสนุนแอตทริบิวต์เหตุการณ์ใน HTML
ตัวอย่างเพิ่มเติม
ตัวอย่าง
วิธีจัดแนวเนื้อหาภายใน <td> (ด้วย CSS):
<table style="width:100%">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td
style="text-align:right">$100</td>
</tr>
<tr>
<td>February</td>
<td
style="text-align:right">$80</td>
</tr>
</table>
ตัวอย่าง
วิธีเพิ่มสีพื้นหลังให้กับเซลล์ตาราง (ด้วย CSS):
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td
style="background-color:#FF0000">January</td>
<td
style="background-color:#00FF00">$100</td>
</tr>
</table>
ตัวอย่าง
วิธีกำหนดความสูงของเซลล์ตาราง (ด้วย CSS):
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td
style="height:100px">January</td>
<td
style="height:100px">$100</td>
</tr>
</table>
ตัวอย่าง
วิธีระบุไม่ให้ตัดคำในเซลล์ตาราง (ด้วย CSS):
<table>
<tr>
<th>Poem</th>
</tr>
<tr>
<td
style="white-space:nowrap">Never increase, beyond what is necessary, the
number of entities required to explain anything</td>
</tr>
</table>
ตัวอย่าง
วิธีจัดแนวเนื้อหาในแนวตั้งภายใน <td> (ด้วย CSS):
<table style="width:50%;">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr style="height:100px">
<td
style="vertical-align:bottom">January</td>
<td
style="vertical-align:bottom">$100</td>
</tr>
</table>
ตัวอย่าง
วิธีกำหนดความกว้างของเซลล์ตาราง (ด้วย CSS):
<table style="width:100%">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td style="width:70%">January</td>
<td style="width:30%">$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: TableData Object
กวดวิชา CSS: ตารางการจัดสไตล์
การตั้งค่า CSS เริ่มต้น
เบราว์เซอร์ส่วนใหญ่จะแสดง<td>
องค์ประกอบด้วยค่าเริ่มต้นดังต่อไปนี้:
td {
display: table-cell;
vertical-align: inherit;
}