7

Some things are unclear to me:

var myDiv = document.getElementById("myDiv");
var computedStyle = window.getComputedStyle(myDiv);

1) Isn't it possible to directly get global border color of a div if there is only one color, the same for each side:

computedStyle.getPropertyValue("border-color");

Instead of doing:

computedStyle.getPropertyValue("border-left-color");

OR

computedStyle.getPropertyValue("border-right-color");

OR

computedStyle.getPropertyValue("border-top-color");

...

2) When having style properties in a CSS file, they are only accessible through the getComputedStyle method and not via the style property like style properties defined inline, via a style attribute in the div, I'm right?

myDiv.style.getPropertyValue("border-left-color");

This will not work.

3) If we want to set a style property, we have to use the style attribute of the element, is it not possible using the computed style object?

computedStyle.setProperty("border-color", "yellowgreen", null);

I thought using the style attribute was the "old way to do", like using the inline style attribute or using object.style.property = "value" to set a style property in Javascript.

Thanks.

jsFiddle: http://jsfiddle.net/pgtFR/12/

4
  • You need a magic.
    – Jashwant
    Commented May 20, 2012 at 18:17
  • 2
    I'm looking for the native way, pure JS to do this. Thanks.
    – baptx
    Commented May 20, 2012 at 18:18
  • 2
    @Jashwant: jQuery does no magic, and especially not the one requested here...
    – Bergi
    Commented May 20, 2012 at 18:51
  • I dunno, it depends on your definition of "magic"; personally I think jQuery is pretty damn magical :-) But yeah I'm not aware of anything jQuery does that would specifically solve this post. Commented May 20, 2012 at 18:56

3 Answers 3

7

1) Isn't it possible to directly get global border color of a div if there is only one color, the same for each side:

Yes, its possible to get the computed style by just using the shorthand property name. I tried the example you have shared on jsfiddle and computedStyle.getPropertyValue("border-color") does return (265,65,0) which is the rgb code for orange as expected.

2) When having style properties in a CSS file, they are only accessible through the getComputedStyle method and not via the style property like style properties defined inline, via a style attribute in the div, I'm right?

Yes. getComputedStyle returns the final computed style value by the browser after applying external, internal and inline style rules. .style attribute only refers to the inline style for the element.

3) If we want to set a style property, we have to use the style attribute of the element, is it not possible using the computed style object?

No. According to this document, getComputedStyle returns a CSSStyleDeclaration object which is read-only.

2
  • Thanks, very useful answer! You're right, computedStyle.getPropertyValue("border-color") works but not in Firefox and it returns rgb(255, 165, 0) not (265,65,0). I've noticed that in Chrome, myDiv.style.setProperty("border-color", "yellowgreen", null); with alert(myDiv.style.getPropertyValue("border-left-color")); returns "rgb(154, 205, 50)" and not "yellowgreen" wich is I think the correct/standard way it should be displayed. I've seen many times that Chrome doesn't respect standards as it should be, so maybe Firefox is right.
    – baptx
    Commented May 20, 2012 at 19:06
  • For information, it is possible to access some CSS properties like width and height via element.style even if they were defined in a stylesheet instead of inline: stackoverflow.com/questions/34378543/…
    – baptx
    Commented Nov 19, 2017 at 21:59
5

Isn't it possible to directly get global border color of a div if there is only one color, the same for each side

Yes and No. The spec describes two methods:

  • getPropertyCSSValue() returns a CSSValue of a single CSS Property. It does not work with shorthand properties.
  • getPropertyValue() returns a DOMString, and works also for shorthand properties. But you need to be careful when there are different borders, the string will represent all of them.

When having style properties in a CSS file, they are only accessible through the getComputedStyle method

No. They are also accessible through document.styleSheets (spec), and can be changed with the StyleSheet interface.

...and not via the style property like style properties defined inline, via a style attribute in the div, I'm right?

Yes. The .style property represents only the style declaration in the style attribute (inline styles).

If we want to set a style property, we have to use the style attribute of the element

I guess you mean a CSS property. You can also influence the computed style by setting classes on your element (or anything else that applies other styles through a stylesheet). Or you can create stylesheets dynamically, and they will be applied on the document. You can also set the style attribute of an element, but usually you will use the CSSStyleDeclaration interface exposed by the .style property.

is it not possible using the computed style object?

Yes. The spec says that the returned "CSSStyleDeclaration is read-only and contains only absolute values".

3
  • Thanks for those great answers. Yes I meant a CSS property, we always have to use .style attribute to set a property value?
    – baptx
    Commented May 20, 2012 at 19:32
  • Are you sure that getPropertyValue() works for shorthand properties? See this question stackoverflow.com/q/40830568/1325646
    – pomber
    Commented Dec 2, 2016 at 13:04
  • @pomber Yes, but a CSSStyleDeclaration doesn't necessarily contain shorthand properties. It seems getComputedStyle doesn't compute shorthands, for example, but if you retrieve a .style or something you should be able to access them.
    – Bergi
    Commented Dec 2, 2016 at 15:12
1

Ok, first off let's address this:

I thought using the style attribute was the "old way to do", like using the inline style attribute or using object.style.property = "value" to set a style property in Javascript.

The style property in JS is very different from inline styles in HTML. Inline styles in HTML are bad (IMHO) because:

  1. They are rough on your designer; if you change blue to lightBlue, and you had a class "blue", you have one place to change (your stylesheet). If instead you had a document full of style='color:blue' ... well now you get to have some fun with using the Linux sed command to do a massive find/replace ;-)

  2. Classes work better performance-wise. For one thing, page load-wise, style='color:blue' is six more characters than class='blue'. When you start having multiple classes/styles, and lots of elements with all those, it (kind of) adds up. Similarly, once you enter JS land, changing classes on things is slightly faster than changing styles directly. PPK did a study on this awhile back on his quirksmode.org blog.

But the thing is, browsers have changed a lot since PPK did that study, and changing classes at best saves a few ms vs. changing styles. Similarly, the page size will change with classes vs. styles, but with today's bandwidth the change won't be significant unless you have some truly horrible markup.

So, at the end of the day, classes and stylesheets are preferred primarily for reason #1; trying to maintain a consistent look, or even a not-fugly-but-inconsistent look, is basically almost impossible with inline styles. Maybe if you do a huge amount of server-side processing to generate those inline styles ... but I wouldn't recommend it.

However, none of that precludes using the JS property "style" though. In fact, if you look at the source for jQuery, you'll see that it's filled with uses of .style. And jQuery isn't just the most popular library on the web; it's all (originally) John Resig code, which means it's as good, quality-wise, as JS code gets.

So yeah, use style. Don't feel guilty about it :-)

Now, as for the rest of your questions, the short answer is that getComputedStyle is essentially the JS reference to the stylesheets, and as such you can't alter them (no to 3), they don't have inline styles (yes to 2) and I'm honestly not sure what the different browsers do in the 1) case; I wouldn't expect anything consistent, but you might get lucky.

2
  • Thanks! So I don't have to hesitate using style attribute contrary to inline style :) (and don't have the choice if I've understand correctly). But you might agree that it's better to use myDiv.style.setProperty("border-color", "blue", null); wich seems to be a DOM standard contrary to myDiv.style.borderColor = "blue";, right?
    – baptx
    Commented May 20, 2012 at 19:20
  • 1
    To be honest I was not (and, as of Googling the issue for about 15 minutes, still am not) aware of any benefits of using style.setProperty over style=. In fact to the contrary, style= is universal, whereas setProperty only seems to work on IE9+ (see help.dottoro.com/ljdpsdnb.php). So I'd think the style that works in any browser > the style that fails on IE7 and IE8 (not to mention those straggling IE6s). Commented May 21, 2012 at 15:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.