TExcelTAD
Home › jQuery › jQuery Dimensions

jQuery Dimensions

jQuery has dedicated methods for measuring elements, the window and the document. They return numbers (in pixels), so you can calculate with them directly — no need to parse "120px" strings.

The box model in one picture

Every element is a set of nested boxes: content, then padding, then border, then margin. The dimension methods differ in which boxes they include:

text
┌──────────── margin ─────────────┐
│ ┌────────── border ───────────┐ │
│ │ ┌──────── padding ────────┐ │ │
│ │ │        content          │ │ │   width()          = content
│ │ └─────────────────────────┘ │ │   innerWidth()     = content + padding
│ └─────────────────────────────┘ │   outerWidth()     = content + padding + border
└─────────────────────────────────┘   outerWidth(true) = … + margin
Method Includes
.width() / .height() Content only
.innerWidth() / .innerHeight() Content + padding
.outerWidth() / .outerHeight() Content + padding + border
.outerWidth(true) / .outerHeight(true) Content + padding + border + margin

Measuring a box

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #box { width: 200px; height: 80px; padding: 10px; border: 5px solid #0769ad; margin: 15px;
           background: #eef5fb; }
  </style>
</head>
<body>
  <div id="box">width 200, padding 10, border 5, margin 15</div>
  <button id="measure">Measure</button>
  <button id="sizing">Toggle box-sizing</button>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#measure").on("click", function () {
      const $b = $("#box");
      console.log("box-sizing:", $b.css("box-sizing"));
      console.log("width():", $b.width());
      console.log("innerWidth():", $b.innerWidth());
      console.log("outerWidth():", $b.outerWidth());
      console.log("outerWidth(true):", $b.outerWidth(true));
    });
    $("#sizing").on("click", function () {
      const $b = $("#box");
      $b.css("box-sizing", $b.css("box-sizing") === "border-box" ? "content-box" : "border-box");
      $("#measure").trigger("click");
    });
  </script>
</body>
</html>

With the default content-box, the results are 200, 220, 230 and 260. Switch to border-box and the CSS width: 200px now includes padding and border, so .width() reports the remaining content width (170) and .outerWidth() becomes 200. jQuery handles both models for you.

Setting sizes

.width(value) and .height(value) are setters too. Numbers are pixels; strings can use any unit; relative values work:

js
$("#box").width(300);          // 300px
$("#box").height("50%");
$("#box").width("+=20");       // 20px wider
$(".col").width(function (i, w) { return w / 2; });   // halve each column

The window and the document

Expression Meaning
$(window).width() / .height() Size of the browser viewport
$(document).height() Height of the whole document (can be taller than the window)
$(window).scrollTop() How far the page is scrolled down
$(el).scrollTop(y) Scroll a scrollable element (or the window) to y
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; margin: 0; }
    #bar { position: fixed; top: 0; left: 0; height: 6px; width: 0; background: #e67e22; }
    #hud { position: fixed; top: 10px; right: 10px; background: #0009; color: #fff; padding: 6px 10px; border-radius: 6px; font-size: 13px; }
    .content { padding: 20px; }
    .content p { height: 120px; margin: 0 0 10px; background: #f4f6f8; padding: 8px; }
  </style>
</head>
<body>
  <div id="bar"></div>
  <div id="hud">scroll me</div>
  <div class="content">
    <p>Scroll this frame ↓</p><p>Section 2</p><p>Section 3</p><p>Section 4</p><p>Section 5</p><p>The end 🎉</p>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    function update() {
      const top = $(window).scrollTop();
      const max = $(document).height() - $(window).height();
      const pct = max > 0 ? Math.round(top / max * 100) : 0;
      $("#bar").css("width", pct + "%");
      $("#hud").text("viewport " + $(window).width() + "×" + $(window).height() + " | " + pct + "%");
    }
    $(window).on("scroll resize", update);
    update();
    console.log("document height:", $(document).height(), "window height:", $(window).height());
  </script>
</body>
</html>

This is a reading-progress bar: the scrolled distance divided by the maximum scroll distance.

Position and offset

Method Returns
.offset() { top, left } relative to the document
.offset({ top, left }) Moves the element to those document coordinates
.position() { top, left } relative to the offset parent (nearest positioned ancestor)
.scrollLeft() / .scrollTop() Scroll position of an element
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #area { position: relative; margin: 30px 0 0 40px; width: 260px; height: 140px; border: 1px solid #aaa; background: #fafafa; }
    #dot { position: absolute; width: 16px; height: 16px; border-radius: 50%; background: crimson; left: 0; top: 0; pointer-events: none; }
  </style>
</head>
<body>
  <div id="area"><div id="dot"></div></div>
  <p id="out">Click inside the box.</p>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#area").on("click", function (e) {
      const off = $(this).offset();                 // area's document position
      const x = e.pageX - off.left;
      const y = e.pageY - off.top;
      const $dot = $("#dot");
      $dot.css({ left: x - $dot.outerWidth() / 2, top: y - $dot.outerHeight() / 2 });
      $("#out").text("x: " + Math.round(x) + ", y: " + Math.round(y));
      console.log("dot.position():", $dot.position(), "| area.offset():", off);
    });
  </script>
</body>
</html>

Note: Dimension methods measure elements as they are rendered. For a hidden element (display: none), jQuery temporarily shows it to measure it, which is slower; elements inside a hidden parent may return 0.

Test yourself

An element has width: 100px; padding: 10px; border: 2px with content-box. What does .outerWidth() return?

  1. 100
  2. 120
  3. 124
  4. 144

Which call returns how far the page is scrolled down?

  1. $(document).height()
  2. $(window).scrollTop()
  3. $(window).offset().top
  4. $("body").height()

What is the difference between .offset() and .position()?

  1. None
  2. .offset() is relative to the document, .position() to the offset parent
  3. .offset() includes margins, .position() does not
  4. .position() is a setter only