CSS div wrapping question

techfuzz

Diamond Member
Feb 11, 2001
3,107
0
76
To make this as simple as possible, let's say I have a table with one row and column. The table is stretched to the width of the page. The table has just one cell and in it there are two div elements. Basically it looks like this:
<table style="width: 100%;">
<tr>
<td>
<div style="white-space: nowrap;">Long item that stretches most of the way across the page</div>
<div style="float: right; white-space: nowrap;">Another item that takes up most of the rest of the page</div>
</td>
</tr>
</table>

Now, when the width of the browser window is wide enough, everything fits into a single line. If you shrink the browser window, the second div will eventually wrap under the first and create a second line. I don't want it to create a second line; I want one line and then the horizontal scrollbar to appear when the browser window width gets too small.

Without hard-coding a static width for the table, tr, or td elements, how do I prevent the second div from wrapping to a second line?

techfuzz
 

techfuzz

Diamond Member
Feb 11, 2001
3,107
0
76
Originally posted by: Crusty
Don't use that table. Tables are not for layout control.
Thanks, but I didn't ask about when it was ok to use tables. I asked how to prevent the second div from wrapping under the first?

techfuzz
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: techfuzz
Originally posted by: Crusty
Don't use that table. Tables are not for layout control.
Thanks, but I didn't ask about when it was ok to use tables. I asked how to prevent the second div from wrapping under the first?

techfuzz

The problem is that your table elements are controlling the displaying of your divs. So matter what styling you put on your divs your table row is controlling the wrapping. You should instead wrap your two content divs with a container div with the appropriate styling.
 

brikis98

Diamond Member
Jul 5, 2005
7,253
8
0
Not sure what the best approach is, but you can try a few things:

1. try adding overflow: auto to the containing td, or wrap the two divs in another div and use overflow: auto on it.
2. Surround the two divs with a <nobr> tag
3. If the divs themselves don't have to stretch to fit the whole page, you can put a fixed width on their surrounding td (or again, wrap them in a div).
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Originally posted by: Crusty
Originally posted by: techfuzz
Originally posted by: Crusty
Don't use that table. Tables are not for layout control.
Thanks, but I didn't ask about when it was ok to use tables. I asked how to prevent the second div from wrapping under the first?

techfuzz

The problem is that your table elements are controlling the displaying of your divs. So matter what styling you put on your divs your table row is controlling the wrapping. You should instead wrap your two content divs with a container div with the appropriate styling.
Not to get into the whole table vs divs thing, in essence, what you suggest he do is exactly what he already has, and have still avoided providing an answer.

To the OP, I don't think you can do what you want to without specifying a fixed width.

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: jjones
Originally posted by: Crusty
Originally posted by: techfuzz
Originally posted by: Crusty
Don't use that table. Tables are not for layout control.
Thanks, but I didn't ask about when it was ok to use tables. I asked how to prevent the second div from wrapping under the first?

techfuzz

The problem is that your table elements are controlling the displaying of your divs. So matter what styling you put on your divs your table row is controlling the wrapping. You should instead wrap your two content divs with a container div with the appropriate styling.
Not to get into the whole table vs divs thing, in essence, what you suggest he do is exactly what he already has, and have still avoided providing an answer.

To the OP, I don't think you can do what you want to without specifying a fixed width.

My point is that divs are easier and more flexible to style with then tables, so changing his "wrapping table" to a div will give him a simpler layout and will allow him to style it so he won't have his problem. The reason I didn't give a simple answer is because I can't tell you off the top of my head the correct syntax to achieve the styling he wants, but I can tell you that using a table will keep the OP's head banging against the wall.

 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Why not just put the second div in a different <td> element?



<table style="width: 100%;">
<tr>
<td>
<div style="white-space: nowrap;">Long item that stretches most of the way across the page</div>
</td><td>
<div style="float: right; white-space: nowrap;">Another item that takes up most of the rest of the page</div>
</td>
</tr>
</table>

 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Originally posted by: PhatoseAlpha
Why not just put the second div in a different <td> element?



<table style="width: 100%;">
<tr>
<td>
<div style="white-space: nowrap;">Long item that stretches most of the way across the page</div>
</td><td>
<div style="float: right; white-space: nowrap;">Another item that takes up most of the rest of the page</div>
</td>
</tr>
</table>
Actually, this is the easiest way except just get rid of the divs altogether:

<table style="width: 100%;">
<tr>
<td style="white-space: nowrap;">Long item that stretches most of the way across the page</td>
<td style="white-space: nowrap; text-align: right;">Another item that takes up most of the rest of the page</td>
</tr>
</table>
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Try adding a float: left; in your first DIV. That's the only way a browser is going to know that both are supposed to be on the same line.
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
That's not really the way you are supposed to do things, but this works:

<table style="width: 100%;">
<tr>
<td>
<div style="float: left; width: 50%">Long item that stretches most of the way across the page</div>
<div>Another item that takes up most of the rest of the page</div>
</td>
</tr>
</table>

Assign whatever width you want to the first float. You are experiencing "drop", which is when there isn't enough room for both on the same line, the second element will drop below the first.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Originally posted by: drebo
Try adding a float: left; in your first DIV. That's the only way a browser is going to know that both are supposed to be on the same line.

Unfortunately, that won't help him. Floated elements will still drop down instead of forcing a scroll bar if the width isn't available.

This kind of horizontal layout really only works worth a damn in CSS if you've got fixed everything. If anything is flexible, tables it is.
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
Originally posted by: PhatoseAlpha
Originally posted by: drebo
Try adding a float: left; in your first DIV. That's the only way a browser is going to know that both are supposed to be on the same line.

Unfortunately, that won't help him. Floated elements will still drop down instead of forcing a scroll bar if the width isn't available.

This kind of horizontal layout really only works worth a damn in CSS if you've got fixed everything. If anything is flexible, tables it is.

Fook tables if you want to use CSS. You can do it correctly with percentages, but otherwise you will end up with the drop problem. Floating works just fine, but you have to use it correctly.

 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
And to do what he wants it to do, what exactly is the method that doesn't involve tables?
 

NiKeFiDO

Diamond Member
May 21, 2004
3,901
1
76
your example always stays on the same line for me. what aren't you showing us?
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
Originally posted by: PhatoseAlpha
And to do what he wants it to do, what exactly is the method that doesn't involve tables?

Put them each in a div, use negative margins, etc.

 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Originally posted by: PhatoseAlpha
And to do what he wants it to do, what exactly is the method that doesn't involve tables?

You'll need a wrapper div around the two divs set to a static width (among other things).

This is the major failing of CSS, in my opinion, and one that makes it incredibly cumbersome to use for layouts.

Well, that and each browser/version renders the same code differently. But that's another debate.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Not exactly like your situation, but my personal website uses a liquid three column CSS layout with no tables, just divs. They stretch to fill up the screen and when the window is resized, shink until a minimum width is reach and then the scrollbars appear.

If that is what you're looking for, Google "liquid two column css layout". That's the site I got my three-column one for.