How to use <col> tag correctly and is it supported in all browser?
Asked Answered
T

2

6

What is the use of <col> tag and is it supported in all browser?

I was trying <col style="background:red"> but it's not working. I'm on Firefox 3.

<table width="100%" border="1">
  <col style="background:red">
  <caption> Table Caption </caption>
Trig answered 11/6, 2010 at 16:38 Comment(0)
O
5

It works, but it doesn't affect the caption. Working example:

<html>
<head>
<title></title>
</head>
<body>

<table>
  <caption>test</caption>
  <col style="background:red" />
  <col style="background:blue" />
  <tr>
    <td>red</td>
    <td>blue</td>
  </tr>
  <tr>
    <td>red</td>
    <td>blue</td>
  </tr>
</table>

</body>
</html>

The tag is defined in HTML 4.01 (1997) and not deprecated in XHTML, so it should be supported (as far as the user interface allows) by every current browser on any platform.

Optimal answered 11/6, 2010 at 16:45 Comment(7)
+1 Thanks Guffa and what is the difference between col and colgroup? and like from <col> we can select any column of table ? is there a way to select row of table?Trig
@metal-gear-solid: The colgroup element can be used for a group of col elements, where you can specify attributes that apply to all columns in the group, and then override some attribute on some columns. To specify attributes for rows, you simply apply them to the tr elements, and the only grouping available is the thead, tbody and tfoot elements.Optimal
thanks for reply - so like with col we can select any column , there is no option in HTML to select any row. what if i want to select some specific rows inside <tbody>Trig
@metal-gear-solid: You can put a class in the tr elements and define the style for them in a style sheet. Otherwise you have to add the same attributes to each tr element.Optimal
The xhtml example above is slightly incorrect. The <caption> must come before any <col> or <colgroup> elements.Veridical
@pgoetz: Thanks for spotting that, I corrected the code. Note that the standard doesn't specify the position of the caption element in relation to col or colgroup elements, but rather that it has to be the first element in the table.Optimal
I've been spending a lot of time staring at the xhtml strict/transitional DTD's. If you look at the DTD, the required ordering is clear. How this is implemented in browsers is anyone's guess.Veridical
I
2

It is used in tables to set default attributes for all the cells in a column or a group of columns.

The col tag is defined in an ordered way and can be part of a HTML colgroup tag or not.

It is supported in all browsers.

Izanami answered 11/6, 2010 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.