编写css让一个已知宽高的div元素水平居中?垂直居中

让一个已知宽高的div元素水平居中

<style>
#div1{
200px;
height:200px;
background:#F00;

margin:0 auto;
}

</style>
</head>

<body>
<div id="div1">

</div>
</body>
</html>

让一个已知宽高的div元素垂直居中

<style>

#div1{
300px;
height:200px;
background:#F00;
position:absolute;
top:50%;

margin-top:-100px;

}

</style>
</head>

<body>
<div id="div1">

</div>
</body>
</html>

原文地址:https://www.cnblogs.com/qinxuemei/p/3980241.html