tags:

views:

163

answers:

5
+3  Q: 

CSS column layout

Hi peeps.

I'm going round in circles with a CSS layout. I basically want it like:

<-------><-------------->
         <------><------>
  400px    50%      50%

So its 3 colums, one fixed size, and the other two taking up 50% each of the remaining space. I cant seem to make the second and third take up 50% of the remaining space.

Any help would be greatly appreciated, thanks very much :)

A: 

First of all it's always a good practice to WRAP your layout. In the following example:

+---------------BODY-----------------+
|<---DIV#1---><--------DIV#2-------->|
| <---DIV3--> | <--DIV4--><--DIV5--> |
| |         | | |        |         | |
| |         | | |        |         | |
| |         | | |        |         | |
| |         | | |        |         | |
| |         | | |__________________| |
| |_________| | |____CLEAR DIV_____| |
+-----------------------------------+
  • DIV0 : main-wrapper
  • DIV1 : sidebar-wrapper
  • DIV2 : content-wrapper
  • DIV3 : sidebar-content
  • DIV4 : content-left
  • DIV5 : content-right
  • CLEAR DIV: to clear the floats.
kuroir
auch. Someone needs a strong cure for their <a href="http://csscreator.com/?q=divitis">divitis</a>...
peirix
friggin' HTML markup not working. Just click here: http://csscreator.com/?q=divitis (waiting 30 seconds to let you know I messed up the link (I'm sorry (Can I post now?)))
peirix
First of all div3 is for segmenting the sidebar blocks or widgets. Div4 and div5 are there because the user wanted that.div1 and div2 are the wrappers.and the clear div is for the float.So wtf?
kuroir
Why would you want to wrap a single DIV in another DIV? What does div1 do, that can't be done to div3? and you wouldn't need the clear div in this instance, since div2 would occupy the rest of the width. but i'm glad you at least removed "main-wrapper" div. (:
peirix
Div3 is the "module-widget" div, it can be duplicated to make blocks etc. Div1 usually is to be able to select the sidebar as a whole, for example to hide the sidebar.
kuroir
A: 

Your best bet would be to have it set up like so:

Wrappers are pretty important and make a lot of things much easier. To center things inside of wrappers, you can set margin left/right of the divs inside of the wrappers to "auto".

<div class="bigwrapper">
 <div class="400pxdiv">
  Content for 400pxdiv
 </div>
 <div class="rightwrapper">
  <div class="50percent1">
   50percent1's content
  </div>
  <div class="50percent2">
   50percent2's content
  </div>
 </div>
</div>
Sneakyness
The problem here is getting the "Other Wrapper" to take up the remaining space, when you don't know how big that will be.
peirix
I tried something like this...I made the 400px div float:left then the other wrapper filled the remianing space. leaving the two small divs to take 50% of the other wrapper but they wanted 50% of the entire row including the 400px div.
Sure you will, considering bigwrapper is going to be some sort of width and what's left is going to be 400 pixels smaller than what would normally be there.
Sneakyness
Why are you floating it to the left that makes no sense. You've made me so angry I had to go code up a real working example for you. I'll be back in 5. Count on it.
Sneakyness
@Dawson: you need margins. see my answer.
geowa4
haha made you angry? made me literally lol there Sneakyness
+1  A: 

the markup:

<div id="left">some content</div>
<div id="main">
    <div>more content</div>
    <div>still more content</div>
</div>

the css:

#left {
    float: left;
    width: 400px;
    margin-right: -405px; /* throwing in a little extra */
}

#main {
    margin-left: 405px; /* matching the margin of #left */
}

#main > div {
    width: 50%; /* may need to make it 49.9% for some browsers */
}
geowa4
That's actually pretty close, but you don't need the margin-right for the left div. Nice solution, though!
peirix
@peirix: #left should have a negative margin so it has no width.
geowa4
+4  A: 

I tried a couple of variations. Below works in Chrome 2, Firefox 3.5 and IE8:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;html&gt;
<head>
<title>NLR</title>
<style type="text/css">
html, body, div { margin: 0; border: 0 none; padding: 0; }
div { height: 500px; border-collapse: collapse; }
#wrapper { padding-left: 400px; }
#nav { width: 400px; margin-left: -400px; float: left; background: yellow; }
#main { overflow: hidden; background: blue; }
#left { float: left; width: 50%; background: red; height: 300px; }
#right { float: right; width: 50%; background: green; }
</style>
</head>
<body>
<div id="wrapper">
<div id="nav"></div>
<div id="main">
  <div id="left"></div>
  <div id="right"></div>
</div>
</div>
</body>
</html>
cletus
you don't need to float #left and #right if you specify width of 50% for each. floating can actually have some bad consequences since they no longer have any height (being floated).
geowa4
Doesn't work for me without the float. So I think it needs to be there.
peirix
@peirix: try putting the widths to 49.9% or slightly smaller. browsers can be stupid sometimes.
geowa4
Set them to 40%, and still not positioned next to each other as expected.
peirix
A: 

This works for me in Firefox.

<!DOCTYPE html>
<title>Test</title>
<style>
  #A { width: 400px; float:left; outline: thin solid pink; }
  #B { margin-left: 400px; overflow: hidden; outline: thin solid pink; }
  #B1, #B2 { width:50%; float:left; outline: thin solid pink; }
</style>
<div id=A>
  A
</div>
<div id=B>
  <div id=B1>
    B1
  </div>
  <div id=B2>
    B2
  </div>
</div>
Ms2ger
perfect thanks!
Well, it works ONLY in Firefox... IE7 does not display the borders, and Opera doesn't stretch B1 and B2, it just right aligns it...
awe
the border issue is because he didn't specify borders in the CSS; he used outlines, which are a different thing, and not supported in IE6/7.
Val