虽然都是绝对定位,但是跟绝对定位加负边距的那种居中方式不一样,这种绝对定位的居中方式是将上下左右都设置为0;
代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>居中</title>
<style type="text/css">
.container {
/*使行内元素或者行内块级元素水平居中*/
text-align: center;
height: 400px;
background-color: #ccc;
position: relative;
}
.center-p {
background-color: yellow;
width: 100px;
height: 80px;
margin:auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="center-p">
table-cell居中
</div>
</div>
</body>
</html>