View Full Version : Style vs. currentStyle

12-27-2002, 10:15 PM

I am getting more and more into actually reading about CSS and javascript rather than just using code from tutorials that I don't necessarily understand. I have figured out a lot of stuff, but I have become stumped over the difference between currentStyle and Style. I have read up on currentStyle over at MSDN (http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/currentstyle.asp ) but I am still confused.

I guess I am trying to figure out if I specify CSS in the head section of my page, i.e.:

<style type="text/css">
.content {font-color: #990033;}

What is currentStyle going to return compared to what just flat out style will return?

12-27-2002, 10:21 PM
it says it on that page:

The currentStyle object returns the cascaded styles on an element, but the style object returns only the styles that have been applied inline on an element through the style attribute

12-27-2002, 10:28 PM
Hey brothercake,

I know it says that there, but I guess my real problem is that I am not sure what that means. I think it says that if I don't directly specify the style on a certain tag but that tag is being styled because it is inside a different tag that is specified a style then Style will return nothing, but currentStyle will return the style that is being cascaded onto my no-styled tag (hmm, I hope that made sense :)). But I am not sure if that is correct...

12-28-2002, 12:41 AM
it means that if you go

<div style="color:green"></div>

you can retrieve the color property with obj.style.color

But if you had defined that color using a class name and a stylesheet, you would use currentStyle to retrieve it.

I believe there's also a property "getComputedStyle" which returns the overall result (ergo computed style) of an element's styling from external CSS and inline. Not too sure about that though .. maybe someone else can fill in here ...

12-28-2002, 01:43 AM
document.defaultView.getComputedStyle(element, '').getPropertyValue('prop-name')

defaultView is defined in DOM2 Views, and extended by DOM2 CSS to include getComputedStyle.

Only Mozilla supports this much of the DOM2 specs. (Opera 7 might actually... I don't remember off hand).

12-29-2002, 04:22 PM
So - does that clear it up for you, or do you still have questions?

12-30-2002, 12:09 AM
Yeah, that did the trick, sorry it took me so long to reply though :)! Anyway, I think I get the whole currentStyle and style deal, but that whole getComputedStyle bit is a little much for me at this point. That's okay though because I don't feel a strong need to understand that right now!!