简体   繁体   English

容器未为子div调整大小

[英]Container not resizing for child div

Click here for visual 点击此处查看

As you can see from the picture, my parent container is not expanding to fit my child container. 从图片中可以看到,我的父容器没有扩展到适合我的子容器的大小。 The page container (#contain) actually stops at the bottom left hand corner of the kitchen photograph. 页面容器(#contain)实际上停在厨房照片的左下角。 The child container (#zone2) is clearly overflowing outside its parent container (#contain). 子容器(#zone2)显然在其父容器(#contain)之外溢出。 I would like to be able to have (#contain) expand automatically to fit (#zone2). 我希望能够(#contain)自动扩展以适合(#zone2)。 The CSS is: CSS是:

#contain {
    position: relative;
    width: 100%;
    margin: 0 px;
    background: #E3DCCC;
    z-index: 0;
}

#zone1 {
    width: 100%;
    height: 850px;
    background: url(http://waly1039.com/sites/default/files/k4.jpg) no-repeat center   top;
    position: absolute;
    z-index: -1;
}
#head {
    position: absolute;
    top: 20px;
    width: 100%;
    height: 330px;
}

#head img {
    max-width: auto;
    height: auto;
}

#zone2 {
    position: relative;
    overflow: hidden;
    padding: 3px;
    top: 360px;
    float: right;
    right: 15px;
    width: 53%;
    height: auto;
    border: 4px solid #715E40;
    background-color: white;
}
#zone2 img {
      max-width:100%;
      height: auto;
      float:left;
      margin: 5px;
}

#zone3 {
    position: relative;
    top: 710px;
    left: 15px;
    float: left;
    height: 340px;
    width: 38%;
    border: 4px solid #715E40;
    background-color: white;
} 

This is a float issue. 这是一个浮动问题。 Try adding the traditional CSS clear fix to #zone2's container: 尝试将传统的CSS清除修复程序添加到#zone2的容器中:

.container:after{
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}

Be sure to put this in the :after pseudo selector, otherwise it won't work for you. 请确保将其放在:after伪选择器中,否则它将对您不起作用。 Floated elements exist outside of normal document flow, which is why the container isn't expanding to contain them. 浮动元素存在于常规文档流之外,这就是为什么容器不扩展以包含它们的原因。 The clear fix forces the floats to be cleared, which will cause the container to expand around the bottom of this element. 清除固定将迫使浮子被清除,这将导致容器在该元件的底部周围膨胀。

I tested adding more images to #zone2 and #contain expands vertically. 我测试了向#zone2添加更多图像,并且#contain垂直扩展。 Somehow you've got an element(s) in #zone2 with padding or margins that aren't being added to the parent's total height. 不知何故,您在#zone2有一个元素,但未将其空白或边距添加到父级的总高度中。

If you want a quick fix in order to move on then add margin-bottom: 30px; 如果要继续快速修复,请添加margin-bottom: 30px; to #zone2 . #zone2

I've duplicated your problem and was able to resolve it with this: You might want to try it. 我已经重复了您的问题,并可以通过以下方法解决它:您可能想尝试一下。 It's looks a bit odd so make a class for it if you like. 它看起来有些奇怪,所以如果您愿意,可以上课。 I'm more concern with where it is placed. 我更关心它的放置位置。

Just beneath lines of your code, add my third line. 在代码行的下面,添加我的第三行。 Just that and you are done. 就这样,您就完成了。 Note, it more about positioning. 注意,它更多关于定位。

<div id="zone3"></div>
<div id="zoneclear"></div>
<br style="clear:both; float:none; display:block; height:1px;" />

Just add the third line. 只需添加第三行。

and just modify one of your styles: 并修改您的一种样式:

#zoneclear {   
    clear: both;
    float:none;
    display:block;  
    height: 30px;
    position: relative;
}

[EDIT] The codes have a serious bug in firefox which is not present in Google Chrome (that I tested in earlier due to your relative positioning. So I've modified the #zoneclear style to fix that. You might have to test if the other browsers like this hack. [EDIT]该代码在firefox中有一个严重的错误,该错误在Google Chrome中不存在(由于您的相对位置,我较早时进行了测试。因此,我已经修改了#zoneclear样式来解决此问题。您可能必须测试一下其他浏览器,如这种黑客。

I hope it helps you 希望对您有帮助

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

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