Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Issue with currentStyle vs getComputedStyle

427 views
Skip to first unread message

rburg...@yahoo.com

unread,
Aug 28, 2011, 6:15:52 AM8/28/11
to
I am trying to get the width of an element according to it's CSS rules
(you can see the code I am using below). The problem is that
"getComputedStyle" returns a pixel value instead of "auto" for an
element with no CSS width value set. In Opera,
"elem.currentStyle['width']" returns "auto", but in firefox, it must
use "getComputedStyle" which returns something like "1149px".

It is vital for me to know what the actual CSS rule is. Is there
something besides getComputedStyle?

My end goal is to find the largest static-width element on the page.
If I cannot read stylesheet values - only rendered/computed values -
then how can I achieve this?

The code I'm currently using:

function getStyle(elem, name) {
if (elem.style[name]) {
return elem.style[name];
} else if (elem.currentStyle) {
return elem.currentStyle[name];
}
else if (document.defaultView &&
document.defaultView.getComputedStyle) {
name = name.replace(/([A-Z])/g, "-$1");
name = name.toLowerCase();
s = document.defaultView.getComputedStyle(elem, "");
return s && s.getPropertyValue(name);
} else {
return null;
}
}

Dave Royal

unread,
Aug 28, 2011, 11:33:38 AM8/28/11
to
rburg...@yahoo.com wrote:

> I am trying to get the width of an element according to it's CSS rules
> (you can see the code I am using below). The problem is that
> "getComputedStyle" returns a pixel value instead of "auto" for an
> element with no CSS width value set. In Opera,
> "elem.currentStyle['width']" returns "auto", but in firefox, it must
> use "getComputedStyle" which returns something like "1149px".
>
> It is vital for me to know what the actual CSS rule is. Is there
> something besides getComputedStyle?
>

https://developer.mozilla.org/en/CSS/used_value
says
"...a script can read only the final used values with
window.getComputedStyle."
--
(Remove any numerics from my email address.)

Message has been deleted

rburg...@yahoo.com

unread,
Aug 28, 2011, 5:37:53 PM8/28/11
to
> https://developer.mozilla.org/en/CSS/used_value
> says
> "...a script can read only the final used values with
> window.getComputedStyle."
> --

Fair enough. So then, to be clear, there is no way to determine if an
element has a static or floating width?

Dave Royal

unread,
Aug 28, 2011, 6:44:10 PM8/28/11
to
rburg...@yahoo.com wrote:

I am not an expert. From reading that and other pages I think that
what you want is what MDN calls 'specified value'. The pages do not
say you cannot get at that, but they do not mention a way to do it. In
my experience MDN would refer to such a method if there were one.

But to be sure, I would ask on the NG sailfish posted, or ask on Stack
Overflow.

0 new messages