5

I'm having trouble defining the CSS styles necessary in order to achieve the following layout:

enter image description here

Ideally, I'd like to have the left two divs be of width 200px. div#image will always have a height of 100px. However, I would like div#sidebar and div#mainContent to have lower borders which lie on the same horizontal level. Their sizes should be large enough to contain their respective content, which is determined when the page is being served. Hence, the one with more content will cause the other div to extend down to the same distance.

The problem is that with absolute positioning, the div#sidebar and div#mainContent elements don't seem to acknowledge the flow of their child elements. Perhaps I don't fully understand absolute positioning. Also, it seems like bad form to use Javascript in order to set the inline style of elements on the page. Is there a way of accomplishing this solely with CSS?

I've also tried floating the div#image and div#sidebar, and setting a margin-left property on div#mainContent, but wasn't able to get it to work...

Any help will be much appreciated!

Andrew

Ben
  • 54,723
  • 49
  • 178
  • 224
Andrew
  • 6,144
  • 10
  • 37
  • 54
  • Can you show us your code including the mainContent? – kazinix Jul 12 '11 at 01:53
  • @domanokz: Actually, the code that I was using is really similar to what @Steve posted in his answer below. Unfortunately, I ran into the same problem that he did, in that I couldn't get the bottom of the sidebar to extend down to where the bottom of the mainContent div extended, in the event that the mainContent div was large enough (see @imoda's comment below). Still, I think everything else about @Steve's answer is spot on. It just suffers from one problem... – Andrew Jul 12 '11 at 02:12
  • @Andrew - Javascript is usually bad form to solve a styling problem...there's probably a good CSS solution but I'll be curious to see if anyone finds it. +1 for an interesting question. – Ben Jul 12 '11 at 02:38
  • Obviously this has been attempted before: http://stackoverflow.com/questions/1172088/make-two-floated-css-elements-the-same-height and that lead to some interesting reading (although I didn't read it): http://m.alistapart.com/articles/holygrail/ – Ben Jul 12 '11 at 02:51
  • @Andrew - I suggest you change your layout, look at stackoverflow's sidebar, it's not cool visually, but the content is soooooo cool! If you really want to achieve that layout you can use table which is not recommended! Oops! I just did! Good luck! – kazinix Jul 12 '11 at 02:59
  • Thanks guys, I'm going to continue to read through your answers, and the links Steve posted. I have to take breaks to go to sleep and then to work, and I'm sorry about the delays. I suspect that the html table is probably the way to go. I'll try to put the code together and post it, unless someone does it before me;-) – Andrew Jul 12 '11 at 11:15

5 Answers5

1

demo: http://jsfiddle.net/TRa35/

html

<div id="wrapper">
    <div id="div-image">div image</div>
    <div id="div-maincontent">
        <div id="div-sidebar">
            div sidebar
        </div>
        div maincontent
        <button>click to add content</button>
        <br />
        <span></span>
    </div>
</div>

css

html, body {
    margin:0;
    padding:0; 
}

#wrapper {
    position:relative;
}

#div-image {
    position:absolute;
    left:0;
    top:0;
    width:200px;
    height:100px;
    background-color:#cef;
}

#div-sidebar {
    position:absolute;
    left:-200px;
    top:100px;
    bottom:0;
    width:200px;
    background-color:#efc;
}

#div-maincontent {
    position:absolute;
    left:200px;
    right:0;
    top:0;
    background-color:#fce;
    min-height:300px;
}
kei
  • 20,157
  • 2
  • 35
  • 62
  • Thanks for the reply, kei. The sidebar now seems to extend down to the maincontent div, but I can't get the reverse to hold true (when content is added to the sidebar div). Cool use of positioning, though;) – Andrew Jul 12 '11 at 02:41
  • Hmm... You're basically asking for a `table`. Have a look here: http://stackoverflow.com/questions/6299033/css-hell-simulating-table-with-div It uses an image but it should be close to what you're looking for. – kei Jul 12 '11 at 02:58
1

This almost solves the problem. In fact, to be more precise, it does solve the problem in Google Chrome and Firefox, but IE 9 seems to have problems recognizing the height of cells and/or rows. I can't really mark it as an answer because of this, but I'm just posting it in case anyone can use something from it. It uses an html table element.

CSS:

#mainContentCell
{
    background-color: Blue;
}

#imageCell
{
    width: 200px;
    height: 100px;
    background-color: Yellow;
}

#sidebarCell
{
    background-color: Red;
}

HTML:

<table id="layoutTable" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td id="imageCell">
            Image
        </td>
        <td id="mainContentCell" rowspan="2">
            Main Content
        </td>
    </tr>
    <tr>
        <td id="sidebarCell">
            Sidebar
        </td>
    </tr>
</table>

Also, if anyone can make this work in IE 9, I'll gladly mark their response as the answer.

Andrew
  • 6,144
  • 10
  • 37
  • 54
0

Note: This has been abandoned as "unsolvable", a reasonable answer is given below, but the original problem question remains without a definite solution.


JS Fiddle: http://jsfiddle.net/steve/gHrce/

Does almost everything, may help put you on the right track.

CSS:

#img {
  background:green;
  float:left;
  height:100px;
  width:200px;
}

#sidebar {
  background:red;
  clear:both;
  float:left;
  width:200px;
}

#mainContent {
  background:yellow;
  margin-left:200px;
}

HTML:

<div id='img'>
  IMG
</div>    

<div id='sidebar'>
  SIDEBAR
  <br />
  <br />
  <br />
</div>   

<div id='mainContent'>
  MAIN CONTENT
  <br style='clear:both' />
</div>

That extra <br /> at the bottom forces the height of the main content to whatever the sidebar height is.

The <br /> tags in the sidebar are just to provide some extra height for demonstration purposes.


Of course, you can add Javascript pretty easily to expand the sidebar's height, but it smells strongly of hack:

if($('mainContent').offsetHeight > $('sidebar').offsetHeight + 100) {        
  $('sidebar').style.height = $('mainContent').offsetHeight - 100 + 'px';
}
Ben
  • 54,723
  • 49
  • 178
  • 224
  • OK, but I don't understand why I'm the only one getting up/down votes...none of the solutions posted here work any better or worse. – Ben Jul 12 '11 at 05:42
  • I think that the problem, as it's been stated, is unsolvable. However, the code that I'm going to end up using is similar to Steve's. I'm going to use the same hack and html, with straight up javascript instead of jquery. Floats are a good idea. So I'm going to mark this as an answer, although it should probably be edited to mention that the problem is probably unsolvable. Thank you! – Andrew Jul 14 '11 at 11:04
0

@Andrew for my suggestion, you should use 960 grid system CSS. pls check on this link http://960.gs/ I think you may more easier to develop and maintain.

CMMaung
  • 165
  • 5
  • 11
  • 27
  • I was hoping not to resort to an external framework. I think the question that I posed shouldn't really require one. But thanks for the suggestion. – Andrew Jul 12 '11 at 02:15
  • @Andrew that framework is only CSS code, for example u can use for left sidebar
    for right sidebar
    that's all
    – CMMaung Jul 12 '11 at 02:23
0

Whew! After searching tons of articles. I hope this could help you.

<style type="text/css">
    #wrapper{
    background:blue;
    position:relative;
    min-height:400px;
}

#img{
    background:green;
    position:absolute;
    left:0;
    height:100px;
    width:200px;
}

#some-panel{
    background:orange;
    position:absolute;
    width:200px;
    top:100px;
    left:0;
    bottom:0;
}

#main-content{
    background:yellow;
    margin-left:200px;
}
</style>

<div id="wrapper">

        <div id="img">img img</div>
        <div id="some-panel">some panel</div>

    <div id="main-content">
         main content main
    </div>
</div>
kazinix
  • 28,987
  • 33
  • 107
  • 157
  • This still doesn't work: I blew up body content (yellow) and the sidebar (orange) followed. Then, I blew up the sidebar content past the body, and it didn't follow. :/ – Ben Jul 12 '11 at 05:40