本网站只能被运行在支持JavaScript脚本的环境中

使用CSS 3美化表格之4例

本文介绍四个使用CSS 3美化表格的代码示例。

在纵向表头中附加三角箭头标记

公司名称 常州玉凌软件有限公司
地址 常州市博爱路67-5号4020室
电话 0519-86660120
手机 13063968069

本例中关键之处在于利用th元素的:after伪元素来制作三角箭头标记。但是,由于在Firefox浏览器中由于不能对位于table元素中的任何伪元素使用absolute样式属性值,所以需要另外书写CSS样式代码。

CSS代码

table.company{
    width: 500px;
    margin: 0 auto;
    border-collapse: separate;
    border-spacing: 0px 15px;
    font-size: 12px;
}
 
table.company th,
table.company td {
    padding: 10px;
}
 
table.company th {
    background: #295890;
    vertical-align: middle;
    text-align: left;
    width: 100px;
    overflow: visible;
    position: relative;
    color: #fff;
    font-weight: normal;
    font-size: 15px;
}
 
table.company th:after {
    left: 100%;
    top: 50%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    border-color: rgba(136, 183, 213, 0);
    border-left-color: #295890;
    border-width: 10px;
    margin-top: -10px;
}
/* firefox */
@-moz-document url-prefix() { 
    table.company th::after {
        float: right;
        padding: 0;
        left: 30px;
        top: 10px;
        content: " ";
        height: 0;
        width: 0;
        position: relative;
        pointer-events: none;
        border: 10px solid transparent;
        border-left: #295890 10px solid;
        margin-top: -10px;
    }
}
 
table.company td {
    background: #f8f8f8;
    width: 360px;
    padding-left: 20px;
}

HTML代码

<table class="company">
    <tbody>
        <tr>
            <th class="arrow_box">公司名称</div></th>
            <td>常州玉凌软件有限公司</td>
        </tr>
        <tr>
            <th class="arrow_box">地址</th>
            <td>常州市博爱路67-5号4020室</td>
        </tr>
        <tr>
            <th>电话</th>
            <td>0519-86660120</td>
        </tr>
        <tr>
            <th>手机</th>
            <td>13063968069</td>
        </tr>
    </tbody>
</table>

瓷砖风格的表格

浏览器名 FireFox Google Chrome Internet Explorer
CSS Hack @-moz-document url-prefix() {
 选择器{ }
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
 选择器{ }
}
因版本不同而异
缺陷 不能对位于table元素中的任何伪元素使用absolute样式属性值 印刷时可能错位1px 无法简单描述
其他注意事项 (无) SSL证书过期时无法直观得知 2014年5月1日的现在,由于安全性漏洞问题,最好不要使用

如果巧妙利用boder样式属性及“border-collapse: separate;”样式表达式,则可以制作瓷砖风格的表格。

CSS代码

table.brwsr1 {
    font-size: 12px;
    margin: 0 auto;
    border-collapse: separate;
    border-spacing: 0px 1px;
}
 
table.brwsr1 th {
    padding: 12px;
    vertical-align: middle;
    text-align: left;
    border-bottom: #999 1px solid;
    color: #fff;
}
 
table.brwsr1 td {
    padding: 12px;
    vertical-align: middle;
    text-align: left;
    border-bottom: #999 1px solid;
    font-size: 11px;
}
 
table.brwsr1 th.r1 {
    width: 152px;
    border-right: #999 1px solid;
    background: #447791;
}
 
table.brwsr1 th.r2,
table.brwsr1 td.r2 {
    width: 128px;
    border-left: #fff 1px solid;
    border-right: #999 1px solid;
    background: #5893b1;
}
 
table.brwsr1 th.r2,
table.brwsr1 th.r3,
table.brwsr1 th.r4 {
    text-align: center;
}
 
table.brwsr1 th.r3,
table.brwsr1 td.r3 {
    width: 128px;
    border-left: #fff 1px solid;
    border-right: #999 1px solid;
    background: #7cacc2;
}
 
table.brwsr1 th.r4,
table.brwsr1 td.r4 {
    width: 128px;
    border-left: #fff 1px solid;
    background: #a0c2d3;
}

HTML代码

<table class="brwsr1">
    <tbody>
        <tr>
            <th class="r1">浏览器名</th>
            <th class="r2">FireFox</th>
            <th class="r3">Google Chrome</th>
            <th class="r4">Internet Explorer</th>
        </tr>
        <tr>
            <th class="r1">CSS Hack</th>
            <td class="r2">@-moz-document url-prefix() {<br> 选择器{ }<br>}</td>
            <td class="r3">@media screen and (-webkit-min-device-pixel-ratio:0) {<br> 选择器{ }<br>}</td>
            <td class="r4">因版本不同而异</td>
        </tr>
        <tr>
            <th class="r1">缺陷</th>
            <td class="r2">
                不能对位于table元素中的任何伪元素使用absolute样式属性值
            </td>
            <td class="r3">
                印刷时可能错位1px
            </td>
            <td class="r4">
                无法简单描述
            </td>
        </tr>
        <tr>
            <th class="r1">其他注意事项</th>
            <td class="r2">(无)</td>
            <td class="r3">SSL证书过期时无法直观得知</td>
            <td class="r4">2014年5月1日的现在,由于安全性漏洞问题,最好不要使用</td>
        </tr>
    </tbody>
</table>

笔记风格的表格

FireFox Gecko引擎 支持Linux操作系统 Mozilla
Chrome Blink引擎 支持Linux操作系统 Google
IE Trident引擎 支持一部分Linux操作系统 Microsoft

CSS代码

table.brwsr2 {
    margin: 0 auto;
    width: 650px;
    border: none;
    border-top: solid 1px #666;
    border-bottom: solid 1px #666;
    border-collapse: separate;
    border-spacing: 0 10px;
    background: #f5f5f5;
}
 
table.brwsr2 th {
    vertical-align: middle;
    height: 53px;
    width: 124px;
    border-right: solid 1px #666;
    margin: 0;
    text-align: center;
    color: #333;
    font-size: 16px;
    font-weight: bold;
}
 
table.brwsr2 td.data {
    padding: 0;
    margin: 0;
    height: 53px;
    width: 100px;
    border: none;
    color: #666;
    font-size: 12px;
    line-height: 16px;
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
    overflow: hidden;
}
 
table.brwsr2 td:fst-child {
    padding: 0 0 0 10px;
    vertical-align: middle;
}
 
table.brwsr2 td.bar {
    height: 1px;
    width: 100%;
    border-top: solid 1px #34495e;
}

HTML代码

<table class="brwsr2" style="float:left;">
    <tbody>
        <tr>
            <th style="background:#f5f5f5;">FireFox</th>
            <td class="data">
                Gecko引擎
            </td>
            <td class="data">
                支持Linux操作系统
            </td>
            <td class="data">
                Mozilla
            </td>
        </tr>
        <tr>
            <td class="bar" colspan="6"></td>
        </tr>
        <tr>
            <th style="background:#f5f5f5;">Chrome</th>
            <td class="data">
                Blink引擎
            </td>
            <td class="data">
                支持Linux操作系统
            </td>
            <td class="data">
                Google
            </td>
        </tr>
        <tr>
            <td class="bar" colspan="6"></td>
        </tr>
        <tr>
            <th style="background:#f5f5f5;">IE</th>
            <td class="data">
                Trident引擎
            </td>
            <td class="data">
                支持一部分Linux操作系统
            </td>
            <td class="data">
                Microsoft
            </td>
        </tr>
    </tbody>
</table>

综合运用上述各种技巧

Business
Team
Free
1,000元 500元 免费
20GB 10GB 2GB
附加SSL(无限制) 附加SSL(3个)

如果结合使用前述三角箭头标记的制作方法及border-radius样式属性的使用方法,可以美化“商品价格比较表”。另外,下述表格中由于使用了CSS 3中的first-child伪元素,所以在IE 9浏览器中不能显示出其应有效果。

CSS代码

table.price {
    width: 500px;
    margin: 0 auto;
    border-collapse: separate;
    border-spacing: 1px 0;
    font-size: 15px;
}
table.price th,
table.price td {
    padding: 10px;
}
table.price th {
    box-shadow: 0px 1px 1px rgba(255,255,255,0.8) inset;
    border-radius: 5px 5px 10px 10px;
    background: #88b7d5;
    vertical-align: middle;
    text-align: center;
    width: 125px;
    height: 50px;
    position: relative;
    color: #fff;
    text-shadow: 1px 1px 1px rgba(0,0,0,0.2);
    font-size: 24px;
} 
table.price th.c1 {
    background: rgb(162,206,26); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(97,174,36,1) 1%, rgba(86,155,32,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,rgba(97,174,36,1)), color-stop(100%,rgba(86,155,32,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(97,174,36,1) 1%,rgba(186,155,32,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(97,174,36,1) 1%,rgba(86,155,32,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(97,174,36,1) 1%,rgba(86,155,32,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(97,174,36,1) 1%,rgba(86,155,32,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#61ae24', endColorstr='#569b20',GradientType=0 ); /* IE6-9 */
} 
table.price th.c2 {
    background: rgb(237,73,147); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(204,20,69,1) 1%, rgba(182,17,61,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,rgba(204,20,69,1)), color-stop(100%,rgba(182,17,61,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(204,20,69,1) 1%,rgba(182,17,61,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(204,20,69,1) 1%,rgba(182,17,61,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(204,20,69,1) 1%,rgba(182,17,61,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(204,20,69,1) 1%,rgba(182,17,61,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#cc1445', endColorstr='#b6113d',GradientType=0 ); /* IE6-9 */
} 
table.price th.c3 {
    background: rgb(29,140,233); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(17,63,140,1) 1%, rgba(15,56,125,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,rgba(17,63,140,1)), color-stop(100%,rgba(15,56,125,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(17,63,140,1) 1%,rgba(15,56,125,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(17,63,140,1) 1%,rgba(15,56,125,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(17,63,140,1) 1%,rgba(15,56,125,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(17,63,140,1) 1%,rgba(15,56,125,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#113f8c', endColorstr='#0f387d',GradientType=0 ); /* IE6-9 */
} 
table.price th:after {
    top: 69px;
    left: 5px;
    content: "";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    border: 78px solid transparent;
}
/* firefox */
@-moz-document url-prefix() {
    table.price th {
        text-indent: 10px;
    }
    table.price th::after {
        padding: 0;
        top: 60px;
        left: 0;
        content: "";
        height: 0;
        width: 0;
        position: relative;
        pointer-events: none;
        border: 50px solid transparent;
        border-top: #B7D567 15px solid;
    }
}
table.price th.c1:after {
    border-top: #569b20 15px solid;
}
table.price th.c2:after {
    border-top: #b6113d 15px solid;
}
table.price th.c3:after {
    border-top: #0f387d 15px solid;
} 
table.price td {
    box-shadow: 0px 1px 1px rgba(255,255,255,0.3) inset;
    text-align: center;
    padding-top: 15px;
    border: none;
    height: 70px;
} 
table.price tr:nth-child(2n+1) td.c1 {
    background: #f7f9e8;
}
table.price tr:nth-child(2n+0) td.c1 {
    background: #e2efbc;
} 
table.price tr:nth-child(2n+1) td.c2 {
    background: #ffecf5;
}
table.price tr:nth-child(2n+0) td.c2 {
    background: #ffdfed;
} 
table.price tr:nth-child(2n+1) td.c3 {
    background: #fff;
}
table.price tr:nth-child(2n+0) td.c3 {
    background: #eee;
}
table.price tr:last-child td {
    border-radius: 0 0 5px 5px;
    box-shadow: 0px 2px 2px rgba(0,0,0,0.2);
} 
table.price tr:last-child td button {
    font-size:12px;
    font-family:Arial;
    font-weight:normal;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
    border-radius:5px;
    border:1px solid #ffaa22;
    padding:9px 18px;
    text-decoration:none;
    background:-moz-linear-gradient( center top, #ffec64 5%, #ffab23 100% );
    background:-ms-linear-gradient( top, #ffec64 5%, #ffab23 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffec64', endColorstr='#ffab23');
    background:-webkit-gradient( linear, left top, left bottom, color-stop(5%, #ffec64), color-stop(100%, #ffab23) );
    background-color:#ffec64;
    color:#333333;
    display:inline-block;
    text-shadow:1px 1px 0px #ffee66;
    -webkit-box-shadow:inset 1px 1px 0px 0px #fff6af;
    -moz-box-shadow:inset 1px 1px 0px 0px #fff6af;
    box-shadow:inset 1px 1px 0px 0px #fff6af;
}
table.price tr:last-child td button:hover {
    background:-moz-linear-gradient( center top, #ffab23 5%, #ffec64 100% );
    background:-ms-linear-gradient( top, #ffab23 5%, #ffec64 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffab23', endColorstr='#ffec64');
    background:-webkit-gradient( linear, left top, left bottom, color-stop(5%, #ffab23), color-stop(100%, #ffec64) );
    background-color:#ffab23;
}
table.price tr:last-child td button {
    position:relative;
    top:1px;
}

HTML代码

<table class="price"  style="float:left;">
    <thead>
        <tr>
            <th class="c1">Business<br></th>
            <th class="c2">Team<br></th>
            <th class="c3">Free<br></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="c1">
                1,000元
            </td>
            <td class="c2">
                500元
            </td>
            <td class="c3">
                免费
            </td>
        </tr>
        <tr>
            <td class="c1">
                20GB
            </td>
            <td class="c2">
                10GB
             </td>
            <td class="c3">
                2GB
            </td>
        </tr>
        <tr>
            <td class="c1">
                附加SSL(无限制)
            </td>
            <td class="c2">
                附加SSL(3个)
            </td>
            <td class="c3">
                无
            </td>
        </tr>
        <tr>
            <td class="c1"><button>申请</button></td>
            <td class="c2"><button>申请</button></td>
            <td class="c3"><button>申请</button></td>
        </tr>
    </tbody>
</table>