html - 在另一个 div 中居中多个 div
问题描述
我想在一个 div 中水平和垂直居中 3 个 div。我不能使用 flexbox,因为后来更多的 div 和 flexbox 会影响这些新的 div,但我不知道如何垂直居中
<div class="hero">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
.hero {
width: 100vw;
height: 100vh;
text-align: center;
}
.box {
width: 250px;
height: 350px;
background: red;
margin: 50px;
display: inline-block;
}
解决方案
在将其撞到一半之后,仍然可以通过将其推高一半来使其居中:
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
更新:(H优先):
html, body {
height: 100%;
}
.hero {
text-align: center;
align-items: center;
vertical-align: middle;
justify-content: center;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box {
width: 250px;
height: 350px;
background: red;
margin: 5px;
top: 50%;
position: relative;
display: inline-block;
}
推荐阅读
- ios - swift 5 TextBox 值在标签中显示
- amazon-web-services - 带有traefik的ec2-instance前面的aws WAF
- javascript - 文件上传正在使用 Postman,但不适用于从浏览器运行的 Javascript
- reactjs - Expo App:任何导航器都未处理“TOGGLE_DRAWER”操作
- excel - 如何拆分逗号分隔值?
- javascript - 一个函数在我用 Postman 测试时有效,但在我的客户中不起作用
- python - 使用 Azure Container Registry python API 列出图像标签
- node.js - 如何使用api从数据库中获取数据?
- python - 有没有一种方法可以找到 SymPy 中定义的函数的独立变量?
- python - 如何绘制networkx图的节点子集