gpt4 book ai didi

html - 容器元素扩展到其位置 :absolute contents 的大小

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:48 25 4
gpt4 key购买 nike

在这个标记中:

<div id="A">
<div id="B">
</div>
</div>

如果“#A”有 position:relative 而“#B”有 position:absolute,似乎“#A”想要折叠到零大小,不管“#B”的实际大小如何。如何让“#A”扩展到其内容的大小?

最佳答案

你不能用 CSS 做到这一点,你需要 JavaScript 来设置父容器的 widthheight(#A) .

您可以使用 .offsetHeight.offsetWidth

两者都返回实际的width/height + padding-leftpadding-right/ padding-toppadding-bottom + border-left-widthborder-right-width/border-顶部宽度border-bottom-width


在下面的示例中,我为父容器 (#A) 提供了一个 backgroundImage 以及 widthheight 与子容器相同,并将子容器的 (#B) backgroundborder 的 alpha 值设置为 0.5 更好地解释它。

var a = document.getElementById('A');
var b = document.getElementById('B');

a.style.height = b.offsetHeight + 'px';
a.style.width = b.offsetWidth + 'px';
a.style.backgroundImage = 'url(http://www.lorempixel.com/' + b.offsetWidth + '/' + b.offsetHeight + ')'
#A {
position: relative;
}
#B {
position: absolute;
background: rgba(210,180,140, 0.5);
width: 300px;
height: 100px;
border: 5px solid rgba(210,0,0, 0.5);
padding: 10px;
}
<div id="A">
<div id="B">
</div>
</div>

您可以在这里阅读更多相关信息:http://kingori.co/minutae/2013/05/relative-parent-absolute-children/

关于html - 容器元素扩展到其位置 :absolute contents 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com