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

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

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

  • Top Window
  • Get the size of an image
  • Calculate the size of scrollbar
  • Check if the browser tab of the page is focused

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

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