在Position:absolute下居中设置

 

在DIV定位中有时候使用的是相对定位,而DIV在Position:absolute情况下的居中需要知道DIV的高和宽;

通过数学我们可以知道,可以首先将DIV的高/宽的定位设置:Left/Top设为50%;然后将Margin-Left/Top:-(宽/高 ÷ 2)即可。

例如:
<div style="position: absolute; 380px; left:50%; top:50%; margin-left:-190px; margin-top:-190px; "></div>
 
原文地址:https://www.cnblogs.com/dashi/p/4034773.html