HTML

Setting a maximum width for a table

June 29th, 2004

I often seem to need to create tables that shrink and grow in width within a minimum and maximum limit. This should be easy with CSS, using the max-width table property. However, this isn't supported in IE, so I used to use some fiendishly complex ways of achieving this.

The minimum width bit is simple. Just put something in the table that can't break. For example, you can use a 1x1 pixel gif image, stretched to the desired width.

It's the maximum width bit that's the difficult part. However, the following bit of code shows how to achieve this easily. I found this somewhere and have unfortunately forgotten where it came from. So apologies for not crediting the original author.

<html>
<head>
  <style type="text/css">
  .MaxWidthTable {
    width: 100%;
    max-width: 500px;
    width: expression(document.body.clientWidth > 500 ? "500px" : "100%" );
    border: solid black 1px;
  }
  </style>
</head>

<body>
  <table class="MaxWidthTable">
    <tr>
      <td>Contents of table here ...</td>
    </tr>
  </table>
</body>
</html>

Fill the table up with content and check out how it expands to 500 pixels wide and then stops. The CSS uses the max-width property, but also uses an expression to say: IF the width is more than 500 pixels THEN make the width 500 pixels ELSE make the width 100%.

Leave a comment

 

Stop using tables for layout

April 18th, 2004

I've always thought that using tables for laying out a Web page was an irritating and ugly workaround. CSS offers a more palatable solution. There are still problems, however, but these sites show some of the possibilities: www.bluerobot.com/web/layouts – The Layout Resevoir glish.com/css – Look Ma, No Tables

Leave a comment



^ back to top ^

Page 1 of 11