CSS & HTML - Written by admin on Wednesday, March 19, 2008 10:09 - 1 Comment
Vertical align div
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.

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=”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.
1 Comment
10 best CSS hacks « Share4Vn.com Blog
Leave a Reply
Most Popular Content
- Kopete, The KDE Instant Messenger
- Warning Signs You Can’t Ignore
- Robotic Jellyfish Swim and Fly at Hannover Fair
- The Real Indiana Jones
- Popular Wealth - Free Photoshop Brushes To Download
- 5 Ways To Combat Aging
- Inflatable robots could explore Mars
- Thinking the Way Animals Do
- Top 10 things to do in a blackout
- The most natural drug
- 30 Websites to follow if you’re into Web Development
- Please,I want you to help me with some of your books that will enable me to keep...
- You should also mention Ubercart (http://www.ubercart.org) a very powerful Drupa...
- Hello i am interessted on this script please can you contact me!?...
- Excellent article and oh so true.
Thanks...
- Well done.
If you use Ubuntu for, say, half a hour, you'll never go back to W...
- I own a "Super Battery" chinesse mobile phone, Chivak78, and I can assure you th...
- Thank You Very MUCH!!!
I had a working key, but the last time I reinstalled, it...
- Is this big deal end yet ?...
PHP, Tip & Tricks - May 30, 2008 3:20 - 0 Comments
Reuse Excel business logic with PHPExcel
More In Code
- 8 Best Open Source Shopping Carts
- 10 best CSS hacks
- Fetching a Web Page From Your PHP Code
- Make fancy buttons using CSS sliding doors technique
- A Guide to Cryptography in PHP
Photoshop Tips - May 31, 2008 8:20 - 0 Comments
Popular Wealth - Free Photoshop Brushes To Download
More In How to ?
- An A-Z Index of the Windows XP command line
- How I built an electricity producing Solar Panel
- Popular Wealth - Free Photoshop Brushes To Download
- Ultimate Boot CD
- In Search of the Red Demon
Wordpress - May 31, 2008 3:34 - 0 Comments
Thinking the Way Animals Do
More In I LIKE IT !
- Seven Kingdoms Conquest
- Vantage Point (2008)
- Top +20 Free and open-source software is good for you
- The Simpsons Movies
- WinterBells - Noel

[…] http://about.share4vn.com/2008/03/19/vertical-align-div/ […]