【发布时间】:2020-05-08 00:48:47
【问题描述】:
在下面的 html 中,我希望 txt-box div 在容器中居中,覆盖图像,然后展开以填充容器。它应该在所有边上都有一个等宽的边距,允许图像的一部分显示为粗边框。
显示的 html 可以满足我的需要,只是随着浏览器窗口大小的调整,垂直边距与水平边距总是略有不同。
我觉得我这里有一个 hack,我错误地使用了flex-grow。我了解flex-grow 允许txt-box div 扩展,因为它是唯一具有增长值的元素。如果我能解决这个问题,我应该能够简单地在txt-box 上设置一个边距,它应该可以工作。
我对@987654326@ 有什么不明白的地方?
.container {
display: flex;
align-items: center;
justify-content: center;
border: solid 2px red;
position: relative;
}
.container img {
width: 100%;
flex-grow: 0;
flex-shrink: 0;
}
.txt-box {
background-color: white;
display: flex;
padding: 5px;
border: solid 2px blue;
flex-grow: 1;
position: absolute;
width: 90%;
height: 80%;
}
<div class="container">
<img src="blocks.png" />
<div class="txt-box">
hello world
</div>
</div>
【问题讨论】: