jQuery .height() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

.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).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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")
CallWhat it does
.height()Learn jQuery .height(): get or set content height as a unitless pixel Number, compare with .css('height'), window and document heights, callback setter, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element(s) with jQuery.
Step 2Call .height() with no args to read a unitless pixel Number.
Step 3Pass a Number (pixels) or CSS string to set height on every match.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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() Method — Get and Set Element Width

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

.width() Method — Get and Set Element Width tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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