DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML和Xhtml
HTML table 直列化格式詳解:colgroup 與 col 的用法
編輯:HTML和Xhtml     

基本語法

<colgroup>...</colgroup>

可用屬性

屬性值說明
alignleft / center / right靠左、居中、靠右
valigntop / middle / bottom靠上、居中、靠下
span數字涵蓋的直列數
bgcolor顏色值該直列的背景色

單獨設定某一列:col

<col> 的功能與 colgroup 幾乎相同,但更適合一次只針對單一列的情境。

特別提醒:若文件第一行把 DOCTYPE 宣告為 XHTML 1.0 Transitional,colgroup 的樣式可能會失效,除錯時記得先檢查這裡。

完整範例

<!DOCTYPE html>
<html>
<head>
  <title>表格的直列化格式</title>
</head>
<body>
<table cellpadding="5px" cellspacing="0px" border="1px" bordercolor="black">
  <!-- 用兩組 colgroup 控制第三列顯示為紅色:
       第一組 span="2" 相當於佔位符,讓第三列(數學)變紅;
       若改成 span="3",則第四列(英語)會變紅 -->
  <colgroup span="2"></colgroup>
  <colgroup bgcolor="red"></colgroup>

  <!-- 改用 col 也能達到相同效果,這裡額外把第三列設為右對齊 -->
  <col span="2" />
  <col bgcolor="red" align="right" />

  <caption align="left">學生成績表</caption>
  <tr>
    <th>姓名</th><th>語文</th><th>數學</th><th>英語</th>
  </tr>
  <tr>
    <td>張三</td><td>90</td><td>89</td><td>99</td>
  </tr>
  <tr>
    <td>李四</td><td>98</td><td>92</td><td>96</td>
  </tr>
  <tr>
    <td>王五</td><td>92</td><td>97</td><td>91</td>
  </tr>
  <tr>
    <td>總分</td><td>200</td><td>200</td><td>200</td>
  </tr>
</table>
</body>
</html>

掌握 span 的佔位概念之後,替整欄套背景色或對齊方式就只是改一個屬性的事,不必逐格寫 inline style。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved