Этот код (demo) позволяет для любого width
и height
быть установлены без необходимости обновлять какие-либо другие свойства (например, top
= height/2
) или полагаться на методы, которые Арен» т хорошо поддерживается (например, display:table;
Один недостаток является поддержка старых версий IE не хватает Комбинируя это с другим решением для IE только, вероятно, вы лучше всего
The CSS:...
.absoluteCenter {
/* Must manually set width/height */
width:800px;
height:500px;
/* The magic centering code */
margin:auto;
position:absolute;
top:0;bottom:0; /* Aligns Vertically - Remove for Horizontal Only */
left:0;right:0; /* Aligns Horizontally - Remove for Vertical Only */
/* Prevent div from overflowing main window */
max-width:100%;
max-height:100%;
overflow:auto;
}
/* IE 7 and Below */
:first-child+html .absoluteCenter,
* html .absoluteCenter {
/* Place code here to override all above values, and add IE friendly centering */
}
И HTML:
<div class="absoluteCenter">
Content of DIV
</div>
Нужно ли вам поддерживать ie7? – mrtsherman
Я не очень волновался за поддержку ie7, хотя ie8 было бы неплохо. –