Re: Calculating the actual content width or height

On Dec 19, 1:15 am, dhtml <dhtmlkitc...@xxxxxxxxx> wrote:
David Mark wrote:
On Dec 17, 11:57 pm, dhtml <dhtmlkitc...@xxxxxxxxx> wrote:
David Mark wrote:
On Dec 16, 2:15 pm, "Ben Amada" <>
dhtml wrote:
No need to interpret.  Read the OP's response: "Thanx. Yes, that
worked."  And the scrollWidth property is not a mystery.
The fact that he said "thanks" does not change what he wrote.


That is
not what the OP asked for.

That is your opinion and it is wrong.  The OP's "width the content of
the div" is clearly not to be interpreted as the "content width"
defined in the specs.  Read further and it is clear he wanted the
scrollWidth.  To wit:


"(not the width or offset width of the div, but the actual width of
the contents, irrespective of whether the div itself is larger or
smaller than the actual content width)"

Why belabor the point?  Personally, I don't care if it is what he
wanted or not.  If not, he will likely follow-up.

If he wants the width of an element inside that div, then that would be
obtained off that element. It might be three divs, two floated, one
position: absolute.

He asked for the 'content' width. That's something clearly defined in

Do you really think he reads the specs?

I don't know. I think he's probably not reading this thread any more.

Right. There's really no telling what he wanted at this point.

As for obtaining the width of the content area, when the element has
padding, that is obtained by removing padding, then grabbing the

Yes. That is a good way to get around the issue of IE's lack of
computed style.

<html lang="en">
   #dummy {
     width: 140px;
     padding: 60px;

In this case the cascaded (all IE knows) and computed styles are the
same, so you have a viable alternative.

     background: lime;
   <script type="text/javascript">
window.onload = function() {
   var div = document.getElementById('dummy'),
       textContent = 'textContent' in div ? 'textContent' : 'innerText',
       s =,
       cssText = s.cssText,

Interesting. Whey not just save the inline padding style?

   s.padding = "0";
   scrollWidth = div.scrollWidth;
   s.cssText = s;
   div[textContent] = "content width: " + scrollWidth + " (expected 140)";}

I honestly can't see what this measurement does for you, but if you
need it, this is not a bad way to get it.


<div id='dummy'>


Of course, not giving the element padding in the first place would be

Yes. That would be a prudent design decision, resulting in smaller,
faster and simpler code. Not a big deal here, but it all adds up.

Relevant Pages