gpt4 book ai didi

html - 为什么内部DIV会溢出外部DIV?

转载 作者:太空狗 更新时间:2023-10-29 14:45:44 26 4
gpt4 key购买 nike

我已经远离 HTML 和 CSS,找不到这个简单问题的解决方案。

我有一个 div 放在另一个里面。外黑内橙。

enter image description here

我的 HTML 和 CSS 是:

#outer {
position: fixed;
width: 30%;
height: 30%;
background-color: black;
}
#inner {
width: 100%;
height: 100%;
margin: 5px;
background-color: orange;
}
<div id="outer">
<div id="inner">
</div>
</div>

为什么我的内部 DIV 会溢出外部?如何在不提供固定尺寸的情况下修复它?

最佳答案

因为边距 - 宽度是 100% 加上边距。为避免这种情况,请编写 width: calc(100% - 10px);(= 两倍的边距。)高度相同。

#outer {
position: fixed;
width: 30%;
height: 30%;
background-color: black;
}
#inner {

width: calc(100% - 10px);
height: calc(100% - 10px);
margin: 5px;
background-color: orange;
}
<div id="outer">
<div id="inner">
</div>
</div>

关于html - 为什么内部DIV会溢出外部DIV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37487863/

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