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: , , ,

21 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 Unknown 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  
Anonymous Web Development India said...

This is interesting great coding technique.. Thanks

June 16, 2015 at 10:35:00 PM PDT  
Blogger Suseela said...



There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me.


SEO Company in Chennai

June 13, 2016 at 5:36:00 AM PDT  
Blogger Unknown said...

Thanks for your post Tempat Tidur Anak

August 2, 2016 at 3:32:00 PM PDT  
Blogger Suseela said...

What an awesome post, I just read it from start to end. Learned something new after a long time.


SAP SD Training in Chennai

August 18, 2016 at 3:26:00 AM PDT  
Blogger Maani kamili said...

I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies. Could you please extend them a little from next time? Thanks for the post..

Web development company

August 24, 2016 at 11:58:00 PM PDT  
Blogger Unknown said...

Thank you for having taken your time to provide us with your valuable information relating to your stay with us. This blog help me in attaining more knowledge in the right way. Thank you for your post

Best Seo Services in Chennai

August 25, 2016 at 2:52:00 AM PDT  
Blogger Unknown said...

This comment has been removed by the author.

September 25, 2016 at 5:41:00 AM PDT  
Blogger Unknown said...

Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision



September 25, 2016 at 5:43:00 AM PDT  
Blogger Unknown said...

I appreciate your effort for providing this useful information with us. Keep sharing such useful information with us


Mumbai web designers | website maker in Mumbai | Mumbai website developers

January 6, 2017 at 9:49:00 PM PST  
Blogger shruti said...

We are also providing best mobile solutions, web design and development, ecommerce solution, SEO.
Check out us here

Web Design company in Hubli | web designing in Hubli | SEO company in Hubli

May 22, 2017 at 10:47:00 PM PDT  
Blogger Unknown said...

Hi,

Thanks for sharing a very interesting article about Vertical align text in floating div using css. This is very useful information for online blog review readers. Keep it up such a nice posting like this.

Regards,
WondersMind,
Web Design Company Bangalore

October 30, 2018 at 2:17:00 AM PDT  
Blogger Anuj Shah said...

Thanks for sharing this article ...It's Very nice information and very helpful...
Are you looking for Mumbai website developers. Don't worry we are here to help you out with our best capabilities.
Our main aim is to provide incredible and world-class services to our clients.

April 3, 2020 at 5:55:00 AM PDT  
Anonymous Anonymous said...

Digital transformation company
hire android developers
iOS app development companies

April 6, 2020 at 10:02:00 AM PDT  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home