目前能想到的实现水平垂直居中的方法:
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
/*方法一:让子元素相对父元素进行left:50%,top:50%绝对定位,然后通过css3的translate对元素本身进行微调偏移,实现真正的水平垂直对齐*/
#container{
width: 200px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
transform:translate(-50%,-50%);
background-color: red;
}
#content{
width: 50px;
height: 50px;
position: absolute;
left: 50%;
top: 50%;
transform:translate(-50%,-50%);
background-color: blue;
}
/*方法二:通过flex设置父元素下的子元素对齐方式为水平垂直对齐即可*/
/*html,body{
margin: 0;
padding: 0;
height: 100%;
display: flex;
justify-content:center;
align-items:center;
}
#container{
width: 200px;
height: 100px;
background-color: red;
display: flex;
justify-content:center;
align-items:center;
}
#content{
width: 50px;
height: 50px;
background-color: blue;
}*/
/*方法三:父元素设置行高和水平对齐,子元素设置垂直对齐*/
/*#container{
width: 200px;
line-height: 100px;
background-color: red;
text-align: center;
}
#content{
width: 50px;
height: 50px;
display: inline-block;
background-color: blue;
vertical-align: middle;
}*/
</style>
</head>
<body>
<div id="container">
<div id="content"></div>
</div>
</body>
</html>