• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Page Details / Get Width and Height of an Element

Get Width and Height of an Element

Get Width and Height of an Element

To get the width and height of an element, you use the offsetWidth and offsetHeight properties of the element:

const box = document.querySelector('.box');

const width = box.offsetWidth,
    height = box.offsetHeight;

The offsetWidth and offsetHeight includes the padding and border.

To get the width and height of an element without the border, you use the clientWidth and clientHeight properties:

const box = document.querySelector('.box');

const width = box.clientWidth,
    height = box.clientHeight;

Note that the clientWidth and clientHeight properties also include the paddings.

Source

https://www.javascripttutorial.net/dom/css/get-width-and-height-of-an-element/

Page Details

Related Snippets:

  • Find the distance from a given element to the top of the document
  • Copyright Notice
  • Reload on Resize
  • Get the size of an image

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers