4

I'm trying to use <div> objects and CSS to emulate the appearance of frames for a project that I'm working on. Using the following code I was able to properly stretch the list on the left with a border-right, padding, and (if I choose) a background using only one extra element:

HTML:

<div id="sidebar">
    <div id="sidebar-content">
        <!-- content goes here -->
    </div>
</div>
<div id="content">
    <!-- more content -->
</div>

CSS:

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#sidebar {
    float: left;
    height: 100%;
    width: 200px;
    overflow: auto;
    border-right: solid 1px #000;
}

#sidebar-content {
    margin: 10px;
    padding: 0;
    list-style: none;
}

#content {
    position: relative;
    float: left;
    padding: 10px;
}

This worked well until I tried adding another element at the top of the content which stretched horizontally. Here's the current code:

HTML:

<div id="content">
    <div id="criteria">
        <!-- select boxes -->
    </div>
    <!-- other content -->
</div>

CSS:

#criteria {
    position: absolute;
    left: 0;
    right: 0;
    background: #FF9;
}

This picture shows the results

Results of the above CSS and HTML

I tried adding the following rule:

#content {
    width: 100%;
}

although this stretched the #content div to the width of the body element, not the body minus the sidebar - so the content appeared below the fold (and beneath the sidebar on the left)

How can I use CSS to stretch the criteria box to fill the content area horizontally?

EDIT -

I wanted to upload a picture of what happened after Karl's recommendation:

Remove the float: left from #content. If there is a floated element next to a normal block element, the block element will fill the remaining space. Also don't set the width attribute.

Here's what happened when float: left was removed

Results of the above recommended fix

Close, however now the #criteria is stretching to cover up the sidebar. Other suggestions?

stevendesu
  • 15,753
  • 22
  • 105
  • 182

3 Answers3

4

Remove the float: left from #content. If there is a floated element next to a normal block element, the block element will fill the remaining space. Also don't set the width attribute.


Edit:

To address the issue of #criteria which is absolutely-positioned forcing itself over to the left, you can add a left-margin to #content to account for the width of the sidebar, as Steven discovered.

As a random sidenote, this would also allow you to keep the content aligned in the case that the sidebar did not take 100% height like it does in Steven's example.

Karl
  • 6,035
  • 5
  • 30
  • 39
  • I've updated the question with a screenshot of what happened after your proposed solution. As you can see, it sort of worked - but not perfectly. With `#sidebar` floating and `#content` not, the sidebar actually floats on top of content, so content's width is 200px greater than you would hope. – stevendesu Aug 05 '11 at 20:20
  • Shortly after updating the question the answer came to me, with help from your solution. With the content block stretching over the sidebar I applied `margin-left: 200px` to `#content` and that was that. If you adjust your answer accordingly then I can accept it. – stevendesu Aug 05 '11 at 20:24
  • http://jsfiddle.net/m5XLB/ What i cant quite understand is, that it's working fine in this fiddle it's not overlaying to the left & that's without your proposed 200px margin fix, and out of curiosity why do you need it absolute positioned ? – Xavier Aug 05 '11 at 20:38
  • It's likely a side effect of `#criteria` being absolutely positioned with `left: 0`. I'll update my answer. – Karl Aug 05 '11 at 20:41
  • @Xavier: Your fiddle isn't doing it cause the `#content` div in your fiddle still has the original CSS properties (floated and position relative) – Karl Aug 05 '11 at 21:18
  • @Xavier: To answer your question, I needed `#criteria` absolutely positioned so that it would stretch to fill the screen horizontally – stevendesu Aug 06 '11 at 14:14
  • You just made my day! I had been searching for solution for about two hours and thanx tu you i found it. THNX A LOT –  Oct 19 '11 at 14:38
1

I've used something like this before to get a sidebar layout

sidebar css

    width: 200px;     z-index:2;     float: left;

content css

    margin-left: 200px;     z-index: 1;     width: 100%;     padding-right: 2

You can use a similar technique to get a header <div> too.

I might not have got the css exactly correct, but the idea is to use a combination of margin, padding and z-index to get a frames-like effect.

Antony Scott
  • 21,690
  • 12
  • 62
  • 94
0

One option would just to have the sidebar and content areas' width based on percentage... say 20% for the sidebar and 80% for the content. I've run into this problem before and just gone with that, but I didn't do too much research on it.

Cassidy Laidlaw
  • 1,318
  • 1
  • 14
  • 24
  • While that would work, it doesn't meet my initial criteria: I wish to emulate frames. use a common applications which uses frames (such as phpMyAdmin) and you'll notice the sidebar never changes as you change the size of the window. Furthermore, I'd eventually like to apply a background image to the sidebar so I really need to keep the pixel dimensions exact. – stevendesu Aug 05 '11 at 20:22