使用position、margin-top、margin-left搞定,margin-top为块元素所占盒子大小(包括元素大小,border大小,padding大小)的一半大小,前面加上“负号”,margin-left同理
代码:
<html>
<head>
<style type="text/css">
body{margin: 0px;}
.floats{position: absolute;top: 0;left: 0;width: 100%;height: 100%;background: #000;
opacity: 0.5;}
.alert{width: 400px;height: 200px;background: #fff;border: 2px solid yellow;position: absolute;top: 50%;left: 50%;
margin-top: -100px;margin-left: -200px;}
</style>
</head>
<body>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
内容内容内容内容内容内容内容内容内容内容内容内容内容</br>
<div class="floats"></div>
<div class="alert"></div>
</body>
</html>
效果: