HTML 表格有許多行和列,我們可以在其中插入、排列和顯示資料。然後,該資料以表格格式顯示在網(wǎng)頁(yè)上。這些表格可協(xié)助我們以有序的方式呈現(xiàn)數(shù)據(jù),例如顯示項(xiàng)目清單、顯示銷(xiāo)售報(bào)告等表格數(shù)據(jù)、為網(wǎng)頁(yè)部分建立版面等。
在本文中,我們將學(xué)習(xí)如何建立以下類(lèi)型的 HTML 表格:
簡(jiǎn)單表格
有邊框和填充的表格
有樣式的表格
有標(biāo)題的表格
有巢狀表格的表格
具有列跨度和行跨度的表格
帶有 colgroup 的表
在 HTML 中建立表格的要點(diǎn)
文字編輯器或 HTML 編輯器: 開(kāi)啟文字編輯器或 HTML 編輯器(例如 Notepad++、Sublime Text 或 Visual Studio Code)來(lái)撰寫(xiě)並儲(chǔ)存 HTML 程式碼。我們使用 Notepad++ 作為預(yù)設(shè)編輯器,但您可以使用任何您喜歡的編輯器。
HTML 檔案: 在 Notepad++ 中建立一個(gè)新檔案。我們將其命名為“table.html”或您喜歡的任何其他名稱(chēng),但請(qǐng)記住檔案名稱(chēng)以“.html”結(jié)尾。您將在該文件中編寫(xiě)網(wǎng)頁(yè)程式碼。如果您需要協(xié)助建立此文件,您可以查看我們的教學(xué)課程「用 HTML 設(shè)計(jì)網(wǎng)頁(yè)」。
HTML 程式碼: 我們?cè)诒疚闹刑峁┝擞渺督⒉煌?lèi)型表格的所有基本程式碼。只需將程式碼複製並貼上到您的“table.html”檔案中即可。
網(wǎng)頁(yè)瀏覽器:在「table.html」檔案中編寫(xiě) HTML 程式碼後,您需要檢視並測(cè)試您的網(wǎng)頁(yè)。您可以使用 Google Chrome、Mozilla Firefox 或 Microsoft Edge 等網(wǎng)頁(yè)瀏覽器。我們使用 Google Chrome 查看本文中所有範(fàn)例的網(wǎng)頁(yè),但您可以選擇您喜歡的任何瀏覽器。
HTML 中使用的標(biāo)籤
在 HTML 中建立表格之前,了解用於建立和建立表格的標(biāo)籤非常重要。以下是用於建立 HTML 表格的關(guān)鍵標(biāo)籤:
Tag
Description
Defines a table and its content.
Defines a title or caption for a table.
Groups the header content in a table.
Groups the body content in a table.
Groups the footer content in a table.
Defines a table row.
Defines a table header cell.
Defines a table data/cell.
Specifies a set of one or more columns in a table for the purpose of formatting.
Defines the attributes for a group of columns in a table.
標(biāo)籤
描述
定義表格及其內(nèi)容。
定義表格的標(biāo)題或說(shuō)明。
將表頭內(nèi)容分組到表格中。
將正文內(nèi)容分組到表格中。
將頁(yè)腳內(nèi)容分組到表格中。
定義表格行。
定義表格標(biāo)題儲(chǔ)存格。
定義表格資料/儲(chǔ)存格。
為格式化目的指定表格中的一組或多列。
定義表中一組列的屬性。
表>
Examples of Tables in HTML
Example 1: Simple Table
Let’s create a basic HTML table that showcases product information. We will include two columns labeled “Product” and “Price.” The table will contain a single row displaying data for the product “Apple” with a price of $20.
To create a simple HTML table:
Open an HTML file in a text or HTML editor.
Add the
element to define the table.
Use the
element to create table rows.
The
element defines table headers (column labels).
Use the
element to create table cells (data).
Insert the desired data within the table cells.
Save the file with the .html extension, and then open it in a web browser to view the table.
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 Padding
In 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.
If 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
tag. For instance, you can use style=”background-color: #33cccc;” to set the background color to a nice shade of teal.
Using 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
In HTML, when we talk about a nested table, it means we have a table placed inside another table. So, basically, some cells in the outer table contain a whole new table structure within them. If you want to include a nested table, you just need to insert another table inside any cell of your main table. To understand better, here is an example:
In HTML, the “colspan” and “rowspan” give you the power to merge or split cells horizontally (colspan) and vertically (rowspan) to create more advanced table structures.
If you want to merge cells horizontally, simply use “colspan” followed by the number of cells you want to merge. And if you want to merge cells vertically, you can use “rowspan” along with the number of cells you want to merge.
To use the colspan and rowspan attributes, you can add them directly within the