简体   繁体   English

CSS Box模型失败:总宽度保持在指定值

[英]Css Box model fails:the total width remains at specified value

I noticed the very strange(for me) behavior in Wordpress. 我注意到Wordpress中非常奇怪的行为(对我而言)。 Let's consider the following simple example. 让我们考虑以下简单示例。 If I set the width to some value, in my example 200px; 如果我将宽度设置为某个值,则在我的示例中为200px; and specify the border to 1px, the total width of element (ie. content + padding + border) remains 200px. 并将边框指定为1px,则元素的总宽度(即,内容+填充+边框)仍为200px。

According to Box Model - http://www.w3.org/TR/CSS2/box.html , if I specify with to 200px, and border to 1px, total width must be 202px(if no paddings are specified). 根据Box Model- http://www.w3.org/TR/CSS2/box.html ,如果我将设置为200px,将边框指定为1px,则总宽度必须为202px(如果未指定填充)。 But in Wordpress( tested in version 3.6) it remains 200px. 但是在Wordpress(在3.6版中测试)中,它仍然为200px。 The content becomes 198px, so 198 + 2 = 200 内容变为198px,所以198 + 2 = 200

Example

<div id="test">.</div>

#test {
display:block;
width:200px;
border:1px solid red;
background-color:black;
}

If I increase the value of border, for example set it to 4px, the content will become 192px, so total width always remains 200px; 如果增加border的值,例如将其设置为4px,则内容将变为192px,因此总宽度始终保持200px;

How this can be explained, and how to restore default behavior? 如何解释,以及如何恢复默认行为?

Meet the box-sizing . 满足框大小

In your case there's box-sizing: border-box; 在您的情况下,有box-sizing: border-box; set either to all elements ( * ) or to that particular div . 设置为所有元素( * )或特定的div

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

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