CSS:如何设置表格背景的样式,包括元素

时间:2013-07-23 11:37:46

标签: html css css3

表格元素的样式表不包括表格标题(<caption>标记内的<table>标记。

表格是否有一种方法可以使用包含标题的渐变背景?

2 个答案:

答案 0 :(得分:1)

添加display:block;应该解决它

<style>
    table{
        background-image: -moz-linear-gradient(bottom, rgb(156,155,250) 43%, rgb(255,255,255) 88%);
        display:block;
   }

<table>
    <caption>Test</caption>
    <tr>
        <td>Col1</td>
        <td>Col2</td>
    </tr>
</table>

答案 1 :(得分:0)

是的,有:http://jsfiddle.net/Pe5Lt/2/

将标题absolute ly relative置于表格中,然后将表格设为padding-top,并将标题置于margin: Xpx auto;的中心位置:

HTML

<table>
    <caption>I'm a CAPTION</caption>
    <thead>        
        <tr>
            <th>I'm a TH</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>I'm a TD</td>
        </tr>
    </tbody>
</table>

CSS

table {
    position: relative;
    padding-top: 30px;
    width: 200px;
    border: 1px solid red;
    background: rgb(30, 87, 153);
    /* Old browsers */
    background: -moz-linear-gradient(top, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 66%, rgba(32, 124, 202, 1) 100%, rgba(125, 185, 232, 1) 100%);
    /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(30, 87, 153, 1)), color-stop(66%, rgba(41, 137, 216, 1)), color-stop(100%, rgba(32, 124, 202, 1)), color-stop(100%, rgba(125, 185, 232, 1)));
    /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 66%, rgba(32, 124, 202, 1) 100%, rgba(125, 185, 232, 1) 100%);
    /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 66%, rgba(32, 124, 202, 1) 100%, rgba(125, 185, 232, 1) 100%);
    /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 66%, rgba(32, 124, 202, 1) 100%, rgba(125, 185, 232, 1) 100%);
    /* IE10+ */
    background: linear-gradient(to bottom, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 66%, rgba(32, 124, 202, 1) 100%, rgba(125, 185, 232, 1) 100%);
    /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#1e5799', endColorstr='#7db9e8', GradientType=0);
    /* IE6-9 */
}
table caption {
    color: red;
    position: absolute;
    top: 0;
    width: 100%;
    margin: 10px auto;
}

使用http://www.colorzilla.com/gradient-editor/

生成的CSS渐变背景