[英]Make absolute div take 100% height of parent
I have a div whose size depends on its content. 我有一个div,其大小取决于其内容。 I want to have an absolute child which takes the whole space.
我想要一个绝对的孩子,它能占据整个空间。
In Firefox I get the wanted result but not in Chromium. 在Firefox中,我得到了想要的结果,但在Chromium中却没有。 How to fix it?
如何解决?
Which browser is rendering against the specification? 哪个浏览器违反了规范?
<div style="position:relative;float:left;"> <div style="position:absolute;display:table;left:0;top:0;height:100%;width:100%;background:red;"> <div style="display:table-cell;vertical-align:middle;">TEST</div> </div> <!-- this is an img in real with unknown size --> <div style="width:200px;height:200px;background:yellow"></div> </div>
You just need to change the display
of this div to display:flex
and use margin: auto
to center the text: 您只需要将此div的
display
更改为display:flex
并使用margin: auto
将文本居中:
<div style="position:relative;float:left"> <div style="position:absolute;display:flex;left:0;top:0;height:100%;width:100%;background:red;"> <div style="margin: auto">TEST</div> </div> <!-- this is an img in real with unknown size --> <div style="width:200px;height:200px;background:yellow"></div> </div>
Or with display: flex
and align-items: center
: 或
display: flex
和align-items: center
:
<div style="position:relative;float:left"> <div style="position:absolute;display:flex;left:0;top:0;height:100%;width:100%;background:red;align-items: center"> <div>TEST</div> </div> <!-- this is an img in real with unknown size --> <div style="width:200px;height:200px;background:yellow"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.