Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align complex multiple div site layout

Tags:

css

center

OK. Here's the deal. I've read quite a few articles on this site and others about vertical centering but they all seem to refer to centering a single div and I haven't been able to apply it correctly to a more complex layout. I'm working on a site which has a header, navigation bar, content area, sidebar, and footer. A mockup of the design can be seen here: mockup

I've got all the divs fitting together nicely thanks to the use of the 0px text trick in the container div and the content & sidebar sit next to each other using display:inline-block. the header, navbar, and footer are horizontally centered using margin-left:auto & margin-right:auto. together this nicely renders the whole site horizontally centered but I can't figure out how to apply vertical centering to the whole site without breaking the design. This is not a fluid layout, all divs have fixed pixel sizes that the content fits into very nicely. It seems that there should be some way to use absolute or relative positioning and percentages to center everything vertically but I can't figure out how to do it. The code for the mockup is attached. Thanks!

<style type="text/css">
<!--
DIV.container {
    position:relative;
    height:100%;
    width:100%;
    display:table;
    text-align:center;
    font-size:0px;
    overflow:hidden;
}
#header {
    width:650px;
    height:87px;
    z-index:1;
    background-color:#C90;
    vertical-align:middle;
    margin-left:auto;
    margin-right:auto;
    font-size:12px;
}
#navbar {
    width:650px;
    height:32px;
    z-index:2;
    background-color:#0FF;
    vertical-align:middle;
    font-size:12px;
    margin-left:auto;
    margin-right:auto;
}
#content {
    width:500px;
    height:265px;
    z-index:3;
    background-color:#33F;
    display:inline-block;
    vertical-align:middle;
    font-size:12px;
}
#sidebar {
    width:150px;
    height:265px;
    z-index:4;
    background-color:#999;
    display:inline-block;
    vertical-align:middle;
    font-size:12px;
}
#footer {
    width:650px;
    height:58px;
    z-index:5;
    background-color:#F69;
    vertical-align:middle;
    font-size:12px;
    margin-left:auto;
    margin-right:auto;
}
-->
</style>
</head>

<body>
<div class="container">
<div id="header">Header</div>
<div id="navbar">Navbar</div>
<div id="content">Content</div>
<div id="sidebar">Sidebar</div>
<div id="footer">Footer</div></div>
</body>
</html>
like image 873
managerharry Avatar asked May 22 '26 21:05

managerharry


2 Answers

You need to put a container around the whole part that you want vertically centered, and you need to know the height. Then you give it an absolute position that is 50% from the top and give it a margin-top of minus half the height.

So if your container is 400px high you would use the following css:

#container {
    position:    absolute;
    top:         50%;
    margin-top: -200px;
}

In your case your 'container' is 442px high, so change this css:

DIV.container {
    position:relative;
    height:100%;
    width:100%;
    display:table;
    text-align:center;
    font-size:0px;
    overflow:hidden;
}

To this:

DIV.container {
    position:absolute;
    top:50%;
    margin-top:-221px;
    width:100%;
    display:table;
    text-align:center;
    font-size:0px;
    overflow:hidden;
}
like image 165
Kokos Avatar answered May 24 '26 09:05

Kokos


Your stylesheet can be much cleaner/smaller.

See this demo fiddle.

And if you don't want a scroll bar when the browser window becomes small, then add overflow: hidden to the body, see this fiddle. But that's NOT a tip in the light of usability.

like image 31
NGLN Avatar answered May 24 '26 10:05

NGLN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!