搜尋
首頁web前端html教學在 HTML 中建立表格

在 HTML 中建立表格

Sep 04, 2024 pm 04:37 PM
htmlhtml5HTML TutorialHTML PropertiesHTML tags

HTML 表格有許多行和列,我們可以在其中插入、排列和顯示資料。然後,該資料以表格格式顯示在網頁上。這些表格可協助我們以有序的方式呈現數據,例如顯示項目清單、顯示銷售報告等表格數據、為網頁部分建立版面等。

在本文中,我們將學習如何建立以下類型的 HTML 表格:

  • 簡單表格
  • 有邊框和填充的表格
  • 有樣式的表格
  • 有標題的表格
  • 有巢狀表格的表格
  • 具有列跨度和行跨度的表格
  • 帶有 colgroup 的表

在 HTML 中建立表格的要點

  1. 文字編輯器或 HTML 編輯器: 開啟文字編輯器或 HTML 編輯器(例如 Notepad++、Sublime Text 或 Visual Studio Code)來撰寫並儲存 HTML 程式碼。我們使用 Notepad++ 作為預設編輯器,但您可以使用任何您喜歡的編輯器。
  2. HTML 檔案: 在 Notepad++ 中建立一個新檔案。我們將其命名為“table.html”或您喜歡的任何其他名稱,但請記住檔案名稱以“.html”結尾。您將在該文件中編寫網頁程式碼。如果您需要協助建立此文件,您可以查看我們的教學課程「用 HTML 設計網頁」。
  3. HTML 程式碼: 我們在本文中提供了用於建立不同類型表格的所有基本程式碼。只需將程式碼複製並貼上到您的“table.html”檔案中即可。
  4. 網頁瀏覽器:在「table.html」檔案中編寫 HTML 程式碼後,您需要檢視並測試您的網頁。您可以使用 Google Chrome、Mozilla Firefox 或 Microsoft Edge 等網頁瀏覽器。我們使用 Google Chrome 查看本文中所有範例的網頁,但您可以選擇您喜歡的任何瀏覽器。

HTML 中使用的標籤

在 HTML 中建立表格之前,了解用於建立和建立表格的標籤非常重要。以下是用於建立 HTML 表格的關鍵標籤:

表>

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:

  1. Open an HTML file in a text or HTML editor.
  2. Add the
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.
標籤
描述
定義表格及其內容。
定義表格的標題或說明。
將表頭內容分組到表格中。
將正文內容分組到表格中。
將頁腳內容分組到表格中。
定義表格行。
定義表格標題儲存格。
定義表格資料/儲存格。
為格式化目的指定表格中的一組或多列。
定義表中一組列的屬性。
element to define the table.
  • Use the
  • element to create table rows.
  • The
  • tag. For instance, you can use style=”background-color: #33cccc;” to set the background color to a nice shade of teal.

    Code:

    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.
  • Code:

    
    
    <title>Simple Table</title>
    
    
    
    Product Price
    Apple $20

    Output:
    The resultant table for product and price will be displayed as seen below:
    在 HTML 中建立表格

    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:

    
    
    Product Price Quantity
    Apple $20 10
    Orange $10 15

    Output:
    在 HTML 中建立表格

    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.

    Code:

    
    
    Name Age Country
    Michael 27 Alaska
    Evelyn 32 Ohio

    Output:
    在 HTML 中建立表格

    Example 3: Table with Styling

    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

    Country Population Capital
    Spain 47 Million Madrid
    Finland 5.5 Million Helsinki

    Output:
    在 HTML 中建立表格

    Example 4: Table with Caption

    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

    tag and place it right below the tag.

    Code:

    <style>
    table {
    border-collapse: collapse;
    }
    th, td {
    border: 1px solid black;
    padding: 8px;
    }
    caption {
    background-color: #33cccc;
    padding: 8px;
    font-weight: bold;
    }
    </style>
    
    Employee Information
    Name Position Salary
    Margot Mitchell Manager $60,000
    Ryan Arnett Developer $50,000

    Output:
    在 HTML 中建立表格

    Example 5: Table with Nested Tables

    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:

    Code:

    
    
    Device Brand Specifications
    Smartphone Apple
    Model Storage
    iPhone 12 Pro 256GB
    iPhone SE 128GB
    Laptop HP 15.6" Display
    Tablet Samsung 10.5" Display

    Output:
    在 HTML 中建立表格

    Example 6: Table with Col Span and Row Span

    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

    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>
    
    
    
    Sport Player
    Football Lionel Messi Cristiano Ronaldo
    Neymar Harry Kane
    Tennis Novak Djokovic Rafael Nadal
    Serena Williams Naomi Osaka
    Basketball LeBron James

    Output: 
    在 HTML 中建立表格

    Example 7: Table with Colgroup

    In HTML, we use the

    element to group columns in an HTML table. We can also use the tag within the tag to set a background color for specific columns. To implement this, you can simply add a right after the opening tag.

    Code:

    
    
    <style>
    table {
    border-collapse: collapse;
    }
    th, td {
    border: 1px solid black;
    padding: 8px;
    }
    </style>
    
    
    
    City Country Continent
    New York United States North America
    London United Kingdom Europe
    Tokyo Japan Asia

    Output: 
    在 HTML 中建立表格

    以上是在 HTML 中建立表格的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    如何驗證您的HTML代碼?如何驗證您的HTML代碼?Apr 24, 2025 am 12:04 AM

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

    HTML與CSS和JavaScript:比較Web技術HTML與CSS和JavaScript:比較Web技術Apr 23, 2025 am 12:05 AM

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

    HTML作為標記語言:其功能和目的HTML作為標記語言:其功能和目的Apr 22, 2025 am 12:02 AM

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

    HTML,CSS和JavaScript的未來:網絡開發趨勢HTML,CSS和JavaScript的未來:網絡開發趨勢Apr 19, 2025 am 12:02 AM

    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:行為HTML:結構,CSS:樣式,JavaScript:行為Apr 18, 2025 am 12:09 AM

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

    HTML的未來:網絡設計的發展和趨勢HTML的未來:網絡設計的發展和趨勢Apr 17, 2025 am 12:12 AM

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

    HTML與CSS vs. JavaScript:比較概述HTML與CSS vs. JavaScript:比較概述Apr 16, 2025 am 12:04 AM

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

    HTML:是編程語言還是其他?HTML:是編程語言還是其他?Apr 15, 2025 am 12:13 AM

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

    See all articles

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

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

    AI Clothes Remover

    AI Clothes Remover

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

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

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

    熱工具

    禪工作室 13.0.1

    禪工作室 13.0.1

    強大的PHP整合開發環境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網頁開發工具

    EditPlus 中文破解版

    EditPlus 中文破解版

    體積小,語法高亮,不支援程式碼提示功能

    SublimeText3 英文版

    SublimeText3 英文版

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

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

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