记录下用到的小知识点。 块元素垂直居中常用在:弹出框需要在页面上垂直居中等。
<body> <div class="parent"> <div class="child"></div> </div> </body>
body, html { width: 100%; height: 100%; } .parent { position: relative; width: 100%; height: 100%; } .child { position: absolute; left: 50%; top: 50%; -webkit-transform: translate(-50%,-50%); transform: translate(-50%,-50%); width: 500px; height: 300px; background: #ddd; }