Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Measure the content area of an element

How can you get the dimensions of the content area of a DOM element?

Strangely couldn't find a question/answer to this. Many questions about measuring DOM elements as a whole, but none about the content area itself.

Example:

div {
  width: 100px;
  height: 200px;

  padding: 10px;
  border: 5px;
  margin: 15px;

  box-sizing: border-box;
}

Now somethings get you various parts of the box model, but nothing seems to give you the content:

const elem = document.querySelector('div');

elem.offsetWidth; // content + padding + border
elem.clientWidth; // content + padding

window.getComputedStyle(elem); // Returns an object with width padding and border as strings like "15px".

window.getBoundingClientRect(); // Gives width and height of total box-model excluding margin if sizing is border-box. 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You'll need to subtract the computed paddings from the client width and height:

const elem = document.getElementById('elem');

const computedStyles = window.getComputedStyle(elem)

const extraWidthOffset =  +computedStyles.getPropertyValue("padding-left").slice(0, -2) + +computedStyles.getPropertyValue("padding-right").slice(0, -2)

const extraHeightOffset =  +computedStyles.getPropertyValue("padding-top").slice(0, -2) + +computedStyles.getPropertyValue("padding-bottom").slice(0, -2)

const contentWidth = elem.clientWidth - extraWidthOffset
const contentHeight = elem.clientWidth - extraHeightOffset

console.log(contentWidth, contentHeight)
#elem {
  width: 100px;
  height: 200px;

  padding: 10px;
  border: 5px;
  margin: 15px;

  box-sizing: border-box;
}
<div id="elem">
  <p>Content</p>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!