HTML 表格有許多行和列,我們可以在其中插入、排列和顯示資料。然後,該資料以表格格式顯示在網頁上。這些表格可協助我們以有序的方式呈現數據,例如顯示項目清單、顯示銷售報告等表格數據、為網頁部分建立版面等。
在本文中,我們將學習如何建立以下類型的 HTML 表格:
在 HTML 中建立表格之前,了解用於建立和建立表格的標籤非常重要。以下是用於建立 HTML 表格的關鍵標籤:
Tag | Description | |||||||||||||||||||||
|
描述 | |||||||||||||||||||||
定義表格及其內容。 | ||||||||||||||||||||||
定義表格的標題或說明。 | ||||||||||||||||||||||
將表頭內容分組到表格中。 | ||||||||||||||||||||||
將正文內容分組到表格中。 | ||||||||||||||||||||||
將頁腳內容分組到表格中。 | ||||||||||||||||||||||
定義表格行。 | ||||||||||||||||||||||
定義表格標題儲存格。 | ||||||||||||||||||||||
定義表格資料/儲存格。 | ||||||||||||||||||||||
為格式化目的指定表格中的一組或多列。 | ||||||||||||||||||||||
定義表中一組列的屬性。 |
element defines table headers (column labels).
element to create table cells (data).
| Code: <!DOCTYPE html> <html> <head> <title>Simple Table</title> </head> <body> <table> <tr> <th>Product</th> <th>Price</th> </tr> <tr> <td>Apple</td> <td>$20</td> </tr> </table> </body> </html> Output: To add an additional column to the table in the example, you can use the element within your table’s column. This element is used to define header cells for the column.
| And if you want to add a new row to the table, you can use the element. This element is used to define regular cells within the table row.
| Code: <table> <tr> <th>Product</th> <th>Price</th> <th>Quantity</th> </tr> <tr> <td>Apple</td> <td>$20</td> <td>10</td> </tr> <tr> <td>Orange</td> <td>$10</td> <td>15</td> </tr> </table> Output: Let’s see how to add borders to an HTML table. This is a way to visually separate the different sections of the table and make it easier to read and understand. Example 2: Table with Borders and PaddingIn this example, we will add a table element and set the border and cellpadding attribute. We will use the border attribute and set the width of the table’s border to 1 pixel. For the cellpadding attribute, we will use 5 pixels of padding for the content inside each cell. Code: <table border="1" cellpadding="5"> <tr> <th>Name</th> <th>Age</th> <th>Country</th> </tr> <tr> <td>Michael</td> <td>27</td> <td>Alaska</td> </tr> <tr> <td>Evelyn</td> <td>32</td> <td>Ohio</td> </tr> </table> Output: Example 3: Table with StylingIf you want to improve the appearance of your table, you can use CSS (Cascading Style Sheets) to add various styles and formatting. One way to enhance the table is by giving different cells a background color. To do this, you can simply add the style attribute with the background-color property inside the opening Code: <table> <tr> <th style="background-color: #33cccc;">Country</th> <th style="background-color: #33cccc;">Population</th> <th style="background-color: #33cccc;">Capital</th> </tr> <tr> <td>Spain</td> <td>47 Million</td> <td>Madrid</td> </tr> <tr> <td>Finland</td> <td>5.5 Million</td> <td>Helsinki</td> </tr> </table> Output: Example 4: Table with CaptionUsing an HTML table with a caption is a great way to present information on a webpage in a tidy and organized manner. It’s like giving your table a title or a brief description to help people grasp its content easily. To include a caption, all you have to do is use the
|
---|