使用 Flexbox 布局实现 div 垂直居中
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.father {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
align-items: center;
/* 垂直居中 */
justify-content: center;
/* 水平居中 */
}
.child {
width: 100px;
height: 100px;
border: 1px solid blue;
}
</style>
<body>
<div>
<div class="father">
<div class="child"></div>
</div>
</div>
</body>
</html>
讯享网
使用 position 属性和 transform 属性实现垂直居中:
讯享网.father { width: 200px; height: 200px; border: 1px solid red; position: relative; } .child { width: 100px; height: 100px; border: 1px solid blue; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
使用 position 和 margin:auto 实现
.father { position: relative; width: 200px; height: 200px; border: 1px solid red; } .child { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 100px; height: 100px; border: 1px solid blue; }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/52902.html