這裏主要總結記錄下表格的一些屬性和簡單的樣式,方便之後不時之需。html
一、<table>html5
用來定義HTML的表格,具備本地屬性 border 表示邊框,border屬性的值必須爲1或空字符串("")。該屬性不會控制邊框的樣式,而是由CSS來控制ide
table元素能夠有tr,th,td,thead,tbody,tfoot,colgroup元素spa
二、<tr>3d
用來定義表格的一行。因爲HTML表格是面向行的,因此必須分別表示每一行code
tr元素能夠在table,thead,tbody和tfoot元素內使用htm
tr元素內能夠包含一個或者多個td或th元素blog
它的align,bgcolor等屬性已過期,若是要設置屬性,請使用CSS設置字符串
三、<td>it
用來定義表格單元格,能夠同colspan,rowspan,headers局部屬性使用
(1)colspan: 列跨度,該屬性規定了單元格可橫跨的列數,該屬性的值必須是整數
(2)rowspan:行跨度,該屬性規定了單元格可橫跨的行數,該屬性的值必須是整數
(3)headers:該屬性的值是一個或多個單元的ID屬性值,將單元格與列標題相關聯,可用於使用屏幕閱讀器
⚠️:每一個表格必須包含以上三個元素
一個簡單的實例
<!DOCTYPE html> <html> <body> <table> <tr> <td>A</td> <td>B</td> <td>C</td> </tr> <tr> <td>D</td> <td>E</td> <td>F</td> </tr> </table> </body> </html>
效果以下:
四、<th>
用來定義標題單元格,使咱們有效區分數據及其描述
它同 <td> 元素具備相同的局部屬性,二者有以下區別:
五、<thead>
用來定義表格的頁眉,表頭的包裝器。能夠定義一行或多行,這些行是 table 元素的列標籤
沒有thead元素,全部的tr被假定爲屬於表的主體
六、<tbody>
用來定義表格的主體
七、<tfoot>
用來定義標記表格的頁腳
⚠️:
八、<colgroup>
用來定義表列組,可使用其來將樣式應用於某個列,固然也可使用下面要說的col元素
具備局部屬性 span 的 <colgroup> 表示列組應該橫跨的列數。默認是一列,即對錶格的一列設置樣式
<colgroup>能夠包含一個或多個 <col> 元素
九、<col>
用來表示表單個列,建議使用<colgroup>包裹<col>元素而不是<colgroup>直接設置span屬性定義組
<col>也具備局部屬性span
<col>放在<colgroup>的元素內部,<col>的咩哥實例表示組中的一列。使用該標籤能夠將樣式應用於列的組和該組的單個列
十、<caption>
用來定義表格的標題,每一個表中只能包含一個<caption>元素
一個簡單的例子:
<!DOCTYPE html> <html> <head> <style> thead th,tfoot th { text-align: left; background: grey; color: white } tbody th { text-align: right; background: lightgrey; color: grey } /* tbody td { background: greenyellow; } */ #colgroup1 { background-color: blueviolet } #col3 { background-color: yellow; font-size: small } </style> </head> <body> <table> <colgroup id="colgroup1"> <col id="collAnd2" span="2"/> <col id="col3"/> </colgroup> <colgroup id="colgroup2" span="2"></colgroup> <thead> <tr> <th>Rank</th> <th>Name</th> <th>Color</th> <th colspan="2">Size & Votes</th> </tr> </thead> <tfoot> <tr> <th>Rank Footer</th> <th>Name Footer</th> <th>Color Footer</th> <th colspan="2">Size And Votes Footer</th> </tr> </tfoot> <tbody> <tr> <th>Favorite:</th> <td>XML</td> <td>CSS</td> <td>Java</td> <td>IOS</td> </tr> <tr> <th>2nd Favorite:</th> <td>Web</td> <td>HTML5</td> <td>CS</td> <td>460</td> </tr> </tbody> </table> </body> </html>
效果以下: