So, I have a #Wrapper
DIV that has a fixed width. Inside the DIV, I have another #Panel
DIV, which also has a fixed width:
<div id="Wrapper"> <p>...</p> <div id="Panel">Panel</div> <p>...</p> </div>
Sometimes the width of the panel is larger than that of Wrapper, and in these cases I would like to extend Wrapper with JavaScript so that it completes the panel perfectly.
Live demo: http://jsfiddle.net/H6rML/
I intended to use .scrollWidth
on a Wrapper to determine the width of the panel. However, the problem is that the Wrapper has horizontal padding, and .scrollWidth
for some reason only includes the left padding of the wrapper. So:
Wrapper.scrollWidth === Wrapper.LeftPadding + Panel.Width
So, considering:
#Wrapper { width: 200px; padding: 10px; } #Panel { width: 300px; }
Wrapper.scrollWidth
returns 310px
, which is not very useful. If .scrollWidth
did not contain spaces and only returned the width of the panel, I could work with this (I would add a manual addition to this value). If both gaskets were turned on, I could handle this as well. But why is only the left addition included? (By the way, this behavior is similar to a cross browser.)
A few additional notes:
I can not set the width to Wrapper directly. In my actual code, I set the width of the ancestor element that is several levels above Wrapper, and I use a custom API to set the width. That is why I need to get the full value of 320px
.
I need a solution that is independent of the contents of the Wrapper. In my demo, this is a panel, but in other scenarios there may be another element that overflows or even several elements. This is why I went from .scrollWidth
to Wrapper.
Is there a way to get the value of 320px
without having to manually add the correct padding to the .scrollWidth
value?
Btw, in accordance with the standard, right filling must be included:
The scrollWidth attribute should return the result of running these steps:
If the element does not have an associated CSS layout field, return zero and complete these steps.
If the element is the root element and the Document is not in quirks return max mode (width of the contents of the document, innerWidth value).
If the element is an element of the HTML body and the document is in quirks return max mode (width of the contents of the document, value innerWidth).
Returns the computed value of the padding-left property plus the computed value of padding-right plus the width of the content of the element.
Source: http://www.w3.org/TR/cssom-view/
Why don't browsers behave accordingly?
To further improve the clarity of my post, let me summarize two main questions:
(1) If the standard states that the correct padding should be included in the .scrollWidth
value, then why don't browsers behave accordingly?
(2) Is it possible to get the correct value ( 320px
in my case) without having to manually add the correct addition?
This question was answered in another thread.
The answer to this question is here: When the child element overflows horizontally, why is the incorrect filling of the parent element ignored?