Pure JavaScript Gets the height and width of an element

Source: Internet
Author: User

pure JavaScript scripts get element styles

One, the problem description

Sometimes, we need to do some of the pages to adapt the elements according to different display, then how to calculate the margin-top of this element, I believe that everyone will calculate, then involved in one is to use JS to get this element of the style, such as height

Two, Solution

As we all know, for an HTML page element, its style definition has two ways, one is written in the elements of the peer node, commonly known as the inline style, the other is defined in the external CSS file, commonly known as the external style, that is, these two definitions, resulting in the way we get height differences, See the table below

var obj = document.getElementById ("The value of the id attribute of your page element");

                          How to get there                                                                     description

                 obj.offsetheight                               offsetxxx Ignore inline or out-of-line style, so this method gets a wide range than the next one

obj.style.height Obj.style.XXX This method is only suitable for inline styles



Pure JavaScript Gets the height and width of an 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.