繁体   English   中英

如何防止子div溢出超出父div的高度?

[英]How do I prevent a child div from overflowing beyond the height of the parent div?

在我正在布局的页面上,有一堆div被塞进彼此。 HTML代码看起来像这样:

<div id="overlay">
<div id="main_section">
  <div class="left">yadda yadda left sidebar</div>
  <div class="middle">
    <div id="header">yadda yadda header</div>
    <div id="main_content"><img class="resize content" src="static/some_image.jpg" /></div>
  </div>
  <div class="right">yadda yadda right sidebar</div>
  <div class="clear"></div>
</div>
</div>

主容器是叠加的,它使用固定位置,如下所示:

#overlay {
    position: fixed;
    width: 100%; height: 90%;
    top: 0px; left: 0px;
    background-color: rgba(255,255,255,0.5);
}

(我正在使用不同级别的不透明度分层多个背景,这就是为什么有main_section,overlay等)

现在,所有的孩子都使用相对定位,效果相当好。 问题发生在#main_content中。 #main_section和.middle都有高度:100%,正如预期的那样,它们一直到#overlay的底部。 现在,这是#main_content:

#main_content {
    position: relative;
    height: 100%;
    width: 70%;
    overflow-y: auto;
    text-align: right;
}

这不是我想要的,因为由于图像大小,事物一直延伸到页面的底部而不是#overlay的底部。 我已经尝试了溢出-y:滚动和高度:继承,我尝试过max-height:100%,我正在撕开我的头发。 Stackoverflow是我心脏病发作前的最后希望!

我更改并添加了一些属性,因为未提供完整的源代码(仅提供颜色以识别相关块)。 在我看来,错误必须在CSS的其他地方,因为底层代码对​​我来说很好。

<!DOCTYPE html>
<html>
<head>
<style>
    #overlay {
        position: fixed;
        width: 100%; 
        height: 90%;
        top: 0px;
        left: 0px;
        background-color: blue;
        border: 2px solid green;
    }
    #main_section {
        width: 100%;
        height: 100%;
        background: yellow;
    }
    .middle {
        height: 100%;
        width: 100%;
        background: lavender;
        position: relative;
    }
    #main_content {
        position: relative;
        height: 100%;
        width: 70%;
        text-align: right;
        overflow-y: auto;
        background-color: red;
    }
    img {
        width: 700px;
        height: 2000px;
        background-color: white;
        border: 1px solid black;
    }
  </style>
</head>
<body>
<div id="overlay">
<div id="main_section">
      <div class="left"></div>
      <div class="middle">
            <div id="header"></div>
            <div id="main_content"><img class="resize content" src="static/some_image.jpg" /></div>
      </div>
      <div class="right"></div>
      <div class="clear"></div>
</div>

我遇到同样问题的唯一一次是,当我设置这些#main_content {position:fixed;}.middle {position:fixed;} 你确定#main_content.middle没有继承固定位置吗? 或者可能是一个未被注意的类添加了属性?

我发现自己也有同样的问题。

关于SO的答案非常相似。 但我发现的只是整个页面/身体或容器上的固定高度。 有些人使用浮动和定位(绝对或相对在这里并不重要)。 然而,总体趋势是使用display元素,我发现它们是最优雅和实用的。

以下是我的解决方案。 您可以将它放在任何容器中。 它将采用精确到容器的高度和宽度,没有溢出。 好吧,你可以看到它只是组合一个display: inline-block父与display: table子。

请注意,如果添加display: table-caption ,将再次出现溢出(如果您知道原因,请发表评论)。

<div id="#place_me_anywhere"
     style="display: inline-block;width: 100%;height: 100%;">
    <div style="display: table;height: 100%;width: 100%;">  
        <!--<div style="display: table-caption;">
            Height overflow will occur
        </div>-->
        <div style="display: table-row;">
            <h1>Heading</h1>
        </div>
        <div style="display: table-row;background-color: pink;height: 100%;">
            <!-- this row consumes all remaining height without overflow -->
            <div style="display: table-cell;width: 10em;min-width: 10em;background-color: red;">
                Just an example of some fixed width column/cell
            </div>
            <div style="display: table-cell;background-color: blue;">
                takes the remaining width
            </div>
        </div>

   </div>

</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM