简体   繁体   English

CSS Box模型宽度问题

[英]CSS Box model width problem

I am newbiew to css. 我是CSS的新手。 Please have a look of this simple two column layout first. 首先请看一下这个简单的两列布局。

http://www.vanseodesign.com/blog/demo/2-col-css.html http://www.vanseodesign.com/blog/demo/2-col-css.html

I want to ask why the width of content + width of sidebar =/= width of container? 我想问为什么内容的宽度+边栏的宽度= / =容器的宽度?

Width of content = 610px 内容宽度= 610px

Width of padding in content = 5+5 = 10px 内容中的填充宽度= 5 + 5 = 10px

Total width of content = 620px 内容的总宽度= 620px

Similarly Total width of sidebar with padding = 340px 同样,带有填充的边栏的总宽度= 340px

So total content and sidebar = 620+340 = 960px which is equal to width of container ! 所以总内容和边栏= 620 + 340 = 960px,等于容器的宽度!

The W3C specification says that the width of a box is: margin + border + padding + width. W3C规范说盒子的宽度是:边距+边框+填充+宽度。 In this way you'll be able to determine the actual box width. 这样,您将能够确定实际的盒子宽度。

If you sum the resulting box widths you will have a result equal to the container width. 如果求和结果框的宽度,则结果将等于容器的宽度。

Without seeing your code it's hard to give you an answer. 没有看到您的代码,很难给您答案。 But some basic CSS and html is below which will give you a content area the same size as your header, see it in action here http://jsfiddle.net/peter/wZQNY/ 但是下面是一些基本的CSS和html,它将为您提供一个与标题大小相同的内容区域,请在此处查看实际操作http://jsfiddle.net/peter/wZQNY/

The CSS CSS

.container{
    margin:0 auto;
    width:960px;
}
.header{
    width:100%;
    background:#eee;
}
.content{
    width:100%;
    background:#ccc;
}

The HTML HTML

<div class="container">
    <div class="header">header navigation</div>
    <div class="content">page content</div>
</div>

I can't see the problem. 我看不到问题。 In your link, the content.width + sidebar.width == container.width 在您的链接中,content.width + sidebar.width == container.width

What's your problem? 你怎么了? what browser are you using? 你使用的是什么浏览器?


A possible solution is that you may have some weird behavior due to border or margins, for that, you should apply a CSS Reset . 一个可能的解决方案是,由于边框或边距,您可能会有一些奇怪的行为,为此,您应该应用CSS Reset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM