tags:

views:

38

answers:

2
<div id="wrapper">
  <div id="header">...</div>
  <div id="main">
    <div id="content">...</div>
    <div id="sidebar">...</div>
  </div>
</div>

#wrapper { min-width: 900px; }
#main { display: table-row; }
#content { display: table-cell; }
#sidebar { display: table-cell; width: 250px; }

The problem is that the sidebar isn't always at the right-most part of the page (depending on the width of #content). As #content's width is variable (depending on the width of the window), how to I make it so that the sidebar is always at the right-most part of its parent?


Ex.

Here's what I have now:

<---  variable window width   ---->
 ---------------------------------
| (header)                        |
 ---------------------------------
 [content]  | [sidebar] |
            |           |
            |           |
            |           |
            |           |
            |           |
            |           |

And here's what I want:

<---  variable window width   ---->
 ---------------------------------
| (header)                        |
 ---------------------------------
 [content]           | [sidebar] |
                     |           |
                     |           |
                     |           |
                     |           |
                     |           |
                     |           |

Please let me know if you need anymore information to help me with this issue. Thanks!

PS - I know I can accomplish this easily with floats. I'm looking for a solution that uses CSS tables.


SOLUTION:

#wrapper { min-width: 900px; }
#main { display: table; table-layout: fixed; }
#content { display: table-cell; }
#sidebar { display: table-cell; width: 250px; }

No need to declare a table-row element, thanks to anonymous table elements. Thanks!

A: 

Set the parent to position:relative. Then set the sidebar to position:absolute and snug it to the right.

So....

#main{
position:relative;
}

#sidebar{
position:absolute;
right:0px;
}

That should keep your sidebar snug to the right of main, regardless of how wide content is.

Brant
-1: what if the main isn't as high as the sidebar, what happens to the footer? Also, OP wants tables. If anything worked, he'd use floats.
ANeves
He didn't say anything about the sidebar needing to be higher than the main content, nor did he say anything about a footer. But thanks for the -1, griefer.
Brant
+2  A: 

You would need to set the style display: table (or inline-table) on a wrapper div around #main in order for the other table display types to make sense; it's undefined what happens if you put rows inside something that's not a table.

Then on that wrapper you'd have to also set table-layout: fixed; to make the browser actually respect the widths you specify (in the first table-row, if you don't have explicit columns). Otherwise you get the auto table layout algorithm second-guessing you. Finally add width: 100%; to avoid shrink-to-fit.

I'm looking for a solution that uses CSS tables.

Any reason for that, or is this just an exercise? CSS tables aren't a good choice today due to poor browser support and in the end they don't really get you anything over just using a table. CSS positioning would seem to be the better way to go for simple layouts like this.

bobince
Thanks for the suggestion; I'll play around with it and let you know how it works for me. About the CSS tables comment: I like future-proofing my sites, and a little more work today saves me that much more in the future. "In the end", the do honor semantic HTML markup and make for simplified stylesheet rules: I think that's reason enough right there. The only major player that doesn't currently support them is IE7 all other releases of currently used browsers can render them just fine. Gotta push that envelope if things are going to change at all!
neezer
Slightly-amended solution appended to my original question. Thanks!
neezer