简体   繁体   English

有没有办法在添加填充时保持div的宽度和高度固定?

[英]Is there a way of keeping width and height of the div fixed when added padding?

For example: 例如:

.large{
    font-size: 200%;
}
.bluebox{
    background-color: blue;
    height: 200px;
    padding-left: 20px;
    width: 200px;
}
<div class="bluebox">
    <p class="large">This is a bigger paragraph</p>
</div>

I want to keep div in fixed widht and height when I add padding to the content. 当我向内容添加填充时,我想将div保持在固定的widht和height中。 (Yes I can pre-calculate padding and then give proper width but if there is a simpler way?) (是的,我可以预先计算填充,然后给出适当的宽度,但如果有更简单的方法?)

box-sizing: border-box; is what you're looking for. 是你在找什么。

It changes the default CSS box model to calculate widths and heights of elements including borders and padding but not margin. 它会更改默认的CSS框模型,以计算元素的宽度和高度,包括边框和填充,但不包括边距。

6.1. 6.1。 'box-sizing' property 'box-sizing'财产

border-box 边界框
The specified width and height (and respective min/max properties) on this element determine the border box of the element. 此元素上指定的宽度和高度(以及相应的最小/最大属性)确定元素的边框。 That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. 也就是说,元素上指定的任何填充或边框都布局并在此指定的宽度和高度内绘制。

EXAMPLE HERE 这里的例子

The following CSS should work 以下CSS应该有效

box-sizing: border-box; 
padding:12px; 

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

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