简体   繁体   English

getBoundingClientRect().width 和 height 是否包括元素的内边距和边框?

[英]Does getBoundingClientRect().width and height includes paddings and borders of element?

getBoundingClientRect().width 和 height 属性,同时返回值包括元素的填充和边框吗?

By default it returns width+padding+border Why?.默认情况下,它返回width+padding+border为什么?。

Because it's relative to the box-sizing CSS property, Which can have two values:因为它相对于box-sizing CSS 属性,它可以有两个值:

  1. content-box (default value) content-box (默认值)
  2. border-box

content-box: includes only the content. content-box:只包含内容。 Border, padding and margin are not included.不包括边框、填充和边距。

This means when you set a width, That width is set to the content only then you add the padding and border.这意味着当您设置宽度时,该宽度仅设置为内容,然后您添加填充和边框。

 console.log(document.querySelector('p').getBoundingClientRect().width)
 p { box-sizing: content-box; width: 300px; padding: 10px; border: 2px solid; }
 <p>The console should log 324px because the width is 300px<br> padding is 10px left 10px right <br> border is 2px left 2px right<br> sums up to 300+10+10+2+2 = 324</p>


border-box: includes content, padding and border.边框框:包括内容、填充和边框。 margin is not included.保证金不包括在内。

This means padding and border will be calculated within the defined width.这意味着内边距和边框将在定义的宽度内计算。

 console.log(document.querySelector('p').getBoundingClientRect().width)
 p { box-sizing: border-box; width: 300px; padding: 10px; border: 2px solid; }
 <p>console should log 300px Because padding's width and border's width have been calculated wihtin the specified width in the CSS</p>

Note: Height is not affected, But the same rules are applied to it too.注意:高度不受影响,但同样的规则也适用于它。

TLDR; TLDR;

It will always return dimensions of the border box (green box bellow), which contains the content area , padding area and border area , as defined in the CSS Box Model .它将始终返回边框框(下面的绿色框)的尺寸,其中包含CSS 框模型中定义的内容区域填充区域边框区域

CSS 盒模型

The width style宽度样式

However, one must not conflate content width (a property of the element layout) and style width (a constraint on the element layout, width: xxx; rules) which are very distinct things.但是,不能将内容宽度(元素布局的属性)和样式宽度(元素布局的约束, width: xxx; rules)混为一谈,这是非常不同的事情。 The box-sizing will only affect whether the style width constraint should include the border and padding , or not. box-sizing只会影响样式宽度约束是否应包括borderpadding

Demonstration by example示例演示

In this first snippet, we are using box-sizing: border-box rule so that style width == boder + padding + content width .在第一个片段中,我们使用box-sizing: border-box规则,这样style width == boder + padding + content width The result of getBoundingClientRect().width is 140 , and it equals style width . getBoundingClientRect().width的结果是140 ,它等于style width

 var x = document.getElementById("x"); console.log('getBoundingClientRect().width =', x.getBoundingClientRect().width); console.log('getComputedStyle(x).width =', getComputedStyle(x).width);
 #x { border: 20px solid blue; padding: 10px; width: 140px; box-sizing: border-box; }
 <div id=x>Border Box</div>

In the second snippet, we are using box-sizing: content-box rule so that style width == content width .在第二个片段中,我们使用box-sizing: content-box规则,使style width == content width The result of getBoundingClientRect().width is still 140 because the sum of border + padding + content width has not changed , only style width has changed. getBoundingClientRect().width的结果仍然是140因为边框+内边距+内容宽度的总和没有变化,只有样式宽度发生了变化。

 var y = document.getElementById("y"); console.log('y.getBoundingClientRect().width =', y.getBoundingClientRect().width); console.log('getComputedStyle(y).width =', getComputedStyle(y).width);
 #y { border: 20px solid blue; padding: 10px; width: 80px; box-sizing: content-box; }
 <div id=y>Content Box</div>

References参考

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

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