1、居于文档中间

如果让一个元素居于html文档中间,可以使用如下代码:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">

<title>测试</title>
<style>
html,body{
height: 100%;
}
.center-content {
position : absolute;
top: 50%;
left:50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="center-content" style="width: 20%;height: 20%;background-color: black;">
</div>
</body>
</html>

这个方法的局限是不能用于下级元素的居中布局,例如要让元素在一个div中居中,这个方法就不适用。