Tom
Tom

Reputation: 199

Website layout advice

I'm working on a website that fits perfectly in the browser window. Below is a basic blueprint of the website layout:

Website example So far, the Red area is just display:block. The Green area is also display:block with margin-right:200px. The Blue areas(nested in a div) is float:right.

So I've got the width sorted. It's the height I need advice on. The Red and Dark Blue areas are a set height, but I need the Green and Light Blue areas to fit the height of the browser window view.

I'm trying to use box-sizing, but it exceeds the height of the window view because it's extending to the max height of the window. Sorry for my poor explanation. Any advice if would be excellent. Thank you!

Upvotes: 2

Views: 307

Answers (5)

Brigand
Brigand

Reputation: 86250

This is kinda the legacy version of C-Link's answer.

jsFiddle and fullscreen

This has the limitation of any content falling below one page-full falling outside of its container (you can see if you scroll down in the fiddle, but not on the fullscreen).

Make sure our page stretches to its full height.

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

Set a static-height header.

header {
    height: 101px;
    background: red;
}

Create a box for everything under the header. You were on the right track with the box-sizing. We can add padding to it, in the same amount as our header. Then percentages inside it work nicely.

.content {
    position: absolute;
    box-sizing: border-box;
    padding-top: 111px;
    padding-bottom: 10px;
    top: 0; left: 0;
    height: 100%; width: 100%;
}

We float our aside (may or may not be the correct element, depending on contents) and set some styles on it.

aside {
    float: right;
    width: 20%;
    height: 100%;
    padding-bottom: 111px;
    box-sizing: border-box;
}

.top {
    height: 100px;
    background: blue;
}

.bottom {
    margin-top: 10px;
    height: 100%;
    background: skyblue;
}

This is our main, large, content area, which we float to the left. The width could be specified exactly if we wanted exact padding at the cost of additional HTML.

[role="main"] {
    width: 78%;
    background: limegreen;
    height: 100%;
    float: left;
    box-sizing: border-box;
}

You can also set overflow-y: auto on our main or aside elements, to have them scroll when they run out of space. There should also be mobile styles for this page that remove the floating, absolute positioning, absolute styling, and widths should be nearly 100%.

Upvotes: 2

giorgio
giorgio

Reputation: 10212

A pretty common trick is to give the green (and light blue) box absolute positioning, a padding AND a negative margin. Because 100% width is relative to the containing box (could be a parent div, or just the window itself) this is not suitable. When the header was a relative height, say 10%, it would've been easy. The padding makes sure the content will not disappear behind the header, the negative margin puts the box back in place. Don't forget the z-index (otherwise the content (green part) will overlap the header).

The css looks like this:

.header { position: absolute; width: 100%; height: 100px; background: red; z-index: 1; }
.content { position: absolute; width: 100%; height: 100%; padding: 100px 0 0; margin-top: -100px; background: green; z-index: 0; }

The fiddle looks like this: http://jsfiddle.net/2L7VU/

Upvotes: 0

null
null

Reputation: 3517

Could you not just give the divs a max or min height depending on their purpose?

I use a main container or wrapper div that the others would be contained in, that div is then my effective page or screen area.

<div id="wrapper">
    <div id="content">
        <div id="sidebar">
        </div>
    </div>
 </div>

#wrapper{
    min-height: Whatever value you want here;
    max-height: Whatever value you want here;
}

It might be a good idea to set up your page using main container divs, hot only for the content but for the header and footer as well.

As an example, I have a main wrapper that is the whole page, within that is the header div, the content div, the nav div and the footer div. These are the main ones. Everything else can then be contained within them.

So, you can set the layout out using percentages so you have a fluid design that'll react to each browser size. The other elements will then 'fit' inside the main divs and be constrained to them. You may need to look into positioning etc but this is certainly the direction you should head towards.

<div id="wrapper">
<div id="header">Header Here including any divs to be contained within this space</div>
<div id="content">All content etc here</div>
<div id="nav">This is your sidebar</div>
<div id="footer">Footer, as per header</div>
</div>

Then use the css to re deisgn the above layout focusing only on those main divs. Use % instead of px to maintain fluidity.

#wrapper{
width: 100%;
height: 100%
}
#header{
width: 100%;
height: 20%
}
#content{
width: 70%;
height: 60%;
float:left;
}
#nav{
width: 30%;
height: 60%;
float:left;
}
#footer{
width: 100%;
height: 20%
}

Upvotes: 0

ikromm
ikromm

Reputation: 523

you can always set the green box height to the window height minus the red box height.

accordingly the light box height to the window height minus the (red box height + the dark blue box height)

Edit 1: I haven't mentioned that has to be done with javascript.

Edit 2: Consider any paddings and margins too.

Upvotes: 0

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85565

For green div set height: calc(100%-{red-div-height}); and for the light blue div set height: calc(100%-{dark-blue-div-height}-{red-div-height});

Upvotes: 4

Related Questions