How to vertically center a div for all browsers?

Answer

Below is the best all-around solution build to vertically & horizontally center a fixed-width, flexible height content box. Tested and working for recent versions of FF, Opera, Chrome, & Safari, and MSIE 6+.

HTML

<div class="outer">
<div class="middle">
<div class="inner">

<h1>The Content</h1>

<p>Once upon a midnight dreary...</p>

</div>
</div>
</div>
CSS

.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}

.middle {
display: table-cell;
vertical-align: middle;
}

.inner {
margin-left: auto;
margin-right: auto;
width: /*whatever width you want*/;
}
To accommodate for IE 7 & older, use a separate style sheet with these changes:

<!--[if lte IE 7]><link rel="stylesheet... /><![endif]-->;
.outer {
display: inline;
top: 0;
}

.middle {
display: inline;
top: 50%;
position: relative;
}

.inner {
display: inline;
top: -50%;
position: relative;
}

All css Questions

Ask your interview questions on css

Write Your comment or Questions if you want the answers on css from css Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---