Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to get the height of a div using jQuery ?
Next article icon

How to get the div height using JavaScript ?

Last Updated : 26 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see How to get the div height using Javascript. We can do this by following ways:

  • Using the offsetHeight property.
  • Using the clientHeight property.
  • Using getBoundingClientRect() Method.

Method 1: Using the offsetHeight property: The offsetHeight property of an element is a read-only property and is used to return the height of an element in pixels. It includes any borders or padding of the element. This property can be used to find the height of the <div> element. 

Syntax:

divElement.offsetHeight

Example: In this example, we will use the offsetHeight property.

html




<style>
    .box {
        height: 100px;
        width: 100px;
        background-color: green;
        display: inline-block;
    }
</style>
<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    Get the height of
    <div>
        element using JavaScript
    </div>
</b>
 
<p>
    Click on the button to get the height
    of
    element
</p>
 
<div class="box"></div>
 
<p>
    Height of the div:
    <span class="output"></span>
</p>
 
<button onclick="getHeight()">
    Get Height
</button>
 
<script type="text/javascript">
    function getHeight() {
 
        divElement = document.querySelector(".box");
 
        elemHeight = divElement.offsetHeight;
 
        document.querySelector(".output").textContent
            = elemHeight + "px";
    }
</script>
 
 

Output:

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20230120173052/gfg.gif" alt="How to get the
height using JavaScript ?” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230120173052/gfg.gif 276w, ” sizes=”100vw” width=”276″>
How to get the <div> height using JavaScript ?

Method 2: Using clientHeight property: The clientHeight property of an element is a read-only property and is used to return the height of an element in pixels. It includes only the padding applied to the element and excludes the borders, margins, or horizontal scrollbars. This property can be used to find the height of the div element.

 Syntax:

divElement.clientHeight

Example: In this example, we will use the clientHeight property.

html




<!-- HTML elements to iterate -->
<style>
    .box {
        height: 100px;
        width: 100px;
        background-color: green;
        display: inline-block;
    }
</style>
<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    Get the height of
    <div>
        element using JavaScript
    </div>
</b>
 
<p>
    Click on the button to get the height
    of
<div>
    element
</div>
</p>
 
<div class="box"></div>
 
<p>
    Height of the div:
    <span class="output"></span>
</p>
 
<button onclick="getHeight()">
    Get Height
</button>
 
<script type="text/javascript">
    function getHeight() {
 
        divElement = document.querySelector(".box");
 
        elemHeight = divElement.clientHeight;
 
        document.querySelector(".output").textContent
            = elemHeight + "px";
    }
</script>
 
 

Output:

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20230120173052/gfg.gif" alt="How to get the
height using JavaScript ?” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230120173052/gfg.gif 276w, ” sizes=”100vw” width=”276″>
How to get the <div> height using JavaScript ?

Method 3: Using getBoundingClientRect() Method: The getBoundingClientRect() method is used to return a DOMRect object which contains 8 properties related to the dimensions of the bounding rectangle. One of these properties of the DOMRect object is the height property. It returns the height of the DOMRect object. This property can be used to find the height of the div element. 

Syntax:

elemRect = divElement.getBoundingClientRect(); elemHeight = elemRect.height;

Example: In this example, we will see the use of getBoundingClientRect() Method

html




<style>
    .box {
        height: 100px;
        width: 100px;
        background-color: green;
        display:inline-block;
    }
</style>
<h1 style="color: green">
    GeeksforGeeks
</h1>
 
<b>
    Get the height of
    <div>
        element using JavaScript
    </div>
</b>
 
<p>
    Click on the button to get the height
    of
    <div>
        element
    </div>
</p>
 
<div class="box"></div>
 
<p>
    Height of the div:
    <span class="output"></span>
</p>
 
<button onclick="getHeight()">
    Get Height
</button>
 
<script type="text/javascript">
    function getHeight() {
         
        divElement = document.querySelector(".box");
     
        elemRect = divElement.getBoundingClientRect();
     
        elemHeight = elemRect.height;
     
        document.querySelector(".output").textContent
                = elemHeight + "px";
    }
</script>
 
 

Output:

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20230120173052/gfg.gif" alt="How to get the
height using JavaScript ?” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230120173052/gfg.gif 276w, ” sizes=”100vw” width=”276″>
How to get the <div> height using JavaScript ?


Next Article
How to get the height of a div using jQuery ?
author
sayantanm19
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to get the height of scroll bar using JavaScript ?
    To get the height of scroll bar we could use different approaches. In this article, we are given an HTML document and the task is to get the height of the scrollbar using JavaScript. Following are the different approaches to solving this problem which are discussed below: Table of Content Using Cont
    3 min read
  • How to get the height of a div using jQuery ?
    In this article, we will learn how to get the height of a div using jQuery. In jQuery, height method is used to get the height of any element in HTML. The height method sets and returns the height of the HTML elements. Method 1: The height() method returns the first matched element's height, But the
    3 min read
  • How to get the Width and Height of an Image using JavaScript?
    Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height. Syntax for width:let width = this.width;Syntax for height:let height = this.height;Example 1: This example selects the image and then
    2 min read
  • How to get the image size (height & width) using JavaScript?
    To get the size of an image (height and width), Element.clientHeight and Element.clientWidth properties are used. Element.clientHeight: We can access the inner height of an element with this property. This height includes the padding but not the margin and border. Element.clientWidth: We can access
    2 min read
  • How to get the height of device screen in JavaScript ?
    Given an HTML document which is running on a device. The task is to find the height of the working screen device using JavaScript. Prerequisite - How to get the width of device screen in JavaScript ? Example 1: This example uses window.innerHeight property to get the height of the device screen. The
    2 min read
  • How to calculate Width and Height of the Window using JavaScript ?
    In this article, we will know to calculate the width & the height of the window using Javascript. Given an HTML document that is running on a Window & we need to find the height and width of the window. The Window innerWidth property is used for returning the width of a window’s content area
    2 min read
  • How to find the height of a text in HTML canvas using JavaScript ?
    In this article, we will find the height of the text canvas using JavaScript. We have two approaches to find the height of a text in HTML canvas using JavaScript which are described below: Approach 1: In the following example, the height attribute of the HTML canvas is used. First set the font in pt
    2 min read
  • How to detect virtual keyboard using JavaScript ?
    This article, we are given an HTML document, the task is to detect a virtual keyboard if it pops up on the device's screen. A virtual keyboard is a tool that helps in the input of characters without the use of a physical keyboard. It is widely used in touchscreen devices. Approach: Unfortunately, th
    2 min read
  • How to get the Element's Actual Width and Height in JavaScript?
    In JavaScript, it is often necessary to retrieve the actual width and height of an HTML element. This information can be useful in a variety of scenarios, such as setting the size of an element dynamically, centering an element on the page, or determining the size of an element for animation purpose
    6 min read
  • How to get the Width of Scroll bar using JavaScript?
    Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences