JS gets the actual width height of the HTML element _javascript tips

Source: Internet
Author: User

The first is that the width and height are written in the style sheet, such as #div1{width:120px. In this case, the width can not be obtained through the #div1.style.width, but only through the #div1.offsetwidth.

The second situation is that wide and high is written in the line, such as style= "width:120px," which can be achieved by the above 2 methods to get width.

Summary, because Id.offsetwidth and id.offsetheight ignore styles written in style sheets or in rows, so it's best to use these 2 attributes when we get the element width and height. Note that if you do not write attributes in the inline style, you cannot get them by ID.STYLE.ATRR.

Now the front-end production is rarely directly to the style of writing, are written in the style table. If you want to get the style does not correspond (just like #div1.style.width corresponding #div1.offsetwidth), can only be for the browser to get the property of the style sheet, you can 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

The above JS to get the actual width of the HTML elements of the height of the method is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.