table 样式美化
1. 单像素边框CSS表格
这是一个很常用的表格样式。

源代码:
1 <!-- CSS goes in the document HEAD or added to your external stylesheet -->
2 <style type="text/css">
3 table.gridtable {
4 font-family: verdana,arial,sans-serif;
5 font-size:11px;
6 color:#333333;
7 border-width: 1px;
8 border-color: #666666;
9 border-collapse: collapse;
10 }
11 table.gridtable th {
12 border-width: 1px;
13 padding: 8px;
14 border-style: solid;
15 border-color: #666666;
16 background-color: #dedede;
17 }
18 table.gridtable td {
19 border-width: 1px;
20 padding: 8px;
21 border-style: solid;
22 border-color: #666666;
23 background-color: #ffffff;
24 }
25 </style>
26
27 <!-- Table goes in the document BODY -->
28 <table class="gridtable">
29 <tr>
30 <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
31 </tr>
32 <tr>
33 <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
34 </tr>
35 <tr>
36 <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
37 </tr>
38 </table>
2. 带背景图的CSS样式表格
和上面差不多,不过每个格子里多了背景图。

cell-blue.jpg
cell-grey.jpg
1. 下载上面两张图,命名为cell-blue.jpg和cell-grey.jpg
2. 拷贝下面的代码到你想要的地方,记得修改图片url
1 <!-- CSS goes in the document HEAD or added to your external stylesheet -->
2 <style type="text/css">
3 table.imagetable {
4 font-family: verdana,arial,sans-serif;
5 font-size:11px;
6 color:#333333;
7 border-width: 1px;
8 border-color: #999999;
9 border-collapse: collapse;
10 }
11 table.imagetable th {
12 background:#b5cfd2 url('cell-blue.jpg');
13 border-width: 1px;
14 padding: 8px;
15 border-style: solid;
16 border-color: #999999;
17 }
18 table.imagetable td {
19 background:#dcddc0 url('cell-grey.jpg');
20 border-width: 1px;
21 padding: 8px;
22 border-style: solid;
23 border-color: #999999;
24 }
25 </style>
26
27 <!-- Table goes in the document BODY -->
28 <table class="imagetable">
29 <tr>
30 <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
31 </tr>
32 <tr>
33 <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
34 </tr>
35 <tr>
36 <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
37 </tr>
38 </table>
3. 自动换整行颜色的CSS样式表格(需要用到JS)
这个CSS样式表格自动切换每一行的颜色,在我们需要频繁更新一个大表格的时候很有用。

代码:
1 <!-- Javascript goes in the document HEAD -->
2 <script type="text/javascript">
3 function altRows(id){
4 if(document.getElementsByTagName){
5
6 var table = document.getElementById(id);
7 var rows = table.getElementsByTagName("tr");
8
9 for(i = 0; i < rows.length; i++){
10 if(i % 2 == 0){
11 rows[i].className = "evenrowcolor";
12 }else{
13 rows[i].className = "oddrowcolor";
14 }
15 }
16 }
17 }
18
19 window.onload=function(){
20 altRows('alternatecolor');
21 }
22 </script>
23
24
25 <!-- CSS goes in the document HEAD or added to your external stylesheet -->
26 <style type="text/css">
27 table.altrowstable {
28 font-family: verdana,arial,sans-serif;
29 font-size:11px;
30 color:#333333;
31 border-width: 1px;
32 border-color: #a9c6c9;
33 border-collapse: collapse;
34 }
35 table.altrowstable th {
36 border-width: 1px;
37 padding: 8px;
38 border-style: solid;
39 border-color: #a9c6c9;
40 }
41 table.altrowstable td {
42 border-width: 1px;
43 padding: 8px;
44 border-style: solid;
45 border-color: #a9c6c9;
46 }
47 .oddrowcolor{
48 background-color:#d4e3e5;
49 }
50 .evenrowcolor{
51 background-color:#c3dde0;
52 }
53 </style>
54
55
56 <!-- Table goes in the document BODY -->
57 <table class="altrowstable" id="alternatecolor">
58 <tr>
59 <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
60 </tr>
61 <tr>
62 <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
63 </tr>
64 <tr>
65 <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
66 </tr>
67 </tr>
68 <tr>
69 <td>Text 3A</td><td>Text 3B</td><td>Text 3C</td>
70 </tr>
71 <tr>
72 <td>Text 4A</td><td>Text 4B</td><td>Text 4C</td>
73 </tr>
74 <tr>
75 <td>Text 5A</td><td>Text 5B</td><td>Text 5C</td>
76 </tr>
77 </table>
78
79 <!-- The table code can be found here: http://www.textfixer/resources/css-tables.php#css-table03 -->
4. 鼠标悬停高亮的CSS样式表格 (需要JS)
纯CSS显示表格高亮在IE中显示有问题,所以这边使用了js来做高亮(由于csdn博客限制了js的使用,我会在近期将博客迁移放到自己的web主机上)。

有一点要小心的是,不要定义格子的背景色。
1 <!-- CSS goes in the document HEAD or added to your external stylesheet -->
2 <style type="text/css">
3 table.hovertable {
4 font-family: verdana,arial,sans-serif;
5 font-size:11px;
6 color:#333333;
7 border-width: 1px;
8 border-color: #999999;
9 border-collapse: collapse;
10 }
11 table.hovertable th {
12 background-color:#c3dde0;
13 border-width: 1px;
14 padding: 8px;
15 border-style: solid;
16 border-color: #a9c6c9;
17 }
18 table.hovertable tr {
19 background-color:#d4e3e5;
20 }
21 table.hovertable td {
22 border-width: 1px;
23 padding: 8px;
24 border-style: solid;
25 border-color: #a9c6c9;
26 }
27 </style>
28
29 <!-- Table goes in the document BODY -->
30 <table class="hovertable">
31 <tr>
32 <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
33 </tr>
34 <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
35 <td>Item 1A</td><td>Item 1B</td><td>Item 1C</td>
36 </tr>
37 <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
38 <td>Item 2A</td><td>Item 2B</td><td>Item 2C</td>
39 </tr>
40 <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
41 <td>Item 3A</td><td>Item 3B</td><td>Item 3C</td>
42 </tr>
43 <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
44 <td>Item 4A</td><td>Item 4B</td><td>Item 4C</td>
45 </tr>
46 <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
47 <td>Item 5A</td><td>Item 5B</td><td>Item 5C</td>
48 </tr>
49 </table>

浙公网安备 33010602011771号