Search code examples
htmlxhtmlcolgroup

How to center XHTML (and/or HTML4) TABLE columns by colgroup?


How to align all columns by colgroup? It works with colspan?

Example

This HTML here was tested with Firefox and Chrome, but no browser renderize the center for all expected columns.

  <table border="1" width="100%">
    <colgroup>
      <col style="text-align:center;background-color:red"/>
      <col align="center" valign="bottom" style="background-color:blue"/>
      <col align="center" valign="top" style="background-color:yellow"/>
    </colgroup>
    <tr>
      <th>ISBN</th>
      <th>Title</th>
      <th>Price</th>
    </tr>
    <tr>
      <td>3476896</td>
      <td>My first HTML</td>
      <td align="center">$53</td>
    </tr>
    <tr>
      <td><big>5869207</big></td>
      <td>My first CSS</td>
      <td><small>$49</small></td>
    </tr>
  </table>

Use this example (copy/paste to) at w3schools.com/tags.

PS: What is wrong with align and valign attributes? Style (by text-align) also not responding.


EDIT

As I said above, I need a solution "by colgroup". It can be also "by colgroup or col tags with style attribute".

My template system need to use colgroup (!), not is valid a solution without colgroup. My system not need to compatiple with HTML5, it uses something like "XHTML module" (see ex. DTD).

Related questions

  • Is html <COL align> deprecated? : not the same, because my problem is about XHTML, not about HTML5 (that is not XML and is a "plan for future standard").

Solution

  • Well, thanks for all answers and clues. My conclusion, about colgroup, is

    1. The HTML must be standard (XHTML1.0, XHTML1.1 or HTML4.X) compliant;

    2. ... but only one browser (Opera) is standard compliant.
      (MS-IE have no "standard compliant" tradiction, we can ignore IE7 surprising case)

    "How to center the columns by colgroup?": following the standards instructions... So, my HTML code (at this question introduction) was right all the time! My mistake was wanting to see it at any web-browser!


    Some "correct questions" are (examples):

    • Why another browsers not implemented the colgroup standard behaviour? At @ns47731's answer we see some clues. Perhaps web-browswer developer are expecting HTML5 and not XHTML2. See also @Alohci comment below.

    • Why HTML5 and XHTML2 proposals diverge about colgroup? No clues at answers... My supposition: XHTML2 and HTML5 will be not 100% compatible.

    • Can I negociate with my "template system developer" (a XSLT developer) to add this "XHTML1 standard compliant feature"? :-) Please help me in a lobby for PMC Article Previewer.