當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML <table>用法及代碼示例


HTML <table>標簽用於在網頁上創建表格。它有助於以有組織和表格的方式構建和顯示數據。它有助於利用諸如<tr>,th, 和<td>指定表格單元格的不同部分。

另外,我們還可以定義thead,<caption>,tfoot, 和<tbody> valign屬性為表格提供附加結構的元素。

用法

<table>
    <tr>
        <th>Course</th>
        <th>Articles</th>
    </tr>
    <tr>
        <td>HTML</td>
        <td>HTML Table</td>
    </tr>
</table>

注意:HTML <table> 標簽支持 全局屬性事件屬性.

示例 1:該示例說明了 HTML 表格的實現。

HTML


<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0"> 
    <title>HTML table tag</title> 
    <style> 
        body { 
            text-align: center; 
        } 
  
        h1 { 
            color: green; 
        } 
  
        table, 
        th, 
        td { 
            margin: auto; 
            border: 1px solid black; 
        } 
    </style> 
</head> 
  
<body> 
    <h1>GeeksforGeeks</h1> 
    <h3>HTML <table> tag</h3> 
    <table> 
        <tr> 
            <th>Name</th> 
            <th>Age</th> 
            <th>Class</th> 
        </tr> 
        <tr> 
            <td>Shivika</td> 
            <td>11</td> 
            <td>7</td> 
        </tr> 
        <tr> 
            <td>Gauri</td> 
            <td>18</td> 
            <td>12</td> 
        </tr> 
        <tr> 
            <td>Mahima</td> 
            <td>15</td> 
            <td>10</td> 
        </tr> 
    </table> 
</body> 
  
</html>

輸出:

Screenshot-2024-01-12-133107

示例 2:該示例顯示了通過使用 rowspan 和 colspan 以及背景顏色、右對齊和 border-collapse 的自定義 CSS 樣式來實現表標記。

HTML


<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0"> 
    <title>HTML table tag</title> 
    <style> 
        body { 
            text-align: center; 
        } 
  
        h1 { 
            color: green; 
        } 
  
        table, 
        td, 
        th { 
            border: 2px solid black; 
            border-collapse: collapse; 
        } 
  
        table { 
            margin: auto; 
            background-color: rgb(173, 240, 213); 
        } 
    </style> 
  
</head> 
  
<body> 
    <h1>GeeksforGeeks</h1> 
    <h3>  
        HTML table with Custom style 
        (right- align, border-collapse 
        and background-color) 
    </h3> 
    <table> 
        <tr> 
            <th rowspan="2">Name</th> 
            <th colspan="2">Details</th> 
        </tr> 
        <tr> 
            <th>Age</th> 
            <th>Class</th> 
        </tr> 
        <tr> 
            <td>Shivika</td> 
            <td>11</td> 
            <td>7</td> 
        </tr> 
        <tr> 
            <td>Gauri</td> 
            <td>18</td> 
            <td>12</td> 
        </tr> 
        <tr> 
            <td>Mahima</td> 
            <td>15</td> 
            <td>10</td> 
        </tr> 
        <tr> 
            <td>Shree</td> 
            <td>15</td> 
            <td>10</td> 
        </tr> 
        <tr> 
            <td>Kanha</td> 
            <td>15</td> 
            <td>10</td> 
        </tr> 
    </table> 
</body> 
  
</html>

輸出:

Screenshot-2024-01-12-163253

示例 3:該示例顯示了使用瀏覽器默認 CSS 的表標簽的實現。

HTML


<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" 
          content="width=device-width,initial-scale=1.0"> 
    <title>HTML table tag</title> 
    <style> 
        table { 
            display: table; 
            border-collapse: separate; 
            box-sizing: border-box; 
            text-indent: initial; 
            border-spacing: 2px; 
            border-color: gray; 
        } 
    </style> 
</head> 
  
<body> 
  
    <table> 
        <tr> 
            <th>Name</th> 
            <th>Age</th> 
            <th>Class</th> 
        </tr> 
        <tr> 
            <td>Shruti</td> 
            <td>10</td> 
            <td>6</td> 
        </tr> 
        <tr> 
            <td>Ravi</td> 
            <td>18</td> 
            <td>12</td> 
        </tr> 
        <tr> 
            <td>Ankur</td> 
            <td>15</td> 
            <td>10</td> 
        </tr> 
        <tr> 
            <td>Gopal</td> 
            <td>12</td> 
            <td>8</td> 
        </tr> 
    </table> 
  
</body> 
  
</html>

輸出:

table1

HTML DOM 對象

HTML 表可以在以下命令的幫助下訪問Table.

瀏覽器支持

  • chrome 1
  • 邊 12
  • 火狐1
  • Opera 12.1
  • 野生動物園 1


相關用法


注:本文由純淨天空篩選整理自shivanigupta18rk大神的英文原創作品 HTML <table> Tag。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。