Using Mootools Element.Dimensions I can get the computed size, in pixels, of any element. However, I can find no way of telling whether an element has been sized using pixel or percentage values (other than in the special case of its having an inline style).
Is there a sensible way of doing this? The only solution I can think of (which is so hideous that it barely deserves the name) is to walk through the document stylesheets, looking for selectors that match the target element and then looking through the declared styles for the target propety.
Background
I'm attempting to replace all textareas of a certain class with CKEditor instances. Ideally, textareas with 100% width would be replaced by similarly styled editor instances - so they would scale on window resize - while fixed size textareas would be replaced by fixed sized editors.
Yes, I could just give them a different class (which I will do if there's no nice solution), but ideally I'd like to be able to drop in my CKEditor script and have everything just work without having to tweak the HTML.
I've migrated and updated my answer from a closed duplicate:
How to tell if an element has a fluid width
Not wanting to go down the route of scanning style declerations I found that this works... but unfortunately only for firefox :( To me it would make sense that if the element has nothing to compute it's width against (i.e. it's not part of the document flow) it should return it's original value - which is what FireFox does:
(have not tested for IE)
Webkit and Opera however return a blank value - yet again another instance of where I agree with FireFox's implementation and frown at the others.
update 2
Ok, not a fan of being defeated by computers ;) so have come up with this function -- totally over the top, but it does seem to work. Again I have yet to test this on IE as I don't have a Windows machine to hand at the moment. It's annoying when the original FireFox-only version is quite succinct, but the logic here is sound - it falls back to what a normal human would do in testing if something is stretchy.
In IE,
element.currentStyle.width
. In many other browsers,getComputedStyle(element, null).getPropertyValue('width')
.I doubt it can be done. You described the reason quite accurately: We have access to this kind of information only if it is in the element's style attribute. The only way I can think of is enlarge the parent container a bit and see if the textarea grows proportionately in size, but this is probably not always feasible, and probably hard to make cross-browser functional.
I'd be highly interested in more positive answers, though.
Not that familiar with Motools but in jQuery you can do it.
Check here for a live demo http://jsbin.com/ewuqa
Or check this it handles also multiple matching CSS rules but only returns the correct value (the only thing I didn't bother to handle is if
!important
is set).Included JS
Own functions
CSS
HTML
It can be done. Looking at this question (Why does getComputedStyle return 'auto' for pixels values right after element creation?) gives us the hint.
If you set the element's style to display = none and then call getComputedStyle you will get the calculated value (percentage width or 'auto' or whatever the stylesheets apply to the element) instead of the pixel width.
Working code jsfiddle https://jsfiddle.net/wtxayrnm/1/
It should be noted that this will cause the layout to be re-rendered but, depending on your situation, it's probably a simpler solution than traversing all the stylesheet rules or cloning the element (which can cause some cascading rules to not be applied).