Contact Us

|

FriendFeed FriendFeed

|

follow us on twitter Twitter

|

 Rss Feed

|

Favorites Add to Favorites

Thursday, July 9, 2009

Vertical align text in floating div using css

Do you know how to vertically align a text in a floating div? When a content is dynamically generated and you want to align it automatically according to it?

Today i was experimenting about this for a long time and finally i have got a solution which will work in all the browsers including ie6. There are many ways of doing it, but i would like to work the code in all the majore browsers inculding ie6.

So here is the code...

HTML:
<div class="contentdiv">
<p>It works! Vertically Center Align!</p>
</div>

CSS:
.contentdiv{ width: 135px; height: 84px; display: table; border:2px solid red;}
.contentdiv p{ display: table-cell; vertical-align: middle; text-align: center;}

Hack for ie:
.contentdiv p{*margin-top: expression(this.offsetHeight < this.parentNode.offsetHeight ? parseInt((this.parentNode.offsetHeight - this.offsetHeight) / 2) + "px" : "0");}

According to the height you increase, the content will get vertically aligned center. My only worry is iam using expression. We must try to avoid using more expression. But it looks very simple. Fine, any other method of handle this will be a great help! Please feel free to post.

Labels: , , ,

11 Comments:

Anonymous Anonymous said...

I tested this in IE 7 and it didn't work...

September 10, 2009 at 1:39:00 PM PDT  
Anonymous Les said...

It works in firefox and IE7
Thanks

September 28, 2009 at 12:51:00 AM PDT  
Anonymous gav said...

Hey man, i tested this with the following o/s browsers and it works fine..

PC Vista Fx3.5
PC Vista IE8
PC Vista IE7
PC Vista Chrome 3
PC Vista Opera 10
PC Vista Safari 4
PC XP IE6
PC XP Fx2
MAC Fx2
MAC Safari 3

October 21, 2009 at 7:14:00 PM PDT  
Blogger Madhu said...

For IE 7

*+html .contentdiv {}

April 5, 2010 at 8:50:00 PM PDT  
Anonymous Anonymous said...

Use this simple example. It shows you how to do it whether in a div or a list. And it doesn't use line heights, javascript or expressions = http://www.andy-howard.com/verticalAndHorizontalAlignment/index.html

May 10, 2010 at 3:20:00 AM PDT  
Anonymous Anonymous said...

Do you know how to vertically align a text in a floating div?

Where's the float?

January 21, 2014 at 8:56:00 PM PST  
Anonymous Omega softwares said...

Great - works like a charm, thank you!
Our company do Web Designing in Mumbai and Website Development at very low cost.

December 29, 2014 at 12:21:00 AM PST  
Anonymous Omega Softwares said...

Thank you for this amazing Article! have a nice day............
For more : CMS website development company and Web Designing


December 29, 2014 at 12:54:00 AM PST  
Blogger Omega software said...

This post is very helpful, edifying and in addition cool thoughts on the topic. It's very encouraging to find this site, can't wait to check out new stuff you post. CMS website development company || Web Designing || Website Development

January 7, 2015 at 11:58:00 PM PST  
Blogger Omega software said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck. Website Development in Mumbai || CMS Website Developer || Website Maintenance in Mumbai

January 7, 2015 at 11:58:00 PM PST  
Blogger Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Wordpress Development Company

May 6, 2015 at 6:59:00 AM PDT  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home