Forum Moderators: not2easy

Message Too Old, No Replies

Two Columns, Revisited

         

adrian_s

9:10 am on Jul 5, 2003 (gmt 0)

10+ Year Member



I have recently been working on updating a table-based layout to a CSS box-based one. This has been almost effortless, except for one (relatively large) element of the page: a two-column layout.

As it is, I have a table with one row and two cells. In each of these, I have a DIV that extends only as far as the content does and has a border and background. The right one fills whatever the left one doesn't (which, in turn, can expand to fit what it must). Everything is happy.

I have examined several ways to create a two-column layout.

  • The float-and-content trick. This involves creating a floating div and following it with some content that will appear to the right (in this case) of the floater. The problem here is that the floater only pushes text out of its way; the border will remain unaligned. Text beyond the bottom of the floater also is not pushed to the side.
  • The left-and-right floaters. Two divs are created, one floating left and the other right. They are given widths of 50%. The problem, of course, is that there is no way to control the sizes of the columns (with one constricting as small as it can and the other expanding as it can).
  • Absolute positioning and the left-margin trick. Messes up the flow of the page. I don't know how wide the left column will be at any given time (a problem in both) and I don't know what will be above the columns (a problem in the former).

So, finally, my question:

What way has anyone found to create two side-by-side columns, one of which takes most of the page, and the other of which expands to fit what it contains, while considering that each must have an individual border?

My greatest congratulations and thanks to whoever solves this conundrum.

dmorison

10:16 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Adrian,

I use 3 columns (left,separator,right), float everything left, and make the middle one (separator) just a few pixels wide and 100% height.

Might not have understood the problem totally though...

<html>

<head>

<style type='text/css'>

.left
{
float: left;

padding: 10px;

border: 1px solid #000;
}

.separator
{
float: left;

height: 100%;

width: 2px;
}

.right
{
float: left;

padding: 10px;

border: 1px solid #000;
}

</style>

</head>

<body>

<div class='left'>

<h1>Left</h1>

</div>

<div class='separator'>

</div>

<div class='right'>

<h1>Right</h1>

<h2>foo bar foo bar foo bar foo bar</h2>

<h2>bar foo bar foo bar foo bar foo</h2>

</div>

</body>

</html>

adrian_s

11:10 am on Jul 5, 2003 (gmt 0)

10+ Year Member



Thanks, dmorison, this is very nearly what I'm looking for. However, you may notice that if you extend the content in the right column (or shrink your browser window), the entire box wraps over to the next "line" (below the "left" column).

(On another note, this may just be my user-agent. For others, does this example render with the right column expanded across the screen even when the content does not do so? This may be what is intended, and I'm missing it due to buggy software.)

I continue in my quest...

adrian_s

10:53 am on Jul 6, 2003 (gmt 0)

10+ Year Member



It's been killing me that I can't find a solution to this problem. It seems like there should be an easy solution, but I am definitely not seeing it. A conclusion I have reached:

Floats don't seem like the right way to go. In the float-and-normal-stuff case, floats are meant to be items wrapped around by text (inline elements), not boxes themselves. In the float-and-float case, it's difficult (impossible?) to get floats to span the page without explicitly declaring percentages because floats are just meant to be wrapped around — they're not meant to acquire the screen as the actual content of the page.

To use floats for what they're not intended for seems in the same class of sin as using tables "incorrectly", not to mention that they don't work.

For this reason, I'm switching to strange little table system controlled by CSS:


<table><tr>
<td class = "dominated">
smaller column<br />
expands as needed
</td>
<td class = "dominator">
larger column, by default, takes up all space not used by the "dominated" column and, therefore, has room for wrapping text
</td>
</tr></table>

... with the stylesheet:


td.dominated {
white-space: nowrap;
width: 0px;
}
td.dominator {
white-space: normal;
width: 100%;
}

I feel silly doing this, but it's the most efficient way to attain a consistent and pretty result.