[JavaScript] js gets the actual width height of the HTML element

Source: Internet
Author: User

The first case is that the width and height are written in the style sheet, like #div1{width:120px;}. In this case,

Over #div1. Style.width can not reach the width, but through the #div1.offsetwidth to get to the width.

The second case is that the width and height are written in the line, for example, the case

All of the above 2 methods can be used to get the width.

Summary, because Id.offsetwidth and id.offsetheight ignore styles written in style sheets or inline, so

It's best to use these 2 attributes when we get the element width and height . Note If you are not writing in the style in the row

Properties are not available 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

Style does not correspond (just like #div1.style.width corresponds to #div1.offsetwidth), it is only

Can be used to get the properties of the style sheet without a browser, you can try to search "JS get style genus

Sex "or something like that.

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

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.