Monday, February 21, 2011

How can I set vertical-align to all cells in a table by setting the table element's style?

I would like to have all cells in a with "vertical-align:top" style. For technical reasons outside my control, I cannot define a new class, or change the stylesheet at all; nor can I set "style" for individual cells, or even rows. All I can do is set the "style" attribute of the <table> element itself.

Using <table style="vertical-align:top"> fails -- apparently, it sets the alignment of the table within its own context, not of individual cells inside it. Is there any other alternative that I'm missing?

From stackoverflow
  • No, as far as I can see there is no way to do this without some form of access to either the td itself or a style sheet.

    Can you apply a workaround like this one? It's not pretty and invalid according to the W3C, but should nevertheless work in all browsers:

    <style type='text/css'> table.topalign td { vertical-align: top } </style>
     <table class="topalign"> 
      .... 
    
  • Set the "classic" HTML valign attribute on the table. The value will be inherited by the cells.

    <table valign="top">
      …
    

    See Tables, Table formatting by visual user agents, Horizontal and vertical alignment in the HTML 4 spec.

0 comments:

Post a Comment