I researched this a bit and from what I found, you have four options:
Version 1: parent div displaying as a cell table
If you don't mind using display:table-cell in your parent div, you can use the following options:
.area{ height: 100px; width: 100px; background: red; margin:10px; text-align: center; display:table-cell; vertical-align:middle; }
Live demo
Version 2: parent div with display and display of table of contents table
.area{ height: 100px; width: 100px; background: red; margin:10px; text-align: center; display:block; } .content { height: 100px; width: 100px; display:table-cell; vertical-align:middle; }
Live demo
Version 3: parent div is floating and content div is as display cell table
.area{ background: red; margin:10px; text-align: center; display:block; float: left; } .content { display:table-cell; vertical-align:middle; height: 100px; width: 100px; }
Live demo
Version 4: Parent div position relative to absolute content position
The only problem I came across with this version is that you seem to have to create css for each specific implementation. The reason for this is because the content of the div must be set to the height that your text will fill, and the top border will be inferred from this. This problem can be seen in the demo. You can make it work for each scenario manually by changing the height% of your div content and multiplying it by -.5 to get the maximum margin value.
.area{ position:relative; display:block; height:100px; width:100px; border:1px solid black; background:red; margin:10px; } .content { position:absolute; top:50%; height:50%; width:100px; margin-top:-25%; text-align:center; }
Live demo
Josh Mein Jun 10 '12 at 12:40 2012-06-10 12:40
source share