父级 div 不包含所有子级
我有一个内容 div,它有一个标题 div,其中包含另外两个 div,然后是一个不在标题中但在内容中的最终 div。但是,看起来内容的最后一个子 div 实际上并未包含在内容中。
这是 CSS:
div.outline {border: 1px solid red;}
#content{background-color:tan;}
#container{width:680px;overflow:hidden;}
#header{margin-bottom:10px; background-color:yellow;}
#title{float:left;}
#link{float:right;}
.clear_it{clear:both;}
#chooser{float:left; margin-right:20px}
#renamer{padding-left:20px;}
#img{width:170px;height:130px;border:1px solid rgb(192,192,192);float:left;margin-left:20px;margin-top:20px;}
这是 HTML:
<div id="container">
<div id="header">
<div id="title">Header Title</div>
<div id="link" style="float:right;">
<a href="http://www.apple.com" target="_blank">Link</a>
</div>
<div class="clear_it"></div>
</div>
<div id="content">
<div id="content_header">
<div id="chooser">
<select>
<option value="1">This is a fairly long option</option>
<option value="2">short</option>
</select>
</div>
<div id="renamer">
<a href="http://apple.com" target="_blank">Link</a>
</div>
</div>
<div class="clear_it"></div>
<div id="img"></div>
</div>
</div>
这是结果:
I have a content div, that has a header div, which contains two other divs, and then a final div that is not in the header, but is in the content. However, it looks like the last child div of content is not actually contained in the content.
Here is the CSS:
div.outline {border: 1px solid red;}
#content{background-color:tan;}
#container{width:680px;overflow:hidden;}
#header{margin-bottom:10px; background-color:yellow;}
#title{float:left;}
#link{float:right;}
.clear_it{clear:both;}
#chooser{float:left; margin-right:20px}
#renamer{padding-left:20px;}
#img{width:170px;height:130px;border:1px solid rgb(192,192,192);float:left;margin-left:20px;margin-top:20px;}
Here is the HTML:
<div id="container">
<div id="header">
<div id="title">Header Title</div>
<div id="link" style="float:right;">
<a href="http://www.apple.com" target="_blank">Link</a>
</div>
<div class="clear_it"></div>
</div>
<div id="content">
<div id="content_header">
<div id="chooser">
<select>
<option value="1">This is a fairly long option</option>
<option value="2">short</option>
</select>
</div>
<div id="renamer">
<a href="http://apple.com" target="_blank">Link</a>
</div>
</div>
<div class="clear_it"></div>
<div id="img"></div>
</div>
</div>
Here is the result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议使用一种不同的方法来清除浮动,该方法不涉及任何清除 div(从而完全删除
clear_it
)。只需将overflow: auto
或overflow: hide
添加到包含浮动元素的容器即可:#content { Overflow: auto; }
#container { 溢出:自动; }
#content-header { 溢出:自动; }
新 HTML:
新 CSS:
请参阅:http://jsfiddle.net/Wexcode/g7MkN/< /a>
I'd recommend a different method to clear your floats that doesn't involve any clearing divs (thus removing
clear_it
altogether). Just addoverflow: auto
oroverflow: hidden
to your containers that contain the floated elements:#content { overflow: auto; }
#container { overflow: auto; }
#content-header { overflow: auto; }
New HTML:
New CSS:
See: http://jsfiddle.net/Wexcode/g7MkN/
尝试将 div.img 放在 div.clear_it 上方
try to place the div.img above the div.clear_it
您需要清除浮动。由于您的
向左浮动,就在您的
clear_it
div 之后,采取 看看这个寻求帮助
You need to clear the float. Since your
<div id="img"></div>
is floated to the left, right after yourclear_it
divTake a look at this for some help
你的问题没写清楚。
您希望棕褐色背景延伸至图像吗?
使用这个:
#content{溢出:自动; }
You question is not clearly written.
Do you want the tan background to extend up to the image?
Use this:
#content{ overflow:auto; }
您的问题是您的子元素是浮动的,而父元素不是浮动的。在这种情况下,子级虽然包含在父级中,但不会与其“对齐”,因为它们不在同一“定位”层次结构中。
我已经设置了一个 JSFiddle: http://jsfiddle.net/M2pMh/6/
请注意使用浮动。您可以使用它(如上面的答案)来实现您想要的结果。
通过将图像 div 移动到内容下方的区域,并将其向左浮动,同时调整其他元素的大小以填充其父元素(并浮动),img 现在将位于标题和内容下方。
Your problem is that you have child elements that are floated, in a parent element that isn't floated. In this instance, the children, although contained by the parent, will not be "aligned" with it, as they aren't in the same "positioning" hierarchy.
I've set up a JSFiddle: http://jsfiddle.net/M2pMh/6/
Note the use of float. You can play with this (as in the answers above) to achieve the results you want.
By moving the image div into the area below content, and floating it left, whilst sizing the other elements to fill their parents (and to float), img will now be below the header and content.