background top

Fixed cell widths in tables

TableWe recently ran into a situation where we needed multiple tables to appear as a single table.  To make sure each column lined up, we needed to set the width of each cell to a set pixel amount.

The first attempt added a width to the <td> as follows:

<td style="width: 150px">some content goes here</td>

However, by nature, table cells will always resize to accommodate the content that is placed in them. That is what they were designed to do. The cell will maintain its 150 pixel width as long as the content is smaller than the specified width.

The solution is to add a div inside each cell. It adds some extra markup, but will ensure that the cell will not resize beyond the specified width. Divs can be set to specific dimensions, and will stay as such even if the content will not fit inside.

So our table cell code now becomes:

<td><div style="width: 150px">some content goes here</div></td>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • TwitThis

Leave a Reply