This time, thoroughly understand the CSS box model (illustrated)

First, let's look at the basic concepts css box model:

Is a box package surrounding HTML elements on the nature of the CSS box model, comprising: margin, border, padding, and the actual content. Cartridge model allows us the space between the other elements and the elements are placed around the element border. Are two kinds of cassette type standard box model and IE mold box

Box size

Width = width of the box content filling + + left + right padding left border right border + + + left right margin from
Height = height of the content box on the filling + + + filling the lower margin border + + + bottom margins on the frame

Standard models and the difference between the cartridge box IE

  • Standard box model: content = content

  • IE box model: content = width + padding + border

How to set up two models css

  • Css by the box-sizing property
  • Standard box model: content-box
  • IE box model: border-box

How to obtain the width and height of the cartridge JS corresponding to the model

  • node.style.height/width
In this way only take both the width and height of the inline style dom element, is introduced through the outer link and inline style style tag provided in the style is not acquired width and height of the node
   <div style="width:100px;height:50px" id="agoni">agoni</div>
   
   document.getElementById("agoni").style.width ="200px";
1, only the operation inline style;
2, only the region including the content, padding and border portions is not included (only the content);
3, the return value with units, the value of the return data type is string.
  • node.currentStyle.width / height (ie only support)
This approach is the result of acquiring rendered, but only supports the older versions of IE browser, this feature is nonstandard, try not to use it in a production environment. style, inline styles are paid outreach; return values ​​with units, return value data type is String; unfortunately only support IE.
  • window.getComputedStyle(node).width/height
window.getComputedStyle()方法返回一个对象
返回值带单位,返回值的数据类型是string。

  • node.getBoundingClientRect().width/height
1、返回元素的大小及其 只相对于可视去窗口。包含 边框(border)内边距(padding)以及CSS设置的 宽度(width)
2、返回值不带单位,返回值的数据类型是number。

  • node.offsetWidth/offsetHeight
1、包含元素的边框(border)、内边距(padding)、滚动条(scrollbar)(如果存在的话)、以及CSS设置的宽度(width)和高度(height)的值。
2、返回值不带单位,返回值的数据类型是number;
3、还有offsetHeight/Width、offsetLeft/Top
4、offset 的方向值需要考虑到父级,如果父级是定位元素,那么子元素的offset值相对于父元素,如果父元素不是定位元素,那么子元素的offset值相对于可视区窗口。
学如逆水行舟,不进则退
发布了540 篇原创文章 · 获赞 1522 · 访问量 22万+

Guess you like

Origin blog.csdn.net/weixin_42429718/article/details/104448450