html - css中的中心对象
问题描述
我有这个 HTML 代码:
<div class='container'>
<div class="post-container">
<div class="post-thumb"><img src="../images/logo.png" /></div>
</div>
//A few other thigs
</div>
后容器高于其他东西。
现在我想将 post-thumb div 放在 post-container 中,并将图像放在 post-thumb 中。
但是怎么做呢?
这是CSS:
.post-container {
overflow: auto;
text-align:center;
}
.post-thumb {
width:230px;
height:50px;
overflow:hidden;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-top: auto;
margin-bottom: auto;
}
.post-thumb img {
width:100%;
margin-left: auto;
margin-right: auto;
margin-top: auto;
margin-bottom: auto;
}
.container {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
box-shadow: 1px 1px 5px #ddd;
width: 250px;
height: 100%;
background-color: #2e3233;
}
解决方案
使用弹性盒来居中:
.post-container {
display: flex;
justify-content: center;
align-items: center;
}
.post-thumb {
display: flex;
justify-content: center;
align-items: center;
}
.post-thumb img {
max-width:100%;
}
推荐阅读
- css - 使用自动列使元素跨越网格上的所有列
- java - 有没有办法增加退出过渡的持续时间?
- angular - 从模板中设置表单控件的值
- regex - 正则表达式逐行匹配并在出现特定字符串时停止
- angular - 如何在 Angular 单元测试中对子组件执行方法
- marklogic - 得到“
: VirtualFree() 中的错误:”markLogic 中的相关错误 - python - 使用 spark submit 提交自定义 udf 时输入错误
- r - 在R闪亮中过滤数据框
- vmware-clarity - clr-tree-node 选择不影响折叠的子节点
- python - 尝试通过命令提示符运行 server.py 时出现语法错误