Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical align and valign not working on table!

Tags:

html

css

I am dynamically generating a table using AJAX. The structure of the table to be populated is as follows:

<table id="foobar"  style="width:100%">
    <thead>
        <tr>
            <th style="width:20%;"></th>
            <th style="width:55%;"></th>
            <th title="widget name">Name</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

The cell data contains either:

  • an image
  • a div wrapper with anchor tags and paragraphs

I tried the following:

  1. setting valign="top" (separately) at the table, th and tr levels - it had no effect
  2. setting style="vertical-align: top;" (separately) at the table, th and tr levels - it had no effect

I don't want to set the align property at the cell level because it will cause too much unnecessary bloat if the table contains several (say hundreds of) rows.

How can I force a table to vertically align its cell contents to top (bearing in mind that the cells contain block elements?

like image 430
oompahloompah Avatar asked Apr 13 '11 09:04

oompahloompah


People also ask

How do you vertically align text in a table?

Highlighted text, right clicked in the table and chose Table Properties > Cell > Vertical Options > Bottom.

Is valign attribute of table?

The HTML <table> align Attribute is used to specify the alignment of the table and its content. Note : This attribute is not supported by HTML5. Attribute Values: left: It sets the left align to the table.

Does text-align work on tables?

We use the CSS property text-align, to center align text in table cells. We use inline, internal style sheet for text alignment in table cells. The text-align property of CSS sets the alignment of the content in <th> and <td>. By default, the content of <th> are center-aligned and the content of <td> are left-aligned.


2 Answers

Here is a basic table example with TH and TD tags where the content is set to "vertical-align:top".

Demo: http://jsfiddle.net/HAQ3s/472/

th, td {
    vertical-align: top;
}
<table>
    <tr>
        <th>A<br /><br /></th>
        <th>B</th>
        <th>C</th>
    </tr>
    <tr>
        <td>1<br /><br /></td>
        <td>2</td>
        <td>3</td>
    </tr>
</table>
like image 133
wdm Avatar answered Oct 19 '22 00:10

wdm


valign should work no matter what if all settings that we see here are those that are actually used.

Global CSS settings

But I suspect a different gunman here. Did you check your global CSS file what it defines for TH elements? Maybe that's what's giving you headaches.

like image 26
Robert Koritnik Avatar answered Oct 19 '22 01:10

Robert Koritnik