Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

วิธีสร้างแถวและคอลัมน์ตาราง (Table Rows & Columns) ใน HTML

เพื่อสร้างแถวและคอลัมน์ตารางใน HTML ใช้แท็ก <table> ตารางประกอบด้วยแถวและคอลัมน์ ซึ่งตั้งค่าได้โดยใช้ <tr>, <th>, และ <td> elements แถวตารางถูกกำหนดโดยแท็ก <tr> สำหรับแถวและคอลัมน์ตาราง ใช้แท็ก <tr> และ <td> ตามลำดับ แท็ก <td>

จำไว้ว่า attribute ตารางหลายอย่าง เช่น align, bgcolor, border, cellpadding, cellspacing ไม่ได้รองรับโดย HTML5 ห้ามใช้ ใช้ attribute style เพื่อเพิ่ม CSS properties สำหรับเพิ่ม border ให้ตาราง เราก็ใช้แท็ก <style> เพื่อจัดรูปแบบ border ตารางเช่นกัน

วิธีสร้างแถวและคอลัมน์ตาราง (Table Rows & Columns) ใน HTML

ตัวอย่าง

<!DOCTYPE html> 
<html> 
  <head> 
    <style> 
      table, th, td { 
        border: 1px solid black; 
      } 
    </style> 
  </head> 
  <body> 
    <h2>Understanding Tables</h2> 
    <table> 
      <tr> 
        <th>Header: This is row1 column1</th> 
        <th>Header: This is row1 column2</th> 
      </tr> 
      <tr> 
        <td>This is row2 column1</td> 
        <td>This is row2 column2</td> 
      </tr> 
    </table> 
  </body> 
</html>

ผลลัพธ์

วิธีสร้างแถวและคอลัมน์ตาราง (Table Rows & Columns) ใน HTML