如何检索HTML元素的实际宽度和高度? [英] How do I retrieve an HTML element's actual width and height?

查看:128
本文介绍了如何检索HTML元素的实际宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个< div> ,我希望在浏览器的显示(视口)中居中。为此,我需要计算< div> 元素的宽度和高度。



我应该使用什么?请包括关于浏览器兼容性的信息。 >和 .offsetHeight 属性。
请注意它们属于元素,而不是 .style



var width = document.getElementById('foo')。offsetWidth;


Suppose that I have a <div> that I wish to center in the browser's display (viewport). To do so, I need to calculate the width and height of the <div> element.

What should I use? Please include information on browser compatibility.

解决方案

You should use the .offsetWidth and .offsetHeight properties. Note they belong to the element, not .style.

var width = document.getElementById('foo').offsetWidth;

这篇关于如何检索HTML元素的实际宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆