Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a table cell 2 colors?

How to create <td> with 2 colors?

This is my code :

<table cellspacing=0>
   <tr>
    <td bgcolor=green><img src='pic/s.gif' width=8 height=5></td>
    <td bgcolor=#AAAAAA><img src='pic/s.gif' width=72 height=5></td>
    <td style="color: green;">10%</td>
   </tr>
</table>

but I want use one td and write 10% on td with z-index=1 but I don't know how.

enter image description here

like image 729
Maysam Avatar asked Jan 18 '14 12:01

Maysam


People also ask

How do I make one cell two colors in Word?

Select the cell or range of cells you want to format. Click Home > Format Cells dialog launcher, or press Ctrl+Shift+F. On the Fill tab, under Background Color, pick the color you want. To use a pattern with two colors, pick a color in the Pattern Color box, and then pick a pattern in the Pattern Style box.

Can we set a background color just for one cell in a table?

In HTML, table background color is defined using Cascading Style Sheets (CSS). Specifically, you use the background-color property to define background color. You can apply this property against the whole table, a row, or a single cell.

How do I put the background color in a specific cell in a table?

Cell background colors are set by applying the bgcolor attribute to a <tr> tag (to color the row) or to a <td> tag (to color the cell). Cell colors override row colors which, in turn, override table background colors.


2 Answers

Interesting concept, but do you really need a table layout?

Here's a FIDDLE with a slightly different approach.

HTML

<table>
    <tr><td>
            <div class='celldiv'>20%
                <div class='variablediv'></div>
            </div>
        </td></tr>
</table>

CSS

td {
    height: 20px;
    width: 100px;
    padding-left: 30px;
}
.celldiv {
    height: 100%;
    width: 100%;
    background-color: red;
}
.variablediv {
    float: left;
    height: 100%;
    width: 20%;
    background-color: blue;
    margin-right: 5px;
}

And you can dynamically change the width of the blue and the number with jQuery.

Just an idea.

like image 119
TimSPQR Avatar answered Oct 10 '22 15:10

TimSPQR


I hope you are expecting the result like this...

DEMO JsFiddle

HTML

    <table>
       <tr>
           <td class="red"></td>
           <td class="green"><span class="ten">10%</span></td>
       </tr>
    </table>

CSS

table
{
    border-collapse: collapse;
}
.red
{
    background-color: red;
    width: 10px;
    height: 5px;
}
.green
{
    background-color: green;
    width: 90px;
    height: 5px;
}
.ten
{
    color: #ffffff;
}
like image 24
Karuppiah RK Avatar answered Oct 10 '22 15:10

Karuppiah RK