JS gets the actual width height of the HTML element

Source: Internet
Author: User

Source: http://www.cnblogs.com/zhwl/p/3858682.html

The first case is that the width and height are written in the style sheet, like #div1{width:120px;}. In this case, the width can not be obtained by #div1.style.width, but by #div1.offsetwidth.

The second case is that the width and height are written in the middle of the line, such as style= "width:120px;", which can be obtained by the above 2 methods.

Summary, because Id.offsetwidth and id.offsetheight ignore the style of writing in the style sheet or in line, so we get the element width and height when the best use of these 2 attributes. Note If you do not write a property in a style within a row, you cannot get it through ID.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.

JS gets the actual width height of the HTML element

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.