HTML,嵌套 Div 问题

发布于 2024-10-30 04:57:05 字数 556 浏览 1 评论 0原文

在我的页面中,我有三个部分:页眉、正文和页脚。 在正文中我有三个 div。第一个 div 包括另外两个小 div。

我写了这段 CSS 代码:

#body_container{
    margin: 10px 160px;
    height: auto;
    width: 100%;
    clear: both;
    border: 1px solid #3F0;
}

div.body_contents{
    height: auto;
    width: 74%;
    float: left;
    border: 1px solid #960;
}

div.sidebar{
    height: auto;
    width: 25%;
    float: right;
    border: 1px solid #F0F;
}

但是当我检查它时,div 超出了我的框架。我给了左右 160px 的边距。我应该怎么办? 谢谢

在此处输入图像描述

In my page I have three sections Header, Body and Footer.
In body I have three divs. First div includes another two small divs.

I wrote this CSS code:

#body_container{
    margin: 10px 160px;
    height: auto;
    width: 100%;
    clear: both;
    border: 1px solid #3F0;
}

div.body_contents{
    height: auto;
    width: 74%;
    float: left;
    border: 1px solid #960;
}

div.sidebar{
    height: auto;
    width: 25%;
    float: right;
    border: 1px solid #F0F;
}

but when I check it, div is out of my frame. I gave 160px margin of left and right. what should i do?
Thanks

enter image description here

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

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

发布评论

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

评论(2

只涨不跌 2024-11-06 04:57:05

我讨厌使用百分比,因为它们取决于其他因素。

我通常用静态值做一些事情,比如:

#body_container{
    margin: 10px auto; /** The auto will make this div centered to the page **/
    width: 1000px; /** suppose you want this width **/
    clear: both;
    border: 1px solid #3F0;
}

div.body_contents{
    width: 748px; /** The width of the main part, -2px (for the border) **/
    float: left;
    border: 1px solid #960;
}

div.sidebar{
    width: 248px; /** The width of the side bar, -2px (for the border) **/
    border: 1px solid #F0F;
}

当然,如果你希望主 div 的宽度根据浏览器的大小而改变(就像你原来的 CSS 一样),我的答案不起作用。

但我建议您不要根据浏览器更改宽度,因为它会根据窗口更改页面的组织。

I hate using percentages since they depends on other things.

I usually do something with static values, like :

#body_container{
    margin: 10px auto; /** The auto will make this div centered to the page **/
    width: 1000px; /** suppose you want this width **/
    clear: both;
    border: 1px solid #3F0;
}

div.body_contents{
    width: 748px; /** The width of the main part, -2px (for the border) **/
    float: left;
    border: 1px solid #960;
}

div.sidebar{
    width: 248px; /** The width of the side bar, -2px (for the border) **/
    border: 1px solid #F0F;
}

Of course, if you want the width of you main div to change regarding of the size of the browser (as it would be with your original CSS), my answer doesn't work.

But I would recommend you to NOT do a change of width based on the browser since it would change the organization of your page depending on the window.

夜无邪 2024-11-06 04:57:05

这很简单,margin 添加到元素的最终渲染宽度(即父级宽度的 160 x 2 + 100%,因此它是溢出),因此您可能想要将其设置为,例如外部 div,宽度为 79%,左右边距各为 10%(因此总计为 99%,少 1% 以便为边框等留出一些空间……尽管我通常不是这样做的)

示例: http://jsfiddle.net/MKjwU/6/

还有一件事:清除浮动,您没有像示例中那样清除它...

要么使用额外的 div,如下所示: http:// /jsfiddle.net/MKjwU/7/

或使用此技术:http://www .quirksmode.org/css/clearing.html

参见:http://jsfiddle.net/ MKjwU/8/

That's simple, margin adds to the final rendered width of the element (that's 160 x 2 + 100% of parent's width, so it is overflow), so you may want to make it, for example, for the outer div, that width is 79%, and the left and right margin is 10% each (so total is 99%, 1% less to allow some room for borders, etc... although that's usually not how I do it)

example: http://jsfiddle.net/MKjwU/6/

one more thing: to clear the floats, you don't clear it like in your example...

either use an extra div, like this: http://jsfiddle.net/MKjwU/7/

or using this technique: http://www.quirksmode.org/css/clearing.html

see it at: http://jsfiddle.net/MKjwU/8/

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