父级 div 不包含所有子级

发布于 2024-12-19 21:21:47 字数 1627 浏览 3 评论 0原文

我有一个内容 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>

这是结果:

results

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:

results

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我ぃ本無心為│何有愛 2024-12-26 21:21:47

我建议使用一种不同的方法来清除浮动,该方法不涉及任何清除 div(从而完全删除 clear_it)。只需将 overflow: autooverflow: hide 添加到包含浮动元素的容器即可:

#content { Overflow: auto; }
#container { 溢出:自动; }
#content-header { 溢出:自动; }

新 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>

    <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 id="img"></div>
    </div>
</div>

新 CSS:

div.outline {border: 1px solid red;}
#content{background-color:tan; overflow: auto;}
#container{width:680px;overflow:auto;}
#header{margin-bottom:10px; background-color:yellow; overflow: auto;}
#title{float:left;}
#link{float:right;}
#content_header { overflow: auto; }
#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;}

请参阅: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 add overflow: auto or overflow: hidden to your containers that contain the floated elements:

#content { overflow: auto; }
#container { overflow: auto; }
#content-header { overflow: auto; }

New 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>

    <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 id="img"></div>
    </div>
</div>

New CSS:

div.outline {border: 1px solid red;}
#content{background-color:tan; overflow: auto;}
#container{width:680px;overflow:auto;}
#header{margin-bottom:10px; background-color:yellow; overflow: auto;}
#title{float:left;}
#link{float:right;}
#content_header { overflow: auto; }
#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;}

See: http://jsfiddle.net/Wexcode/g7MkN/

南七夏 2024-12-26 21:21:47

尝试将 div.img 放在 div.clear_it 上方

<div id="img"></div>
<div class="clear_it"></div>

try to place the div.img above the div.clear_it

<div id="img"></div>
<div class="clear_it"></div>
动听の歌 2024-12-26 21:21:47

您需要清除浮动。由于您的

向左浮动,就在您的 clear_it div 之后,

采取 看看这个寻求帮助

You need to clear the float. Since your <div id="img"></div> is floated to the left, right after your clear_it div

Take a look at this for some help

春夜浅 2024-12-26 21:21:47

你的问题没写清楚。

您希望棕褐色背景延伸至图像吗?

使用这个:

#content{溢出:自动; }

You question is not clearly written.

Do you want the tan background to extend up to the image?

Use this:

#content{ overflow:auto; }

债姬 2024-12-26 21:21:47

您的问题是您的子元素是浮动的,而父元素不是浮动的。在这种情况下,子级虽然包含在父级中,但不会与其“对齐”,因为它们不在同一“定位”层次结构中。

我已经设置了一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文