HTML 表格有許多行和列,我們可以在其中插入、排列和顯示資料。然後,該資料以表格格式顯示在網頁上。這些表格可協助我們以有序的方式呈現數據,例如顯示項目清單、顯示銷售報告等表格數據、為網頁部分建立版面等。
在本文中,我們將學習如何建立以下類型的 HTML 表格:
- 簡單表格
- 有邊框和填充的表格
- 有樣式的表格
- 有標題的表格
- 有巢狀表格的表格
- 具有列跨度和行跨度的表格
- 帶有 colgroup 的表
在 HTML 中建立表格的要點
- 文字編輯器或 HTML 編輯器: 開啟文字編輯器或 HTML 編輯器(例如 Notepad++、Sublime Text 或 Visual Studio Code)來撰寫並儲存 HTML 程式碼。我們使用 Notepad++ 作為預設編輯器,但您可以使用任何您喜歡的編輯器。
- HTML 檔案: 在 Notepad++ 中建立一個新檔案。我們將其命名為“table.html”或您喜歡的任何其他名稱,但請記住檔案名稱以“.html”結尾。您將在該文件中編寫網頁程式碼。如果您需要協助建立此文件,您可以查看我們的教學課程「用 HTML 設計網頁」。
- HTML 程式碼: 我們在本文中提供了用於建立不同類型表格的所有基本程式碼。只需將程式碼複製並貼上到您的“table.html”檔案中即可。
- 網頁瀏覽器:在「table.html」檔案中編寫 HTML 程式碼後,您需要檢視並測試您的網頁。您可以使用 Google Chrome、Mozilla Firefox 或 Microsoft Edge 等網頁瀏覽器。我們使用 Google Chrome 查看本文中所有範例的網頁,但您可以選擇您喜歡的任何瀏覽器。
HTML 中使用的標籤
在 HTML 中建立表格之前,了解用於建立和建立表格的標籤非常重要。以下是用於建立 HTML 表格的關鍵標籤:
Tag | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
定義表格及其內容。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
定義表格的標題或說明。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
將表頭內容分組到表格中。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
將正文內容分組到表格中。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
將頁腳內容分組到表格中。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
定義表格行。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
定義表格標題儲存格。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
定義表格資料/儲存格。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
為格式化目的指定表格中的一組或多列。 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
定義表中一組列的屬性。 |
element defines table headers (column labels).
element to create table cells (data).
|
Code: <title>Simple Table</title>
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:
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:
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:
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
Output: Example 5: Table with Nested TablesIn 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: Code:
Output: Example 6: Table with Col Span and Row SpanIn 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 or |
elements that you want to merge.
|
Code: <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; text-align: left; } .football { background-color: #33cccc; } .tennis { background-color: #33cccc; } .lebron { background-color: #fddb5d; } .heading { background-color: #808080; /* Grey */ color: #fff; /* White */ } </style>
Output: Example 7: Table with ColgroupIn HTML, we use the
Output: |
---|
以上是在 HTML 中建立表格的詳細內容。更多資訊請關注PHP中文網其他相關文章!

HTML代碼可以通過在線驗證器、集成工具和自動化流程來確保其清潔度。 1)使用W3CMarkupValidationService在線驗證HTML代碼。 2)在VisualStudioCode中安裝並配置HTMLHint擴展進行實時驗證。 3)利用HTMLTidy在構建流程中自動驗證和清理HTML文件。

HTML、CSS和JavaScript是構建現代網頁的核心技術:1.HTML定義網頁結構,2.CSS負責網頁外觀,3.JavaScript提供網頁動態和交互性,它們共同作用,打造出用戶體驗良好的網站。

HTML的功能是定義網頁的結構和內容,其目的在於提供一種標準化的方式來展示信息。 1)HTML通過標籤和屬性組織網頁的各個部分,如標題和段落。 2)它支持內容與表現分離,提升維護效率。 3)HTML具有可擴展性,允許自定義標籤增強SEO。

HTML的未來趨勢是語義化和Web組件,CSS的未來趨勢是CSS-in-JS和CSSHoudini,JavaScript的未來趨勢是WebAssembly和Serverless。 1.HTML的語義化提高可訪問性和SEO效果,Web組件提升開發效率但需注意瀏覽器兼容性。 2.CSS-in-JS增強樣式管理靈活性但可能增大文件體積,CSSHoudini允許直接操作CSS渲染。 3.WebAssembly優化瀏覽器應用性能但學習曲線陡,Serverless簡化開發但需優化冷啟動問題。

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

HTML的未來充滿了無限可能。 1)新功能和標準將包括更多的語義化標籤和WebComponents的普及。 2)網頁設計趨勢將繼續向響應式和無障礙設計發展。 3)性能優化將通過響應式圖片加載和延遲加載技術提升用戶體驗。

HTML、CSS和JavaScript在網頁開發中的角色分別是:HTML負責內容結構,CSS負責樣式,JavaScript負責動態行為。 1.HTML通過標籤定義網頁結構和內容,確保語義化。 2.CSS通過選擇器和屬性控製網頁樣式,使其美觀易讀。 3.JavaScript通過腳本控製網頁行為,實現動態和交互功能。

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增強WebevebDevelopment。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。