CSS Styling Column

I have the following HTML table. I need to specify a background color for each column (first column = red, second column = yellow, third column = blue). How to do it using CSS?

Note. This is required to work in IE6.

http://jsfiddle.net/Lijo/kw4yU/

  <table id = "myTable">
    <thead>
        <tr>
            <th>
                 Name
            </th>
            <th>
                Address
            </th>
            <th>
                Age
            </th>
        </tr>
    </thead>
    <tr>
        <td>
            Lijo
        </td>
        <td>
            India
        </td>
        <td>
            27
        </td>
    </tr>
</table>

EDIT:

I earned by putting js code inside document.ready. Thanks @Jose Rui Santos http://jsfiddle.net/Lijo/kw4yU/11/

Another solution is http://jsfiddle.net/Lijo/kw4yU/12/

Another approach: Setting column width - HTML table

+3
source share
9 answers

Use selector +

#myTable tr td {            /* makes all columns red */
    background-color: red;
}
#myTable tr td + td {       /* makes 2nd and 3rd columns yellow */
    background-color: yellow;
}
#myTable tr td + td + td {  /* makes 3rd column blue */
    background-color: blue;
}

demo here

EDIT

CSS . , :

#myTable tr th,
#myTable tr td {
    background-color: red;
}

#myTable tr th + th,
#myTable tr td + td {
    background-color: yellow;
}

#myTable tr th + th + th,
#myTable tr td + td + td {
    background-color: blue;
}

EDIT2

javascript , jQuery

$("#myTable th, #myTable td").each(function (i) {
    $(this).css('background-color', ['red', 'yellow', 'blue'][i % 3]);
});

+8

:

td:nth-child(1){
    background-color: #f00;
}
td:nth-child(2){
    background-color: #ff0;
}
td:nth-child(3){
    background-color: #00f;
}

CSS3, , , JavaScript.

+5

COL.

<style>
table {
    background-color: black;
}
.first {
    background-color: orange;
}
.second {
    background-color: green;
}
.third {
    background-color: pink;
}
</style>

  <table id = "myTable">
  <col class="first" /><col class="second" /><col class="third" />
    <thead>
        <tr>
            <th>
                 Name
            </th>
            <th>
                Address
            </th>
    <th>
                Age
            </th>
        </tr>
    </thead>
    <tr>
        <td>
            Lijo
        </td>
        <td>
            India
        </td>
    <td>
            27
        </td>
    </tr>
</table>
+3

col table

<table id = "myTable">
<col style="background-color: red;" />
<col style="background-color: yellow;" />
<col style="background-color: blue;" />
<thead>
    <tr>
        <th>
             Name
        </th>
        <th>
            Address
        </th>
        <th>
            Age
        </th>
    </tr>
    </thead>
    <tr>
        <td>
            Lijo
        </td>
        <td>
            India
        </td>
        <td>
            27
        </td>
    </tr>
</table>
+2
th,td{background:yellow}
th:first-child,td:first-child{background:red}
th:last-child,td:last-child{background:blue}​

http://jsfiddle.net/thebabydino/kw4yU/3/

IE :

th,td{background:red}
th+th,td+td{background:yellow}
th+th+th,td+td+td{background:blue}​

... HTML, - .

+1

colgroup

<table id="myTable">
  <colgroup span="2" style="background-color:#FF0000;"></colgroup>
  <colgroup style="background-color:#0000FF;"></colgroup>
  <thead>
        <tr>
            <th>
                 Name
            </th>
            <th>
                Address
            </th>
    <th>
                Age
            </th>
        </tr>
    </thead>
    <tr>
        <td>
            Lijo
        </td>
        <td>
            India
        </td>
    <td>
            27
        </td>
    </tr>

</table> 

CSS, colgroups .

+1

td.

HTML: <td class="first">data</td>

CSS

td.first {
    background-color: #00FF00;
}
0

Tables are not built by column, but are built by row. Therefore, in order to make a column-based style, each cell element must have an individual general style applied to it.

Violin

Style

.col1 { background-color: #ffddbb; }
.col2 { background-color: #ddffbb; }
.col3 { background-color: #bbddff; }

HTML

<table id = "myTable">
    <thead>
        <tr>
            <th class="col1">
                 Name
            </th>
            <th class="col2">
                Address
            </th>
    <th class="col3">
                Age
            </th>
        </tr>
    </thead>
    <tr>
        <td class="col1">
            Lijo
        </td>
        <td class="col2">
            India
        </td>
    <td class="col3">
            27
        </td>
    </tr>
</table>
0
source

If you don't want to change your HTML code, you can try:

table tr td {
    background-color: orange;            
}
table tr td+td {
    background-color: yellow;            
}

table tr td+td+td {
    background-color: green;            
}
0
source

All Articles