0

I have two elements on a page. One being the main content container and the next one a sidebar. What I am trying to do is have it so that the sidebar is aligned to the far right ALL the time - meaning on the right of the screen no matter the screen resolution and the content container should be more or less in the middle of the page.

I have a main container that holds them both.

First thing that I tried was using absolute positioning on the sidebar but this didn't work as when the resolution changed the sidebar would overlap the content.

For interests sake the content box is 632px & the sidebar is 272px.

So next thing I tried was floating each of these elements to the right & using a margin-left "percentage" of 10%. This obviously stopped the overlapping; however now when the resolution gets smaller to the point that it can't contain the content element, the margin & the sidebar the content element now drops below the sidebar content.

So I was wondering if there was anyway that I could make it so the screen would get bigger and utilize horizontal scroll bars instead of making the content drop below the sidebar?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Brett
  • 19,449
  • 54
  • 157
  • 290
  • I'm not sure I understand what you're trying to do. But you can always use overflow:scroll if you want to have an element of a set size with a scroll. You should probably put both content and sidbar in one main containing div too. – AR. Jun 14 '11 at 18:27
  • I do have both elements in a containing div. Tried what you suggested but it didn't work. – Brett Jun 14 '11 at 19:46
  • padding-left:227px; the content container would solve the overlap problem when using absolutes as the solution. – Wayne Jun 14 '11 at 20:39
  • @Wayne..... I just replied to your answer but it got deleted? I tried your solution but it didn't help the overlapping and also created horizontal scrollbars even when not needed.... had to disable the `width` on the `content` element to get rid of them. – Brett Jun 14 '11 at 20:46
  • I deleted it because it was down voted. People are mean here today - I'll go away for awhile ... * {padding: 0px;margin: 0px;} cures the extra width. The padding trick is used on many wordpress themes to allow the sidebar to not overlap the content and overlaps the padding instead. – Wayne Jun 14 '11 at 20:54
  • Hmmmm.... didn't seem to work for me. :( – Brett Jun 14 '11 at 20:58
  • Wex - parent is set to 100% width - padding is used on the sibling content div leaving room for the absolute sidebar. – Wayne Jun 14 '11 at 21:06

3 Answers3

1

In order to implement my solution to your problem, you're going to need to modify your HTML a little:

<div id="container">
    <div id="main">
    ...
    </div>
</div>
<div id="sidebar">
   ...
</div>

CSS:

* { margin: 0; padding: 0; } /* Or your favorite CSS reset to ensure that your body/html tag has no margin or padding */
#container { float: left; width: 100%; margin: 0 -272px 0 0; }
#main { float: right; width: 632px; }
#sidebar { float: right; width: 272px; }

I created a fiddle to show you what I've posted (Note that I modified the values so that it would fit in the tiny preview screen).

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Wex
  • 15,539
  • 10
  • 64
  • 107
  • I see that the fiddle you made works, however after making the changes you suggested my content just seems to sit on top of one another. If I disable the `width` on the `#container` the `#main` floats to the left side of the screen. – Brett Jun 14 '11 at 19:37
  • Hmm, that should not be the case. What do you mean sit on top of one another? Are you referring to the content inside the `main`? – Wex Jun 14 '11 at 20:13
  • Check this.... http://jsfiddle.net/53LPb/1/ -- The #addWrapper doesn't seem to want to show up. – Brett Jun 14 '11 at 20:17
  • Just seen your new comment. What I mean is that both the `#main` and `#sidebar` are floated all the way to the right and the `#sidebar` is sitting on top of the `#main.` – Brett Jun 14 '11 at 20:19
  • 1
    Two possible reasons. First reason is that you've set `margin: 0 75px 0 0;` instead of `margin: 0 -75px 0 0;`. Second reason is that any class that has the word **ad** in it is a candidate to be blocked by any AdBlocking addon that you might have installed on your browser. Try renaming the class, or disabling the adblocker (after fixing the margin problem), and see if that solves your problem. – Wex Jun 14 '11 at 20:21
  • Ahh good catch. As for the `75px` setting and not `-75px`; I was simply screwing around trying to figure out why it wasn't showing and forgot to put it back. But yes, disabling adblock on jsfiddle did the trick..... already have disabled it on my localhost however so it isn't causing problems there. Ok, here we go..... I have tried to incorporate all the css I am using for the 3 elements: http://jsfiddle.net/53LPb/2/ – Brett Jun 14 '11 at 20:34
  • Hmm, you're right. If you add `margin: 0 115px 0 0;` to `#posts`, it will shift it over the 115px that you need it to move. – Wex Jun 14 '11 at 20:54
  • Good suggestion. I ended up going with `margin: 0 33% 0 0;` & that seems to make things work great. – Brett Jun 14 '11 at 21:08
  • The 115px might be more exact, but it's good that you at least have it working now. – Wex Jun 14 '11 at 21:11
  • Yeah..... thing is though I want the content area to have more of a margin between the sidebar & itself. Taking all resolutions into account I thought a percentage was best to use; however I only worry about now not having ENOUGH of a margin and ending up with the sidebar overlapping the content. Don't think there is a way to set a `minimum margin` is there? – Brett Jun 14 '11 at 21:48
1

Your question is a little ambiguous; but i think the simplest solution is to wrap both the main content and the sidebar in a wrapper and give the wrapper a fixed width. Float the main content left and the sidebar right, and voila! Note that this will display the same on pretty much any screensize and wont' resize to fit, but CSS3 Media Queries are helpful for resizing issues.

Alternatively, you can just use percentages, which handles resizing windows just fine, but doesn't give you the fine control over the dimensions. To get the best of both worlds, you either need JavaScript or, as I pointed out above, media queries, so that you can dynamically resize/re-layout elements based on browser window size.

The other solution is to use tables, which are somewhat frowned upon for use in site layout.

Community
  • 1
  • 1
Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
  • Well that's the main point. I don't want to use a fixed width as I want the sidebar located at the right of the SCREEN no matter the resolution; so it HAS to be able to expand. – Brett Jun 14 '11 at 19:09
  • @Brett that's why i mentioned Media Queries; assuming your browsers support CSS3, you can use Media Queries to style items differently depending on the size of the browser window. I'll see if i can get something working without them though. – Thomas Shields Jun 14 '11 at 19:11
  • Thanks. Didn't really want to go down the CSS3 path for this though if I could avoid it. – Brett Jun 14 '11 at 19:39
1

If I'm understanding you correctly, this ought to do it:

CSS:

/* Pseudo CSS reset */
* { padding:0; margin:0; }

#containerOuter {
    position:absolute;
    width:100%;
    height:100%;
}
#containerInner {
    display:table;
    width:100%;
    height:100%;
}

#hiddenSidebar {
    display:table-cell;
    width:272px;
}

#contentOuter {
    display:table-cell;
    background-color:#999999;
    min-width:632px;
    height:100%;
}

#contentInner {
    margin-left:auto;
    margin-right:auto;
    background-color:#888888;
    width:632px;
    height:100%;
}

#sidebar {
    display:table-cell;
    width:272px;
    min-width:272px;
    background-color:#777777;
}

HTML:

<div id="containerOuter">
    <div id="containerInner">
        <div id="hiddenSidebar"></div>
        <div id="contentOuter">
            <div id="contentInner">Content</div>
        </div>
        <div id="sidebar">Right Sidebar</div>
    </div>
</div>
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
BCDeWitt
  • 4,540
  • 2
  • 21
  • 34
  • I don't really see the point of using `display:table` and `display:table-cell` on divs instead of just using a table. – Thomas Shields Jun 14 '11 at 19:13
  • 1
    @Thomas Shields Tables are to be used for data that actually belongs in a table. It's a problem with what the word "table" implies to humans or search engines that are reading the HTML code. Here, we are saying to style the page like a table, not insert a table. It works just the same, really. From what I've seen, it's strictly a semantics issue. – BCDeWitt Jun 14 '11 at 19:23
  • @Thomas Shields Although, there is one advantage to doing it this way instead. Because the layout is separated from the HTML, the entire look and feel of the web page can be changed by simply modifying a separate CSS file. If you have many pages based on the same CSS file, you could change the layout of many pages at once rather than changing all of the table elements in every HTML file individually. Much more convenient :-) – BCDeWitt Jun 14 '11 at 19:37
  • @BDawg Thanks but that's not going to work as it's got a set width of 904px for the containing div. The sidebar needs to ALWAYS be at the farmost right of the SCREEN no matter the resolution. – Brett Jun 14 '11 at 19:42
  • @Brett Updated my code. See if that's what you are looking for. – BCDeWitt Jun 14 '11 at 20:02
  • @BDawg your right that using CSS to table-stylize it gets rid of peoples's concerns with tables; but I do think semantics are important and wouldn't *personally* do this. I don't have any *professional* qualms with this though, I just don't think its the best semantic solution. – Thomas Shields Jun 14 '11 at 20:05
  • @Thomas Shields With HTML5 elements, it makes a bit more sense than this example. I didn't use them here since I don't know if Brett is dealing with HTML5 at all but new elements such as aside, header, footer and section make more sense to me semantically than a table. To each their own, though. It's not like using a table in the HTML is very difficult to understand, either. – BCDeWitt Jun 14 '11 at 20:37
  • @BDawg Yes, i understand why you used this answer. I was just noting that I like to use more semantic methods (like HTML5, as you say) than tables. And you're right, tables are a quickfix, and I've no issue with whoever wants to use them. :) – Thomas Shields Jun 14 '11 at 20:41
  • @Thomas Shields Totally agree. HTML5 is great :-) – BCDeWitt Jun 14 '11 at 20:52
  • @BDawg Thanks for the edit. I'll give it a go if nothing else works. :) – Brett Jun 14 '11 at 21:05