HTML,嵌套 Div 问题
在我的页面中,我有三个部分:页眉、正文和页脚。 在正文中我有三个 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我讨厌使用百分比,因为它们取决于其他因素。
我通常用静态值做一些事情,比如:
当然,如果你希望主 div 的宽度根据浏览器的大小而改变(就像你原来的 CSS 一样),我的答案不起作用。
但我建议您不要根据浏览器更改宽度,因为它会根据窗口更改页面的组织。
I hate using percentages since they depends on other things.
I usually do something with static values, like :
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.
这很简单,
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/