CSS & HTML - Written by admin on Wednesday, March 19, 2008 10:09 - 1 Comment

Vertical align div

Tags: , ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

One of current CSS left outs is vertical align div. And before CSS 3 comes we have to use some tricks to solve this problem. I looked over for some solutions and it all comes up to defining it as table and using vertical align. The second one appears a lot and it’s a nice solution, but it has two faults, I’ll tell you later about them, here’s the code first.

Idea is to place absolute div 50% left and top and then to move margin left and top half if it’s size.

Vector people

.wrapper {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
Problem?

This works only with defined height and when div height is less then browser height so you can’t have scroll. Now you’ll ask why do anyone want vertical align when there’s a vertical scroll? Well for example you have container height 820px and you have it vertically centered in all larger resolutions, simple.

Solution

In this problem (when browser height is less than div height) at smaller resolutions 1/3 of div is not visible, it’s in negative top margin. So idea is to place some relative div that will prevent div to go into negative margin. Here is the code.

<div id=”shim”/></div>
<div id=”wrapper”>
Content
</div>html, body {
height: 100%;
margin: 0;
padding: 0;
}

* {
margin:0px auto;
padding:0;
}

div#shim {
visibility: hidden;
width: 100%;
height: 50%;
margin-top: -200px;
float: left;
}

div#wrapper {
width: 1000px;
height: 400px;
clear: both;
background:red;
position: relative;
top: -200px;
/* IE4ever Hack: Hide from IE4 **/
position: static;
/** end hack */

}

/* Hide from IE5mac \*//*/
div#shim {
display: none;
}

html, body {
height: auto;
}
/* end hack */

/* ]]> */

Only fault is again defined height and it can’t be solved with CSS, not right now, I hope new version will or some JS fix.

Read more:
»» Most used CSS tricks
»» 10 best CSS hacks
»» Make fancy buttons using CSS sliding doors technique
»» Make fancy buttons using CSS sliding doors technique
»» picotux - the smallest Linux Computer in the World


1 Comment

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

10 best CSS hacks « Share4Vn.com Blog
Mar 19, 2008 10:11

Leave a Reply

Comment

Most Popular Content

PHP, Tip & Tricks - May 30, 2008 3:20 - 0 Comments

Reuse Excel business logic with PHPExcel

More In Code


Photoshop Tips - May 31, 2008 8:20 - 0 Comments

Popular Wealth - Free Photoshop Brushes To Download

More In How to ?


Wordpress - May 31, 2008 3:34 - 0 Comments

Thinking the Way Animals Do

More In I LIKE IT !