jQuery .height() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is .height()?

.height() gets or sets the content height of matched elements (since jQuery 1.0). The getter returns a unitless pixel Number — ideal for math. It also works on $(window) and $(document).

Remember
$("div").height()           // Number  →  content height (px)
$("div").height(100)        // set 100px
$("div").height("50%")      // set with CSS units
$(window).height()          // viewport height
$(document).height()        // full page height

.height()     → Number (unitless)
.css("height") → String ("80px")
NeedUse
Content height (Number)$el.height()
Include padding$el.innerHeight()
Include padding + border$el.outerHeight()

How to Use .height()

Pick getter or setter, then choose the box edge you really need.

  1. Select the element(s) with jQuery.
  2. Call .height() with no args to read a unitless pixel Number.
  3. Pass a Number (pixels) or CSS string to set height on every match.
  4. Use the callback form when each element needs a different value.
PieceRequired?Role
jQuery selectionYesElement(s), window, or document
value / functionSetter onlyNew height (Number, string, or per-element fn)
Return—Getter → Number; setter → jQuery (chainable)

Pseudocode

Pseudocode
function height(selection, value):
    if value is missing:
        el = first matched element
        return contentHeight(el) as Number   // no "px"

    for each el in selection:
        if value is a function:
            newH = value(index, currentHeight)
        else:
            newH = value
        set CSS height of el to newH        // Number → pixels
    return selection

Cheat Sheet

Keep these facts handy while measuring vertical size.

TopicDetail
Syntax.height() / .height(value) / .height(fn)
Since1.0 (callback setter: 1.4.1)
Getter returnsUnitless pixel Number (may be fractional)
MeasuresContent height (padding & border excluded)
box-sizingStill reports content height (extra work if border-box)
Window / document$(window).height() / $(document).height()
vs .css("height")Number vs string with units

Live Preview

Pick an action against the sample box and see what .height() returns or sets.

Browser demo of .height() — same idea as the examples below.

Result
$("#box").height()
→ 60

typeof result → "number"

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Get Element, Document, and Window Height

Official-style reads: paragraph, full page, and viewport.

jQuery
function showHeight(label, height) {
  console.log(label + ": " + height + "px");
}

showHeight("paragraph", $("p").height());
showHeight("document", $(document).height());
showHeight("window", $(window).height());
Try It Yourself

How It Works

  1. Element. $("p").height() returns content height as a Number.
  2. Document. $(document).height() is the full scrollable page height.
  3. Window. $(window).height() is the visible viewport height.
  4. Units. All three getters are unitless pixels — append "px" only when displaying.

Example 2 — Set Height with a Number

Pass a Number to set pixel height; chain other methods afterward.

jQuery
$("#box").height(30);

console.log($("#box").height());  // 30
Try It Yourself

How It Works

  1. Setter. .height(30) sets CSS height to 30 pixels on every match.
  2. Assumption. A bare Number means pixels; strings can use "50%", "2em", etc.
  3. Chain. The setter returns the jQuery object, so you can call .css() next.
  4. Verify. Call .height() again to read the new content height.

Example 3 — .height() vs .css("height")

See why the unitless Number is easier for math.

jQuery
var $box = $("#box");

console.log($box.height());           // 80  (Number)
console.log($box.css("height"));      // "80px" (String)
console.log($box.height() * 2);       // 160 — ready for math
Try It Yourself

How It Works

  1. Dimension helper. .height() strips units and returns a Number.
  2. CSS getter. .css("height") keeps the computed string (often with px).
  3. Math. Multiply the Number directly — no parseInt needed.
  4. Rule. Prefer .height() for calculations; use .css() for literal CSS values.

Example 4 — Callback Setter (Since 1.4.1)

Give each matched element a different height from its index.

jQuery
$(".bar").height(function (index) {
  return 30 + index * 25;
});

$(".bar").each(function (i) {
  console.log("bar " + i + ": " + $(this).height() + "px");
});
// bar 0: 30px
// bar 1: 55px
// bar 2: 80px
Try It Yourself

How It Works

  1. Callback. jQuery calls your function once per matched element.
  2. Args. First argument is the zero-based index; second is the current height.
  3. Return. Return a Number (or CSS string) to set that element’s height.
  4. Pattern. Great for bar charts and staggered panels without a manual loop.

Edge Cases & Pitfalls

Check these before trusting .height() in layout code.

hidden

Hidden elements can report 0

Measure while the element is visible. Do not rely on temporary show/hide measurement.

box

border-box still returns content height

Since 1.8, jQuery may subtract padding/border to keep the content-height contract — that costs extra work. Use .css("height") if you only need the CSS property string.

fraction

Values may be fractional

Sub-pixel layouts can return numbers like 80.5. Round only when your UI needs integers.

tags

Avoid style / script tags

Calling .height() on those tags is unreliable and discouraged by the official API.

Browser Support

.height() has shipped since jQuery 1.0 (callback setter since 1.4.1) and works in all modern browsers with jQuery loaded.

✓ jQuery 1.0+

.height()

Core dimension API for content height — getter returns a unitless Number; setter accepts Number, string, or function.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.height() Excellent

Bottom line: Use .height() for content-height math. Prefer .innerHeight() / .outerHeight() when padding or border must be included.

Performance Notes

ApproachCostBest for
.height()Layout readContent-height math
.css("height")Cheaper readCSS string / avoid border-box math
Repeated reads in a loopCan thrash layoutCache the Number first

Dimension getters force layout. Read once, reuse the Number, and avoid calling .height() inside tight animation frames unless needed.

Key Takeaways

  • Role: get/set content height as a unitless pixel Number.
  • Math: prefer .height() over parsing .css("height").
  • Box edges: use innerHeight / outerHeight when padding or border matters.
  • Safety: measure visible elements; expect fractional pixels.

One line: .height() is your content-height Number — get for math, set with a Number/string/callback, and switch to inner/outer when the box edge changes.

Frequently Asked Questions

.height() with no arguments returns the content height of the first matched element as a unitless pixel Number. .height(value) or .height(function) sets the CSS height on every matched element and returns the jQuery object for chaining.
.height() returns a Number like 80 — ready for math. .css('height') returns a string such as '80px' or 'auto'. Prefer .height() for calculations and animations; use .css('height') when you need the literal CSS value.
No. .height() measures content height only. Use .innerHeight() for padding, and .outerHeight() for padding plus border (pass true to include margin).
$(window).height() is the viewport height. $(document).height() is the full scrollable page height. Both return unitless Numbers.
Since jQuery 1.4.1, .height(function(index, height)) runs once per matched element. Return a Number or CSS string to set that element's height. Handy for staggered layouts and bar charts.
No. Values can be wrong (often 0) when the element or a parent is hidden. Measure while visible. Fractional pixels are possible — do not assume an integer.

Next: .width()

Learn the horizontal twin of .height() for content width.

.width() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful