Skip to content
the poor coder

jquery get div height

Poor Man
Published
Reading time
1 min read
On this page

Using jQuery to Get the Height of a Div#

If you're working with HTML and CSS, chances are you'll need to get the height of a <div> element at some point. Fortunately, jQuery makes it easy to do just that.

Method 1: Using .height()#

The simplest way to get the height of a <div> element is to use the .height() method in jQuery. This method returns the height of the first matched element in pixels.

Text
var divHeight = $('div').height();
console.log(divHeight); // Output: 300

In the example above, the .height() method is used to get the height of the first <div> element on the page. The height of the <div> element is then stored in the divHeight variable and logged to the console.

Method 2: Using .css()#

If you need to get the height of a <div> element that has been styled with CSS, you can use the .css() method in jQuery.

Text
var divHeight = $('div').css('height');
console.log(divHeight); // Output: "300px"

In the example above, the .css() method is used to get the value of the height property of the first <div> element on the page. The height of the <div> element, including the units (pixels), is then stored in the divHeight variable and logged to the console.

Method 3: Using .outerHeight()#

If you need to get the outer height of a <div> element (including padding, border, and margin), you can use the .outerHeight() method in jQuery.

Text
var divHeight = $('div').outerHeight();
console.log(divHeight); // Output: 350

In the example above, the .outerHeight() method is used to get the outer height of the first <div> element on the page. The outer height of the <div> element, including padding, border, and margin, is then stored in the divHeight variable and logged to the console.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC