如何防止 padding 属性改变 CSS 中的宽度或高度?

新手上路,请多包涵

我正在使用 DIV s 创建一个站点。除了创建 DIV 之外,一切正常。我这样创建它们(示例):

 newdiv {
    width: 200px;
    height: 60px;
    padding-left: 20px;
    text-align: left;
}

当我添加 padding-left 属性时, DIV 的宽度变为 220px,我希望它保持在 200px。

Let’s say I create another DIV named anotherdiv exactly the same as newdiv , and put it inside of newdiv but newdiv 没有填充和 anotherdivpadding-left: 20px 。我得到同样的东西, newdiv 的宽度将是 220px。

我该如何解决这个问题?

原文由 Sam 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 663
1 个回答

添加属性:

 -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box;    /* Firefox, other Gecko */
box-sizing: border-box;         /* Opera/IE 8+ */

注意: 这在版本 8 以下的 Internet Explorer 中 不起作用

原文由 Pramod 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏