在現在 div 大行其道的時代,table 這個標簽似乎很少被人提及,到處都是 div+css 布局的書以及博客文章,但其實 table 以及連帶的其他表格標簽依然在網頁中占很重要的地位,特別是后台展示數據的時候表格運用是否熟練就顯得很重要,一個清爽簡約的表格能夠把繁雜的數據表現得很有條理,雖然 div 布局也可以做到,但是總沒有表格來得方便。平時也經常接觸到表格,現在總結一下表格的一些屬性和樣式,以及學習構思一些表格的樣式,以便以后不時之需。
一、標簽
<table> 定義 HTML 表格
<thead> 標簽定義表格的表頭
<tbody> 標簽表格主體(正文)
<tfoot> 標簽定義表格的頁腳(腳注或表注)
<tr> 元素定義表格行
<th> 元素定義表頭
<td> 元素定義表格單元
<caption> 元素定義表格標題,必須緊隨 table 標簽之后。只能對每個表格定義一個標題,默認居中與表格之上
<col> 標簽為表格中一個或多個列定義屬性值。
<colgroup> 標簽用於對表格中的列進行組合,以便對其進行格式化。
二、表格標簽及標簽屬性
(1)<table>
<table> 標簽屬性說實話個人覺得挺好用的,比如 align,width等,但是本着表現層與結構層的分離,現在w3c上已經不贊成使用了。
align:相當於浮動,用 css 的 float 代替
bgcolor:用 css 的background-color 代替
border:table 的 border 屬性是繼承到內部的 td 標簽的,相當於同時對 (selector):table 和 (selector) table td 設置 css 的 border 屬性,但是當你設置大於1的 border 數值時,又只有 table 的 border 寬度會改變。默認的瀏覽器屬性包括:border-collapse: separate;border-spacing: 2px; 故默認添加后是雙邊border
cellpadding:規定單元邊沿與其內容之間的空白,其實是設置表哥內部 td 標簽的 padding 屬性,用 (selector) table td 設置 css 的 padding 屬性代替。
cellspacing:規定單元格之間的空間,用 (selector):table 設置 css 的 padding 屬性代替。
frame:規定外側邊框的哪個部分是可見的,即設置表格 border,基本不會用這個屬性。
rules:規定內側邊框的哪個部分是可見的,同 frame,幾乎不會用到。
summary:規定表格內容的摘要,屏幕閱讀器可以利用該屬性,不會有其他視覺效果。
width:用 css 的 width 代替。
css 屬性
table-layout:automatic(default) | fixed | inherit(列寬度由單元格內容設定 | 列寬由表格寬度和列寬度設定 | 繼承父屬性)
需要注意的是,表格以及列設置的 width 屬性在 table-layout 為 automatic 時都是表現為 min-width,當 table-layout 的值為 fixed 時則表現為固定的 width 值。
當需要表格內容的寬度在控制范圍內,不會超出自己設置的范圍,則可以用 fixed 屬性值來設置這個屬性,下面舉個例子。
--------------------------------------------------------------舉個栗子--------------------------------------------------------------
HTML:

1 <table> 2 <thead> 3 <tr> 4 <th>title</th> 5 <th>title</th> 6 <th>title</th> 7 </tr> 8 </thead> 9 <tbody> 10 <tr> 11 <td>the</td> 12 <td>testinglongstring</td> 13 <td>Row</td> 14 </tr> 15 <tr> 16 <td>Second</td> 17 <td>-</td> 18 <td>Row</td> 19 </tr> 20 <tr> 21 <td>the</td> 22 <td>third</td> 23 <td>Row</td> 24 </tr> 25 </tbody> 26 </table>
CSS:

1 table{ 2 width: 300px; 3 border:1px solid #000; 4 } 5 table td,table th{ 6 border:1px solid #000; 7 }
沒有顯式加 table-layout 這個屬性時是默認為 automatic 值,此時由於 testinglongstring 內容比較長,表格自動調整列的寬度,效果如下:

table 標簽加了 table-layout:fixed 屬性后,瀏覽器直接渲染表結構(相當於顯示沒有內容的表),太長的內容會覆蓋到其他表格,效果如下:

--------------------------------------------------------------吃完栗子--------------------------------------------------------------
而往往我們希望既保持我們要的列寬度,同時確保內容不會這樣覆蓋到其他表格單元單元(針對英文)。這里就要涉及到另外兩個 css 屬性了:
1、word-wrap:normal(default)| break-word(只在允許的斷字點換行 | 在長單詞或 URL 地址內部進行換行)
這里主要用到 break-word 屬性值。
2、work-break:normal(defaut)| break-all | keep-all(默認的換行規則 | 允許在單詞內換行 | 只能在半角空格或連字符處換行)
這里主要用到 break-all 屬性值。
用到的兩個 css 屬性的屬性值作用類似,但有些區別,break-word 只有在單個單詞都超過外圍寬度的時候才會斷單詞;break-all 則是把每個行填滿,然后行尾不管是單詞還是空格都斷為下一行,個人優選 word-wrap 屬性,可以減少斷單詞的幾率,畢竟不得已才斷單詞,斷了后會影響單詞內容表達。具體辨析可以看 《
你真的了解word-wrap和word-break的區別嗎?》講得比較詳細。
--------------------------------------------------------------舉個栗子--------------------------------------------------------------
沿用上例的 HTML 結構,舉 word-wrap 屬性的例子:
CSS:

1 table { 2 width: 300px; 3 border: 1px solid #000; 4 table-layout: fixed; 5 word-wrap:break-word; 6 } 7 table td, 8 table th { 9 border: 1px solid #000; 10 } 11 .odd{ 12 width: 120px; 13 }
表現如下:

--------------------------------------------------------------吃完栗子--------------------------------------------------------------
(2)<thead>、<tbody>、<tfoot>
align:屬性規定內容的水平對齊方式,用 css 屬性 text-align 代替。
valign:( top|middle|bottom|baseline ),規定 tbody 元素中內容的垂直對齊方式,默認是 middle。相當於 css 的 vertical-align 屬性,這里 bottom 和 baseline 在 w3shcool 上還有一些
辨析,不過只是對於不同字號的英文內容來說有點用處。
還有 char,charoff 屬性幾乎是用不到的,所以不列舉了。
注意
:
1、<thead>、<tfoot> 標簽內部必須擁有 <tr> 標簽;
2、<thead> 標簽不管放在 <table> 內的哪個位置,都會被定位到表格的頭部,同理 <tfoot> 會被定位到底部。另外,和我們的常識不同,<tfoot> 標簽是要放在 <tbody> 標簽之前的,結合官方文檔和我自己的理解,主要是因為 <thead> 和 <tfoot> 內的內容一般比較重要,這樣能保證在表現具有大量行內容的表格時能夠提前渲染 <tfoot>,讓用戶先看到。
3、<thead>、<tbody>、<tfoot> 的結束標簽為了精簡,均可以省略。
4、設置 <thead> 和 <tfoot> 的一個好處是,當打印很長的需要分頁的表格時,每一頁上都會有 <thead>、<tfoot> 的內容。
(3)tr
align,valign:同(2)
(4)td,th
abbr:規定單元格中內容的縮寫版本。不會在普通的 web 瀏覽器中造成任何視覺效果方面的變化。屏幕閱讀器可以利用該屬性。
headers:規定表頭與單元格相關聯。不會在普通瀏覽器中產生任何視覺變化。屏幕閱讀器可以利用該屬性。
scope:定義將表頭單元與數據單元相關聯的方法。不會在普通瀏覽器中產生任何視覺變化。屏幕閱讀器可以利用該屬性。
align,valign:同(2)。需要留意的是 th 默認是居中屬性的,而且 th 內的字體是加粗的。
nowrap:規定表格單元格中的內容不換行。用 white-space: nowrap 代替
colspan:規定單元格可橫跨的列數。比較常用,相當於 word 中的合並橫向的單元格。
rowspan:規定單元格可橫跨的行數。比較常用,相當於 word 中的合並垂直方向的單元格。
(5)caption
align:(top | bottom | left | right ),規定 caption 元素的對齊方式,默認居中。
align 的瀏覽器支持並不好,只有 top 和 bottom 屬性值支持是統一的,left 和 right 在不同瀏覽器中的表現也不一樣,所以這兩個屬性值當然不用,而 css 屬性 caption-side 剛好就只有 top 和 bottom,故完全可以用 css 屬性 caption-side 代替 align。至於要實現 left 和 right 的效果,給 caption 加 margin 屬性或者 padding 屬性或者用其他標簽實現都不難 。
c
ss 屬性
caption-side:top | bottom | inherit(表格標題定位在表格之上 | 表格標題定位在表格之下 | 繼承父屬性)
(6)col,colgroup
<colgroup> 標簽中的一些屬性在一些瀏覽器中的支持並不好,只有在一些老的瀏覽器中(IE7-等)才能表現那些屬性,具體原因可以
點此參考。
能夠用的屬性主要是以下三個:
span:規定列組應該橫跨的列數。默認一列,即對表格的一列設置樣式。
width:col 元素的寬度。
background:設置背景色,要通過 css 樣式來設置。
<col> 標簽需要包裹在 <colgroup> 內,就算沒有寫一般瀏覽器也會自動幫你加上。屬性和 <colgroup> 類似,增加了可以設置 css 的 border 屬性。個人認為不在 <colgroup> 標簽上設置屬性,而是用 <colgroup> 包裹 <col> 的寫法比較規范。
說到列,要重新提一下前面的 table-layout 屬性,下面舉例子說明 col 對這個屬性的影響。
--------------------------------------------------------------舉個栗子--------------------------------------------------------------
HTML:

1 <table> 2 <colgroup> 3 <col class="odd"></col> 4 <col class="even"></col> 5 <col class="odd"></col> 6 </colgroup> 7 <thead> 8 <tr> 9 <th>title</th> 10 <th>title</th> 11 <th>title</th> 12 </tr> 13 </thead> 14 <tbody> 15 <tr> 16 <td>the</td> 17 <td>testinglongstring</td> 18 <td>Row</td> 19 </tr> 20 <tr> 21 <td>Second</td> 22 <td>-</td> 23 <td>Row</td> 24 </tr> 25 <tr> 26 <td>the</td> 27 <td>third</td> 28 <td>Row</td> 29 </tr> 30 </tbody> 31 </table>
CSS:

1 table { 2 width: 300px; 3 border: 1px solid #000; 4 } 5 table td, 6 table th { 7 border: 1px solid #000; 8 } 9 .odd{ 10 width: 120px; 11 }
設置 120px 的列寬度后,中間列(.even)的寬度不夠,但是瀏覽器會幫你計算並為中間內容空出足夠寬度:

設置了 table-layout 屬性的 fixed 值后就完全按照自己的意思來渲染了:

--------------------------------------------------------------吃完栗子--------------------------------------------------------------
(7)border-collapse | border-spacing | empty-cells
這幾個 css 屬性,基本只會用在表格上。
border-collapse:separate(defaul)| collapse |
inherit (邊框會被分開 | 邊框會合並為一個單一的邊框 | 繼承父屬性)
border-spacing:length length(定義一個 length 參數,那么定義的是水平和垂直間距。定義兩個 length 參數,那么第一個設置水平間距,而第二個設置垂直間距)
empty-cells:show(defaul)| hide | inherit (在空單元格周圍繪制邊框 | 不繪制 | 繼承父屬性)
注意:
border-spacing 和 empty-cells 屬性只有在 border-collapse 為 separate 是才有效的。
--------------------------------------------------------------舉個栗子--------------------------------------------------------------
HTML:

1 <table> 2 <tr> 3 <td>Row</td> 4 <td>One</td> 5 </tr> 6 <tr> 7 <td>Row</td> 8 <td></td> 9 </tr> 10 </table>
CSS:

1 table { 2 border-collapse: separate; 3 border-spacing: 5px 20px; 4 empty-cells: hide; 5 border:1px solid #000; 6 } 7 td{ 8 border:1px solid #000; 9 }
表現如下:

--------------------------------------------------------------吃完栗子--------------------------------------------------------------
三、表格 css 樣式
(1)單線邊框
HTML:

1 <table> 2 <tr> 3 <td>Row</td> 4 <td>One</td> 5 </tr> 6 <tr> 7 <td>Row</td> 8 <td>Two</td> 9 </tr> 10 </table>
(方法一) CSS:

1 table { 2 width: 200px; 3 border-left: 1px solid #000; 4 border-top: 1px solid #000; 5 border-collapse: collapse; 6 } 7 td { 8 border-right: 1px solid #000; 9 border-bottom: 1px solid #000; 10 }
(方法二)CSS:

1 table { 2 width: 200px; 3 border-collapse: collapse; 4 } 5 td { 6 border: 1px solid #000; 7 }
效果如下:

(2)列和行的高亮
在復雜的多行多列的表格中,鼠標停留的行或列高亮能夠帶來更優的體驗,下面講講這方面的應用。
1、行的高亮
行的高亮實現比較簡單,可以使用 css 的 :hover 偽類實現,即 tr:hover 即可實現,后面的表格例子會出現。
2、列的高亮
列的高亮用 css 似乎沒有什么實現的方法,故還是用 JavaScript 實現。
<1>原生 JavaScript
原生 JavaScript 實現比較麻煩,
A Complete Guide to the Table Element 文章介紹了一種原生 js 代碼的寫法。不過代碼的兼容性不是很好,IE10 以下的版本就無法支持了(替換 addClass() 和 removeClass() 方法后在 IE9 下也可以用)。下面是我自己寫的原生 JavaScript 實現方法,不過也只能支持 IE9+,IE8 及以下的 IE 瀏覽器要加兼容性代碼:
JavaScript:

1 //#table可以是table的id,也可以是tbody的id 2 var element = document.getElementById("table"); 3 var row_col = element.rows; 4 window.onload = function() { 5 for (var i = 0; i < row_col.length; i++) { 6 var cell_col = row_col[i].cells; 7 for (var j = 0; j < cell_col.length; j++) { 8 cell_col[j].addEventListener('mouseover', function() { 9 cellIndex(this, true); 10 }, false); 11 cell_col[j].addEventListener('mouseout', function() { 12 cellIndex(this, false); 13 }, false); 14 } 15 } 16 } 17 function cellIndex(element, bool) { 18 for (var i = 0; i < row_col.length; i++) { 19 var cell_col = row_col[i].cells; 20 for (var j = 0; j < cell_col.length; j++) { 21 if (element == cell_col[j]) { 22 highLight(j, bool); 23 } 24 } 25 } 26 } 27 function highLight(index, bool) { 28 for (var i = 0; i < row_col.length; i++) { 29 var cell_col = row_col[i].cells; 30 if (bool) { 31 //列高亮,#eee為高亮顏色 32 cell_col[index].style.backgroundColor = "#eee"; 33 } else { 34 //移除列高亮,#fff為原來顏色 35 cell_col[index].style.backgroundColor = "#fff"; 36 } 37 } 38 }
<2>引用Jquery
使用 Jquery 框架實現方便很多,而且能夠兼容 IE5.5+。好久沒寫 Jquery,選擇器不是很熟,所以參考了一下
html5及css3對table表格高亮當前行列的多瀏覽器兼容寫法 中的代碼,其中 high_light css 類中定義高亮的顏色:

1 $(document).ready( 2 function() { 3 $('table td').hover( 4 function() { 5 //獲取鼠標所在的 td 在所在行中的 index 6 var td_index = $(this).parent().children('td').index($(this)[0]); 7 $("table tr:not(:has(th))").each( 8 function(i){ 9 $(this).children("td").eq(td_index).addClass('high_light'); 10 } 11 ); 12 }, 13 function() { 14 var td_index = $(this).parent().children('td').index($(this)[0]); 15 $("table tr:not(:has(th))").each( 16 function(i){ 17 $(this).children("td").eq(td_index).removeClass('high_light'); 18 } 19 ); 20 } 21 ); 22 } 23 );
四、清爽簡約的表格
下面給出一些簡約的實用的表格樣式,留作備用。
(1)不帶豎線的表格
HTML:

1 <table> 2 <thead> 3 <tr> 4 <th>title</th> 5 <th>title</th> 6 <th>title</th> 7 </tr> 8 </thead> 9 <tbody> 10 <tr> 11 <td>the</td> 12 <td>First</td> 13 <td>Row</td> 14 </tr> 15 <tr> 16 <td>Second</td> 17 <td>-</td> 18 <td>Row</td> 19 </tr> 20 <tr> 21 <td>the</td> 22 <td>third</td> 23 <td>Row</td> 24 </tr> 25 </tbody> 26 </table>
CSS:

1 table { 2 width: 300px; 3 line-height: 2em; 4 font-family: Arial; 5 border-collapse: collapse; 6 } 7 thead tr { 8 color: #ae1212; 9 border-bottom: 2px solid #980000; 10 } 11 tbody tr { 12 color: #bd3030; 13 font-size: 0.8em; 14 border-bottom: 1px solid #ffaeae; 15 } 16 th { 17 font-weight: normal; 18 text-align: left; 19 } 20 th,td { 21 padding: 0 10px; 22 }
效果如下:

(2)不帶橫線的表格
HTML:
< 沿用例 1)的 HTML >
CSS:

1 table { 2 width: 300px; 3 line-height: 2em; 4 font-family: Arial; 5 text-align: center; 6 border-collapse: collapse; 7 border-top: 2px solid #980000; 8 border-bottom: 2px solid #980000; 9 } 10 thead tr { 11 color: #ae1212; 12 } 13 tbody tr { 14 color: #bd3030; 15 font-size: 0.8em; 16 } 17 th { 18 font-weight: normal; 19 } 20 th,td { 21 border-left: 1px solid #ffaeae; 22 border-right: 1px solid #ffaeae; 23 }
效果如下:

(3)帶背景表格
1、HTML:
< 沿用例 1)的 HTML >
CSS:

1 table { 2 width: 300px; 3 line-height: 2em; 4 font-family: Arial; 5 border-collapse: collapse; 6 } 7 thead tr { 8 color: #902d2d; 9 background-color: #e09999; 10 border-bottom: 1px solid #fff; 11 } 12 tbody tr { 13 color: #ac5959; 14 font-size: 0.8em; 15 border-bottom: 1px solid #fff; 16 background-color: #ffe7e7; 17 } 18 tbody tr:hover { 19 background-color: #ffd4d4; 20 } 21 th { 22 font-weight: normal; 23 text-align: left; 24 } 25 th,td { 26 padding: 0 10px; 27 }
效果如下:


2、HTML:
< 沿用例 1)的 HTML >
CSS:

1 table { 2 width: 300px; 3 line-height: 2em; 4 font-family: Arial; 5 border-collapse: collapse; 6 } 7 th,td { 8 padding: 0 10px; 9 } 10 th { 11 color: #a03f3f; 12 font-weight: normal; 13 text-align: left; 14 background-color: #f2caca; 15 border: 1px solid #fff; 16 } 17 td{ 18 color: #c48080; 19 font-size: 0.8em; 20 background-color: #fff3f3; 21 border-top: 1px solid #ffdede; 22 border-left: 1px solid #ffdede; 23 } 24 tbody tr:hover th{ 25 background-color: #ffdede; 26 border-right:1px solid #ffdede; 27 color:#9a4242; 28 } 29 tbody tr:hover td{ 30 background-color: #ffdede; 31 border-left:1px solid #ffdede; 32 border-top: 1px solid #ffdede; 33 color:#9a4242; 34 }
效果如下:

3、HTML:

1 <table> 2 <colgroup> 3 <col class="odd"></col> 4 <col class="even"></col> 5 <col class="odd"></col> 6 <col class="even"></col> 7 </colgroup> 8 <thead> 9 <tr> 10 <th>title</th> 11 <th class="even_th">title</th> 12 <th>title</th> 13 <th class="even_th">title</th> 14 </tr> 15 </thead> 16 <tbody> 17 <tr> 18 <td>the</td> 19 <td>the</td> 20 <td>the</td> 21 <td>the</td> 22 </tr> 23 <tr> 24 <td>first</td> 25 <td>-</td> 26 <td>third</td> 27 <td>fourth</td> 28 </tr> 29 <tr> 30 <td>Row</td> 31 <td>Row</td> 32 <td>Row</td> 33 <td>Row</td> 34 </tr> 35 </tbody> 36 </table>
CSS:

1 table { 2 width: 300px; 3 line-height: 2em; 4 font-family: Arial; 5 border-collapse: collapse; 6 text-align: center; 7 } 8 th,td { 9 padding: 0 10px; 10 } 11 th { 12 color: #a03f3f; 13 font-weight: normal; 14 } 15 td{ 16 color: #c48080; 17 font-size: 0.8em; 18 } 19 thead th{ 20 background-color: #fbdcdc; 21 } 22 .odd{ 23 background-color: #fff3f3; 24 } 25 .even{ 26 border-left:1px solid #fff; 27 border-right:1px solid #fff; 28 background-color: #ffe9e9; 29 } 30 .even_th{ 31 background-color: #eec4c4; 32 }
效果如下:


4、適用於表現簡單后台數據的表格
HTML:

1 <table> 2 <tr> 3 <td>the</td> 4 <td>the</td> 5 <td>the</td> 6 <td>the</td> 7 </tr> 8 <tr> 9 <td>first</td> 10 <td>-</td> 11 <td>third</td> 12 <td>fourth</td> 13 </tr> 14 <tr> 15 <td>Row</td> 16 <td>Row</td> 17 <td>Row</td> 18 <td>Row</td> 19 </tr> 20 </table>
CSS:

1 table { 2 width: 300px; 3 line-height: 2em; 4 font-family: Arial; 5 border-width: 1px; 6 border-style: solid; 7 border-color: #eec4c4 #eec4c4 #fff #fff; 8 text-shadow: 0 1px 0 #FFF; 9 border-collapse: separate; 10 border-spacing: 0; 11 background-color: #ffe9e9; 12 } 13 th { 14 color: #a03f3f; 15 font-weight: normal; 16 text-align: left; 17 } 18 td { 19 color: #c48080; 20 font-size: 0.8em; 21 } 22 th,td { 23 padding: 0 10px; 24 border-width: 1px; 25 border-style: solid; 26 border-color: #fff #fff #eec4c4 #eec4c4; 27 }
效果如下:

這里也只是列舉比較基本的表格實例,加圓角、背景圖等等在不同情況下可以獲得更好的視覺效果,以后如果有看到更好的例子會繼續補充。表格在現在的使用中已不像以前那么廣了,基本只有在表現數據的時候才會用到,所以我並沒有太深入地去了解表格的表現原理,
這里涉及
的知識都比較基礎,而且只是冰山一角,
想要進一步了解的可以去看看 HTML 4.01 和 CSS 2.01關於 table 的文檔(文末有附鏈接)。寫這篇博文的時候也發現了其他一些有趣的關於表格的應用與拓展,比如響應式的表格、表格搜索、表格排序等等,再加進來博文就太長了(為自己太懶找個借口......),等有空的時候再來了解總結一下吧。
本文來源:JuFoFu
本文地址:http://www.cnblogs.com/JuFoFu/p/5140302.html
水平有限,錯誤歡迎指正。原創博文,轉載請注明出處。
參考文檔:
CSS2.1 .
Tables
參考文章:
R. Christie .
Top 10 CSS Table Designs
W3Cfuns .
參透Html表格