2011-04-05 123 views
2

在我的页面中,我有三个部分Header,Body和Footer。 在身体我有三个divs。第一格包括另外两个小格子。HTML,嵌套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的左右边距。我该怎么办? 感谢

enter image description here

+3

首先你应该提供一个最低的HTML代码,让回答者可以重现您的问题 – ITroubs 2011-04-05 07:36:09

回答

1

我讨厌使用百分比,因为它们依赖于其他的事情。

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

#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),我的回答不起作用。

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

+0

感谢您的指南,因为你预测我想改变任何关于改变浏览器。但是你的建议是非常有帮助的。再次感谢。 – Hesam 2011-04-05 07:48:20

+0

不客气:) – 2011-04-05 09:49:47

1

这很简单,margin添加到元素的最终渲染宽度(160 x 2 + 100%的父宽度,所以它是溢出),所以你可能想让它,例如,外部div ,宽度为79%,左右边距分别为10%(因此总数为99%,为了留出一些空间用于边界等等,虽然通常不是这样)

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

一两件事:清除浮动,你不清晰,仿佛是你的榜样?

要么使用分机RA格,像这样:http://jsfiddle.net/MKjwU/7/

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

看到它:http://jsfiddle.net/MKjwU/8/