如何使用 HTML5 對錶格的表頭內容進行分組?


在 HTML 表格中顯示資料時,通常需要對錶格的表頭內容進行分組,以便為顯示的資料提供更多上下文資訊。對錶頭內容進行分組可以幫助使用者更容易理解資料之間的關係,並識別模式或趨勢。在本文中,我們將討論使用 HTML5 對 HTML 表格中的表頭內容進行分組的幾種方法。

方法 1:使用 <thead> 和 <th> 元素

對 HTML 表格中的表頭內容進行分組最直接的方法是使用 <thead> 和 <th> 元素。<thead> 元素用於對錶格的表頭內容進行分組,而 <th> 元素用於定義組內的每個表頭單元格。以下是一個示例

示例

<table>
   <thead>
      <tr>
      <th colspan="2">Group 1</th>
      <th colspan="2">Group 2</th>
      </tr>
      <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      </tr>
   </thead>
   <tbody>
      <tr>
      <td>Row 1, Column 1</td>
      <td>Row 1, Column 2</td>
      <td>Row 1, Column 3</td>
      <td>Row 1, Column 4</td>
      </tr>
      <tr>
      <td>Row 2, Column 1</td>
      <td>Row 2, Column 2</td>
      <td>Row 2, Column 3</td>
      <td>Row 2, Column 4</td>
      </tr>
   </tbody>
</table>

在這個示例中,我們使用了 <thead> 元素將表頭內容分成兩個單獨的組。第一組包含兩列,第二組也包含兩列。我們還使用了 colspan 屬性來指定每個組應跨越的列數。

方法 2:使用 <colgroup> 元素

對 HTML 表格中的表頭內容進行分組的另一種方法是使用 <colgroup> 元素。<colgroup> 元素允許您將列組合在一起,並對整個組應用樣式或屬性。以下是一個示例 −

示例

<table>
   <colgroup span="2"></colgroup>
   <colgroup span="2"></colgroup>
   <thead>
      <tr>
         <th>Column 1</th>
         <th>Column 2</th>
         <th>Column 3</th>
         <th>Column 4</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Row 1, Column 1</td>
         <td>Row 1, Column 2</td>
         <td>Row 1, Column 3</td>
         <td>Row 1, Column 4</td>
      </tr>
      <tr>
         <td>Row 2, Column 1</td>
         <td>Row 2, Column 2</td>
         <td>Row 2, Column 3</td>
         <td>Row 2, Column 4</td>
      </tr>
   </tbody>
</table>

在這個示例中,我們使用了 <colgroup> 元素將列分成兩個單獨的組。我們使用了 span 屬性來指定每個組應跨越的列數。

方法 3:使用 CSS

您還可以使用 CSS 對 HTML 表格中的表頭內容進行分組。透過定位特定的列或行,您可以應用樣式以視覺方式將內容組合在一起。以下是一個示例

示例

<style>
   table {
      border-collapse: collapse;
   }
   th {
      background-color: #ccc;
      font-weight: bold;
      text-align: center;
   }
   th.group1 {
      background-color: #eee;
   }
   th.group2 {
      background-color: #ddd;
   }
   th.group3 {
      background-color: #ccc;
   }
</style>
<table>
   <thead>
      <tr>
         <th class="group1">Column 1</th>
         <th class="group1">Column 2</th>
         <th class="group2">Column 3</th>
         <th class="group2">Column 4</th>
         <th class="group3">Column 5</th>
         <th class="group3">Column 6</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Row 1, Column 1</td>
         <td>Row 1, Column 2</td>
         <td>Row 1, Column 3</td>
         <td>Row 1, Column 4</td>
         <td>Row 1, Column 5</td>
         <td>Row 1, Column 6</td>
      </tr>
      <tr>
         <td>Row 2, Column 1</td>
         <td>Row 2, Column 2</td>
         <td>Row 2, Column 3</td>
         <td>Row 2, Column 4</td>
         <td>Row 2, Column 5</td>
         <td>Row 2, Column 6</td>
      </tr>
   </tbody>
</table>

在這個示例中,我們使用 CSS 透過對不同列應用不同的背景顏色來對錶頭內容進行分組。我們使用了 class 屬性來定位特定的列並應用相應的樣式。

您可以使用其他 CSS 屬性(如邊框、填充、邊距、字型大小、字型粗細等)以不同的方式視覺上對錶頭內容進行分組。例如,您可以為特定的列或行新增邊框,調整字型大小或粗細,或新增填充或邊距以在組之間建立視覺分隔。

總的來說,使用 CSS 對 HTML 表格中的表頭內容進行分組在樣式和設計方面提供了更大的靈活性。您可以建立視覺上吸引人且引人入勝的表格,從而改善使用者體驗,並使內容更易於閱讀和理解。

使用 CSS 為分組的表頭新增樣式

使用上述方法之一對錶頭進行分組後,您可以使用 CSS 為這些組應用樣式。例如,您可以新增背景顏色、更改字型大小或在組周圍新增邊框以使它們脫穎而出。以下是如何將 CSS 應用於 <colgroup> 元素以設定分組表頭樣式的示例

table {
   border-collapse: collapse;
}

colgroup:first-child {
   background-color: #f5f5f5;
}

colgroup:last-child {
   background-color: #e6e6e6;
}

th {
   padding: 10px;
   border: 1px solid #ccc;
}

在這個示例中,我們使用 :first-child 和 :last-child 偽選擇器為第一組表頭添加了灰色背景顏色,為第二組添加了淺灰色顏色。我們還為 <th> 元素添加了一些填充和邊框以使它們脫穎而出。

為分組的表頭新增工具提示

如果您的表格包含大量列,使用者可能難以記住每列代表什麼。您可以為分組的表頭新增工具提示,為使用者提供有關每列的更多資訊。以下是如何為分組表頭新增工具提示的示例。

示例

<table>
   <colgroup span="2"></colgroup>
   <colgroup span="2"></colgroup>
   <thead>
      <tr>
         <th title="Group 1">Column 1</th>
         <th title="Group 1">Column 2</th>
         <th title="Group 2">Column 3</th>
         <th title="Group 2">Column 4</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Row 1, Column 1</td>
         <td>Row 1, Column 2</td>
         <td>Row 1, Column 3</td>
         <td>Row 1, Column 4</td>
      </tr>
      <tr>
         <td>Row 2, Column 1</td>
         <td>Row 2, Column 2</td>
         <td>Row 2, Column 3</td>
         <td>Row 2, Column 4</td>
      </tr>
   </tbody>
</table>

在這個示例中,我們為每個 <th> 元素添加了 title 屬性以指定工具提示文字。當用戶將滑鼠懸停在分組的表頭上方時,將顯示工具提示,提供有關該列的更多資訊。

使用 CSS 為分組的表頭新增響應式設計

設計帶有分組表頭的表格的另一個重要方面是確保它們具有響應式設計。在為 Web 設計表格時,必須考慮它們在不同螢幕尺寸和裝置上的顯示效果。您可以使用 CSS 確保帶有分組表頭的表格具有響應式設計。

使表格具有響應式設計的一種方法是使用媒體查詢在不同的螢幕尺寸下調整表格的佈局。例如,您可以使用媒體查詢調整表格表頭的字型大小、填充和邊距,以確保它們在小螢幕上易於閱讀且間距合理。

以下是如何使用媒體查詢為小螢幕調整帶有分組表頭的表格佈局的示例

table {
   width: 100%;
   border-collapse: collapse;
}
th {
   padding: 10px;
   background-color: #ddd;
   border: 1px solid #ccc;
   font-weight: bold;
   text-align: left;
}
.group1 {
   background-color: #bbb;
}
.group2 {
   background-color: #999;
}
@media (max-width: 768px) {
   th {
      padding: 5px;
      font-size: 0.9em;
   }
   .group1, .group2 {
      display: none;
   }
   .group1-toggle, .group2-toggle {
      display: block;
      padding: 5px;
      background-color: #ddd;
      border: 1px solid #ccc;
      font-weight: bold;
      text-align: center;
      cursor: pointer;
   }
   .group1-toggle i, .group2-toggle i {
      margin-left: 5px;
   }
   .group1-toggle {
      background-color: #bbb;
   }
   .group2-toggle {
      background-color: #999;
   }
   .group1-toggle + .group1-row, .group2-toggle +  .group2-row {
      display: none;
   }
}

在這個示例中,我們使用了媒體查詢來調整最大寬度為 768 畫素的螢幕上的表格表頭佈局。當螢幕小於 768 畫素時,我們將隱藏分組的表頭,並將其替換為使用者可以單擊以展開或摺疊表頭內容的切換按鈕。我們還調整了表格表頭的字型大小、填充和邊距,以確保它們在小螢幕上易於閱讀且間距合理。

結論

分組的表頭是設計易於閱讀和理解的表格的重要方面。透過將相關的列組合在一起並應用適當的樣式和屬性,您可以使表格更易於訪問和使用者友好。無論您使用 HTML、CSS 還是 JavaScript,都有多種方法可以建立既實用又美觀的帶有分組表頭的表格。

更新於: 2023-08-07

1K+ 閱讀量

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.