JavaScript gets the actual width and height of the HTML element

Source: Internet
Author: User

One, JavaScript get HTML element width high

1. The width and height are written in the style table, such as #div1{width:120px;}. In this case, the width can not be obtained by #div1.style.width, but by #div1.offsetwidth.
2. Width and height are written in the middle of the line, such as style= "width:120px;", in this case through the above 2 methods can be achieved.

Summary, because clientwidth and offsetwidth ignore style written in the style sheet or line, so we get the element width and height when it is best to use these 2 attributes. Note If you do not write a property in a style within a row, you cannot get it through I**.STYLE.ATRR.
Today's front-end productions seldom write styles directly in style, they are written in the style table. If you want to get a style that does not correspond (like #div1.style.width corresponding to #div1.offsetwidth), you can only use the browser to get the style sheet properties, you may try to search "JS get style properties" and so on.

Code:
var o = document.getElementById ("View");
var h = o.offsetheight; Height
var w = o.offsetwidth; Width

Ii. the relationship between width, clientwidth and offsetwidth

Width is the contents of the HTML element

clientwidth = width + padding
offsetwidth = width + padding + border

JavaScript gets the actual width and height of the HTML element

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.