Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we set color in a col tag? [closed]

Tags:

html

css

w3c

I've just ran into a use case where it would be useful to style a col tag: enter image description here

In this particular case I wanted to set the color of the "Post Name" column but it turns out that it doesn't work or it is not allowed. But background-color is allowed though which makes me wonder why color is not.

I've checked the spec (http://www.w3.org/TR/html5/tabular-data.html#the-col-element) but I couldn't find any useful information. I know how to do the same effect with CSS only, I just wonder if anyone knows the reason why this happens to be like that?

Example: http://jsfiddle.net/nunoarruda/d8d7rans/

like image 714
nunoarruda Avatar asked Dec 11 '14 19:12

nunoarruda


People also ask

What is the use of Col tag in HTML?

The <col> tag specifies column properties for each column within a <colgroup> element. The <col> tag is useful for applying styles to entire columns, instead of repeating the styles for each cell, for each row.

What is the correct HTML tag for column?

<col>: The Table Column element. The <col> HTML element defines a column within a table and is used for defining common semantics on all common cells. It is generally found within a <colgroup> element.


2 Answers

Take a look at the diagram at CSS 2.1 spec, 17.5.1 Table layers and transparency

Columns are a layer in the rendering. so backgrounds of the layers above show through to the columns layer by default. Text on the other hand only applies to the cell, so only it can set the text color. Text color inherits by default though, so the cell can acquire its text color from an ancestor element. Col elements are never ancestors of cells.

The properties for which columns' properties apply are listed at CSS 2.1 spec, 17.3 Columns

like image 52
Alohci Avatar answered Oct 17 '22 06:10

Alohci


If you can use CSS3:

table tr td:nth-child(2) {
    color: #CC0000;
}

That sets the color of every second <td> in a <tr>.

like image 2
Ted Avatar answered Oct 17 '22 06:10

Ted