jQuery Dimensions


With jQuery, it is easy to handle the dimensions of elements and browser windows.


jQuery Dimension Methods

jQuery provides several important methods for handling dimensions:

  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()

jQuery Dimensions

jQuery Dimensions


jQuery width() and height() Methods

The width() method sets or returns the width of an element (excluding padding, border, or margin).

The height() method sets or returns the height of an element (excluding padding, border, or margin).

The following example returns the width and height of the specified <div> element:

Example

$("button").click(function(){ var txt=""; txt+="The width of div is:" + $("#div1").width() + "</br>"; txt+="The height of div is:" + $("#div1").height(); $("#div1").html(txt); });

Try it Yourself »


jQuery innerWidth() and innerHeight() Methods

The innerWidth() method returns the width of an element (including padding).

The innerHeight() method returns the height of an element (including padding).

The following example returns the inner-width/height of the specified <div> element:

Example

$("button").click(function(){ var txt=""; txt+="div width, including padding:" + $("#div1").innerWidth() + "</br>"; txt+="div height, including padding:" + $("#div1").innerHeight(); $("#div1").html(txt); });

Try it Yourself »


jQuery outerWidth() and outerHeight() Methods

The outerWidth() method returns the width of an element (including padding and border).

The outerHeight() method returns the height of an element (including padding and border).

The following example returns the outer-width/height of the specified <div> element:

Example

$("button").click(function(){ var txt=""; txt+="div width, including padding and border:" + $("#div1").outerWidth() + "</br>"; txt+="div height, including padding and border:" + $("#div1").outerHeight(); $("#div1").html(txt); });

Try it Yourself
Other Extensions