I migrated and updated my answer from a closed duplicate:
How to determine if an element has a fluid width
Not wanting to follow the path of scanning style decals, I found that this works ... but, unfortunately, only for firefox :( I would be wise that if an element has nothing to calculate its width (i.e. part document flow), it should return to its original value - this is what FireFox does:
function isElementFluid(elm){ var clone = elm.cloneNode(false); if( window.getComputedStyle ) { value = window.getComputedStyle(clone,null).width; } else if( clone.currentStyle ) { value = clone.currentStyle.width; } return (value && String(value).indexOf('%') != -1 ); }
(not tested for IE)
Webkit and Opera, however, return a null value, but again another example of where I agree with the implementation of FireFox and are unhappy with others.
update 2
Well, not a fan of being defeated by computers;) so we came up with this feature - completely on top, but it seems to work. Again, I have yet to test this on IE, because at the moment I don't have a Windows machine. This is annoying when the original FireFox-only version is pretty brief, but the logic here is that it returns to what a normal person will do when testing if something stretches.
function isElementFluid(elm){ var wrapper, clone = elm.cloneNode(false), ow, p1, p2; if( window.getComputedStyle ) { value = window.getComputedStyle(clone,null).width; } else if( clone.currentStyle ) { value = clone.currentStyle.width; } /// the browsers that fail to work as Firefox does /// return an empty width value, so here we fall back. if ( !value ) { /// remove styles that can get in the way clone.style.margin = '0'; clone.style.padding = '0'; clone.style.maxWidth = 'none'; clone.style.minWidth = 'none'; /// create a wrapper that we can control, my reason for /// using an unknown element is that it stands less chance /// of being affected by stylesheets - this could be improved /// to avoid possible erroneous results by overriding more css /// attributes with inline styles. wrapper = document.createElement('wrapper'); wrapper.style.display = 'block'; wrapper.style.width = '500px'; wrapper.style.padding = '0'; wrapper.style.margin = '0'; wrapper.appendChild(clone); /// insert the element in the same location as our target elm.parentNode.insertBefore(wrapper,elm); /// store the clone calculated width ow = clone.offsetWidth; /// change the wrapper size once more wrapper.style.width = '600px'; /// if the new width is the same as before, most likely a fixed width if( clone.offsetWidth == ow ){ /// tidy up elm.parentNode.removeChild(wrapper); return false; } /// otherwise, calculate the percentages each time - if they /// match then it likely this is a fluid element else { p1 = Math.floor(100/500*ow); p2 = Math.floor(100/600*clone.offsetWidth); /// tidy up elm.parentNode.removeChild(wrapper); return (p1 == p2) ? Math.round(p1)+'%' : false; } } else { p1 = (value && String(value).indexOf('%') != -1); return p1 ? value : false; } }
Pebbl
source share