zero-divisor
zero-divisor

Reputation: 610

CSS: How to style table background including the <caption> element

Stylesheets for tables elements exclude the table caption (a <caption> tag inside the <table> tag).

Is there a way for a table to have a gradiented background that includes the caption ?

Upvotes: 0

Views: 296

Answers (2)

Andrea Ligios
Andrea Ligios

Reputation: 50203

Yes, there is: http://jsfiddle.net/Pe5Lt/2/

Position the caption absolutely, relatively to the table, then give the table some padding-top and center the caption with 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;
}

CSS Gradient background generated with http://www.colorzilla.com/gradient-editor/

Upvotes: 0

insanebits
insanebits

Reputation: 838

Adding display:block; should solve it

<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>

Upvotes: 1

Related Questions