</>
Skip to content
jQuery lessons (22/39)

jQuery — Dimensions

width() and height()

var w = $("div").width();    // Content width
var h = $("div").height();   // Content height

// Set
$("div").width(300);
$("div").height(200);

innerWidth() and innerHeight()

Includes padding but not border:

var iw = $("div").innerWidth();
var ih = $("div").innerHeight();

outerWidth() and outerHeight()

Includes padding and border:

var ow = $("div").outerWidth();
var oh = $("div").outerHeight();

// Include margin
var omw = $("div").outerWidth(true);
var omh = $("div").outerHeight(true);

Width/Height vs CSS

MethodReturns
.width()Number (px)
.css("width")String ("200px")

Position

var offset = $("div").offset();
console.log(offset.top, offset.left); // Relative to document

var pos = $("div").position();
console.log(pos.top, pos.left); // Relative to offset parent

Scroll Position

var scrollTop = $(window).scrollTop();
var scrollLeft = $(window).scrollLeft();

Dimension Summary

MethodIncludes
width() / height()Content only
innerWidth / innerHeight+ Padding
outerWidth / outerHeight+ Border
outerWidth(true)+ Margin

Mini Practice

  1. Get an element's width and height
  2. Set dimensions with width() and height()
  3. Get the offset position
  4. Check window scroll position

Up Next

Continue with Traversing — navigating the DOM tree.

Related Topics

Frequently Asked Questions about Dimensions

What is Dimensions in jQuery?

Dimensions is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Dimensions?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Dimensions.

Why is Dimensions important in jQuery?

Dimensions is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.