首页 > 解决方案 > 宽度 100 % 超出父级

问题描述

我正在尝试创建一个div#firstdiv. 在那个 div 里面我还有其他div#otherdiv的,它们的宽度是 % of #firstdiv。此外,这个其他 div 有一个特定的边距,不应该受到另一个的影响,#otherdiv但我的问题是当我这样做时position :absolute; widht:100%#otherdiv 超出了 #firstdiv。让我们看一个例子。我有 :

<div style=" width: 500px; border: 2px solid red; height: 150px; >
     <div style="width: 100%; position: absolute; border: 1px solid blue; >here<div>
</div>

我有以下结果:

在此处输入图像描述

我希望蓝色的 div 在红色的里面。如果我这样做,它会起作用如果position absolute我有另一个#otherdiv 剩余边距取决于已经存在的东西,我不想要那个。

我改变了任何东西:我正在使用引导程序,我的这两个潜水在里面

<div class="container-fluid container-first"><div class="container">

标签: htmlcss

解决方案


像这样添加position relative到父级

<div style=" width: 500px; border: 2px solid red; height: 150px; position: relative" >
             <div style="width: 100%; position: absolute; border: 1px solid blue;">here</div>
        </div>

推荐阅读