• 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:

  • Check If the Document is Ready
  • Get all URL parameters from a query string
  • Copyright Notice
  • Viewport Width

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us