plainJS

Functions / Styles

Getting width and height of an element

jQuery $.width(), $.height(), $.outerWidth(), $.outerHeight(), $.innerWidth(), $.innerHeight()

2015 page

This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.

In short

Get the current computed dimension of an element, with or without borders and margins

The following figure shows the CSS box model: a block element with content, padding, border, and margin:

Use the following methods to get width and height of each of those boxes:

js
var box = document.querySelector('div');

// width and height in pixels, including padding and border
// Corresponds to jQuery outerWidth(), outerHeight()
var width = box.offsetWidth;
var height = box.offsetHeight;

// width and height in pixels, including padding, but without border
// Corresponds to jQuery innerWidth(), innerHeight()
var width = box.clientWidth;
var height = box.clientHeight;

The margin of an element is calculated from its computed style:

js
var style = window.getComputedStyle ? getComputedStyle(el, null) : el.currentStyle;

var marginLeft = parseInt(style.marginLeft) || 0;
var marginRight = parseInt(style.marginRight) || 0;
var marginTop = parseInt(style.marginTop) || 0;
var marginBottom = parseInt(style.marginBottom) || 0;

The computed style may be used to read other properties, too. For example border width:

js
var borderLeft = parseInt(style.borderLeftWidth) || 0;

Getting the window width and height is done differently. Just like in jQuery, the above presented methods do not work on the window or document object. Use this instead:

js
var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;