The .height() method gets or sets the height of matched elements as a unitless pixel Number — ideal for math and animations. This tutorial covers getter and setter overloads, comparisons with .css("height"), .width(), .innerHeight(), and .outerHeight(), window and document heights, and practical accordion and bar-chart patterns.
01
Getter
.height() → Number
02
Setter
String or Number
03
vs css()
Number vs string
04
inner/outer
Padding & border
05
Callback
Since 1.4.1
06
Since 1.0
Core API
Fundamentals
Introduction
After learning to read and write CSS with .css(), you often need a simpler way to work with element dimensions — especially height. jQuery’s .height() returns a plain number you can multiply, add, or pass directly to .animate(), without parsing "80px" strings.
Available since jQuery 1.0, .height() works as both a getter (no arguments) and a setter (value or callback). The getter reads the content height of the first matched element; the setter applies a new height to every element in the set. It also works on $(window) and $(document) for viewport and page measurements.
Concept
Understanding the .height() Method
Given a jQuery object, .height() either returns the current content height of the first matched element as a unitless pixel Number (getter), or sets the CSS height property on every matched element (setter). Unlike .css("height"), the getter never returns a string with units — it is ready for arithmetic.
Think of .height() as a specialized ruler for the vertical content box. .innerHeight() extends the ruler through padding; .outerHeight() includes border (and optionally margin). Use .height() when you need clean numbers; reach for .css() when you need percentage heights, auto, or other CSS values.
💡
Beginner Tip
var h = $("div").height() gives you a Number like 80. $("div").css("height") gives you a String like "80px". For h * 2 or .animate({ height: h }), use .height().
Foundation
📝 Syntax
jQuery .height() has three argument forms — one getter and two setters:
Get — no arguments (since 1.0)
jQuery
.height() // -> Number
Returns the content height of the first matched element as a unitless pixel Number.
Works on elements, $(window), and $(document).
Equivalent to reading the inner content box — padding and border are excluded.
Set — String or Number (since 1.0)
jQuery
.height( value ) // -> jQuery
value — a pixel height as a Number (e.g. 30) or a String with units (e.g. "50%", "10em").
Numbers are treated as pixels and applied as inline height.
Returns the original jQuery object for chaining — e.g. .height(30).css("backgroundColor", "green").
Set — callback function (since 1.4.1)
jQuery
.height( function( index, height ) {
// return new height (Number or String)
} )
index — zero-based position of the element in the matched set.
height — current height of this element before the change.
Return a new height per element; jQuery applies it and returns the jQuery object.
Return value
Getter — unitless Number (pixels). May be fractional on sub-pixel layouts.
Setter — the original jQuery object for chaining.
Important note
⚠️
Hidden elements
.height() on display:none or hidden elements is unreliable — jQuery may return 0. Measure after showing the element, clone it off-screen, or use a visibility trick before reading.
Official jQuery API example
jQuery
function showHeight( element, height ) {
$( "div" ).text( "The height for the " + element + " is " + height + "px." );
}
showHeight( "paragraph", $( "p" ).height() );
showHeight( "document", $( document ).height() );
showHeight( "window", $( window ).height() );
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read element height (Number)
$("div").height()
Set height in pixels
$("div").height(100)
Set height with units
$("div").height("50%")
Per-element callback (1.4.1+)
$(".bar").height(function(i){ return 30 + i * 25; })
Viewport height
$(window).height()
Full page scroll height
$(document).height()
Include padding
$("div").innerHeight()
Include padding + border
$("div").outerHeight()
Compare
📋 .height() vs .css() vs .width() vs .innerHeight() vs .outerHeight()
Five dimension methods — content box, CSS strings, horizontal twin, and padding/border variants.
.height()
content H
Get/set content height — returns unitless Number
.css()
"80px"
General CSS getter/setter — height returns String with units
.width()
content W
Horizontal twin of .height() — same getter/setter pattern
.innerHeight()
+ pad
Content height plus padding — getter only on elements
.outerHeight()
+ border
Includes padding and border; pass true to add margin
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Examples 3–5 compare .css(), demonstrate callback setters, and build an accordion. Use DevTools or the Try-it links to run each snippet.
📚 Getting Started
Official jQuery demos for reading and setting height.
Example 1 — Official Demo: Get Paragraph, Document, and Window Height
Three buttons read heights from a paragraph, the full document, and the viewport.
Get Paragraph Height -> "The height for the paragraph is 42px."
Get Document Height -> full scrollable page height in px
Get Window Height -> visible viewport height in px
How It Works
No-argument .height() returns a unitless pixel Number. On elements it measures content height; on document it returns total page height; on window it returns the viewport.
Example 2 — Official Demo: Set Height to 30px on Click
Click an orange box once to shrink it and change its background.
.height() -> 80 (Number, unitless px)
.css("height") -> 80px (String with units)
Math: height * 2 -> 160px
No parseInt needed — multiply the Number directly
How It Works
.height() strips the unit and returns a Number. .css("height") returns the computed CSS string. For height * 2, use .height(); use parseInt(..., 10) only if you must read via .css().
Example 4 — Callback Bar Chart: index * 25 Heights
Each bar gets a taller height based on its index in the matched set.
Apply -> bar heights: 30px, 55px, 80px, 105px, 130px
Formula: 30 + index * 25 (index 0, 1, 2, 3, 4)
Callback runs once per .bar element
How It Works
Since jQuery 1.4.1, the callback setter receives index and the current height. Return a new value per element — ideal for staggered bar charts and indexed layouts without a manual loop.
Example 5 — Accordion: Animate Height Open and Close
Measure content height once, then animate between 0 and full height.
jQuery
var open = false;
var fullHeight = 0;
$( ".acc-head" ).on( "click", function() {
var $body = $( ".acc-body" );
if ( !fullHeight ) {
$body.css( "height", "auto" );
fullHeight = $body.height();
$body.height( 0 );
}
open = !open;
$body.animate({ height: open ? fullHeight : 0 }, 300 );
});
First click -> panel animates from 0 to measured fullHeight
Second click -> animates back to 0
.height() captures natural content size before collapsing
How It Works
Temporarily set height: auto, read the natural height with .height(), collapse to 0, then .animate() between 0 and the stored value. The unitless Number from .height() works directly in animation options.
Applications
🚀 Common Use Cases
Responsive layouts — $(window).height() to size full-screen sections or modals to the viewport.
Scroll progress — $(document).height() vs $(window).scrollTop() for read-progress bars.
Accordion panels — measure with .height(), animate open/closed with .animate({ height: ... }).
Equal-height columns — read each column’s .height(), find the max, set all to match.
Bar charts — callback setter .height(function(i){ return base + i * step; }) for indexed bar heights.
Dynamic spacing — var h = $el.height(); $next.css("marginTop", h + 10) without string parsing.
🧠 How .height() Reads and Writes Dimensions
1
Match target
jQuery object holds elements, window, or document.
Input
2
Detect mode
No args → getter. Value or function → setter on each element.
Route
3
Measure or apply
Getter reads content box via offsetHeight minus padding/border. Setter writes inline height.
DOM
4
📏
Return result
Getter → unitless pixel Number. Setter → same jQuery object for chaining.
Important
📝 Notes
Available since jQuery 1.0 — core dimension API alongside .width().
Getter returns a unitless pixel Number from the first matched element only.
Callback setter since 1.4.1 — function receives (index, height).
box-sizing: border-box affects CSS layout but .height() still reports the inner content box.
Unreliable on hidden (display:none) elements — may return 0.
Values can be fractional (e.g. 80.5) on sub-pixel or zoomed displays.
Setter returns the original jQuery object — e.g. .height(30).css("color","white").
Compatibility
Browser Support
.height() has been part of jQuery since 1.0+. The callback setter arrived in 1.4.1+. It wraps cross-browser dimension reads (offsetHeight, clientHeight, window/document APIs) with no browser-specific quirks beyond jQuery itself.
✓ jQuery 1.0+
jQuery .height()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.offsetHeight minus padding/border, or getBoundingClientRect().height — jQuery adds collection semantics, unitless Numbers, and setter chaining.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.height()Universal
Bottom line: Safe in any jQuery project. Use .height() for pixel math; use .css('height') for percentage or auto values.
Wrap Up
Conclusion
The jQuery .height() method gets or sets element height as a unitless pixel Number — the go-to dimension helper when you need math-ready values for layouts, animations, and comparisons. It complements .css(), which returns strings with units.
Remember the official demo: $( "p" ).height() reads content height, $( document ).height() measures the full page, and $( window ).height() returns the viewport. Use the callback form for per-element heights, and pair .height() with .animate() for smooth accordion panels.
Use .height() when you need a Number for math or .animate()
Use $(window).height() for viewport-based responsive layouts
Use the callback setter for per-element heights in one call
Reach for .innerHeight() or .outerHeight() when padding or border matters
Measure hidden content by temporarily showing or cloning off-screen
❌ Don’t
Parse .css("height") when .height() already returns a Number
Trust .height() on display:none elements — it may return 0
Assume .height() includes padding — use .innerHeight() instead
Expect the getter to read all matched elements — only the first
Forget that fractional values may need Math.round() for integer layout
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .height()
Unitless pixels — ready for math.
6
Core concepts
#01
Getter
Number
Read
=02
Setter
px / %
Write
css03
vs css
String
Compare
fn04
Callback
1.4.1+
Index
win05
Window
Viewport
Measure
hide06
Hidden
Unreliable
Caveat
❓ Frequently Asked Questions
Calling .height() with no arguments is the getter — it returns the current height of the first matched element as a unitless Number (pixels). Calling .height(value) or .height(function) is the setter — it sets the CSS height on every matched element and returns the original jQuery object for chaining. The getter works on elements, $(window), and $(document); setters apply to elements only.
.height() is a dimension helper: it always returns an integer or fractional pixel value as a plain Number with no unit suffix — ready for math like height * 2. .css('height') is a general CSS getter that returns a string such as '80px', '50%', or 'auto'. For calculations and animations, prefer .height(); use .css('height') when you need the literal computed CSS value or non-pixel units.
.height() measures the content height only — the area inside padding and border, equivalent to clientHeight without scrollbar width. .innerHeight() includes padding; .outerHeight() includes padding and border (and optionally margin). When box-sizing: border-box is set, the CSS height property includes padding and border, but .height() still reports the inner content box — so the numbers may not match what you wrote in CSS.
$(window).height() returns the viewport height — the visible browser window area in pixels. $(document).height() returns the full scrollable document height, including content below the fold. Both return unitless Numbers. Use window height for responsive layouts and full-screen overlays; use document height for scroll-position math or progress indicators tied to page length.
Since jQuery 1.4.1, you can pass a function: .height(function(index, currentHeight) { return newHeight; }). jQuery calls the function once per matched element, passing the zero-based index and the element's current height. Return a Number or string to set that element's height. Useful for bar charts, staggered layouts, and any pattern where each element needs a different computed height.
No — .height() on display:none or visibility:hidden elements is unreliable; jQuery may report 0 or an incorrect value because the element is not rendered. Temporarily show the element, clone it off-screen, or use .innerHeight() after making it visible to measure. Returned heights can be fractional (e.g. 80.5) on sub-pixel layouts or zoomed displays; round with Math.round() when you need integers for layout.
Did you know?
jQuery’s .height() has existed since version 1.0 — alongside .width() as one of the first dimension helpers. It deliberately returns a unitless Number so you can write $("div").height() * 2 without parseInt. The callback setter (1.4.1+) mirrors .css() and .width() — return a different height per index for bar charts and staggered layouts. For the full box including padding, .innerHeight() adds roughly 16px on a typical padded element — always pick the method that matches what you are measuring.