views:

35

answers:

3

I have a table that is 640px wide seperated in two [TD]'s. The left one is 150px and the right one 490px.

The question is, using CSS or any other method, how do I stop the content from overflowing the size I have set and making the page look like a mess.

I have have tried the css overflow: scroll method and in some cases this works, but not in all. I need something that is going to work everytime. If I could get each TD to scroll if the content is larger that would certainly suffice.

I do not have a link to provide, this is just a general question as I will have many areas on my site that I may need to use something like this.

Thanks

+1  A: 

If you're using tables as backbone of you website, then you do it wrong. You should use div elements instead. Tables should be use for tabular data, not for structure.

Sometimes it's quite hard to get fast the look as when used table, but it's not impossible. For 2-row table you can use something like this

    <div id="container">
      <div id="left">
      <div id="right">
      <div class="clr" >
    </div>

CSS:

#container{
  width: 640px;
}
#left{
  width: 150px;
  flot: left; /*if you want them to be next to each other */
  overflow: scroll; /*or hidden?*/
}
#right{
  width: 490px;
  float: left;
  overflow: scroll; 
}
.clr {
  clear: both;
}
Ventus
+1  A: 

I agree with both answers so far - the ideal solution is to re-code your layout without the table, but if you don't have time, wrapping your table cell content in a <div> will do the trick:

HTML

<table>
    <tr>
        <td class="left">
           <div>This is your left content</div>
        </td>
        <td class="right">
           <div>This is your right content</div>
        </td>
    </tr>
</table>

CSS

table {
    width: 640px;   
}

td div {
   overflow: scroll;  
}   

td.left,
td.left div {
    width: 150px;
}   

td.right,
td.right div {
    width: 490px;
} 

The added <div>'s around your content will respect the CSS overflow property and prevent non-breaking content from blowing up your layout.

Pat
OK, maybe I'm just not good enough at this yet. But think of this like a guestbook, with your name and city on the left and your comments on the right. Each entry also has a heading in a 12px gray bar with the date on the left and a delete link on the right. This is in a PHP while loop. I have never used DIVS in this manor as I still find tables better in these situations as you don't have to predefine all containers, especially if each one you do is a different size - or even different sizes on the same page - which I have.
Theoden
I also tried what you suggeseted before even posting but with no success.Thanks
Theoden
That's odd...if you check out this fiddle http://jsfiddle.net/HCZeS/1/, you'll see it working. Perhaps there's something else in your CSS/markup that's causing the issue.
Pat
I play a little more with your table example and it appears to be working now. Thanks
Theoden
+1 - This, unlike the answers he agreed with that just started rewriting with divs, actually gave an answer in the context of a table, without assuming that Theoden was using a table for layout.
Scott
@Theoden: Not a problem and I wouldn't say no to the 15 points for accepting my answer ;)
Pat
A: 

Since you are using fixed widths, it sounds like you need to set table-layout: fixed on your table element.

Scott