1

The goal is to define a custom CSS property value in an external style sheet.

Fiddle It

External CSS:

#myDiv {
  --myCustomProperty: 'myCustomValue';
}

Markup:

<html>
    <div id='myDiv'></div>
</html>

There is nothing in the CSS spec that says custom properties are invalid. However, the browser will render them as invalid, but they should, in theory, still be available by window.getComputedStyle or similar. Firebug shows the custom property and its value in the styles pane but its marked as overwritten.

Following snippet from: http://www.quirksmode.org/dom/getstyles.html

JavaScript:

function getStyle(el,styleProp) {
    var x = document.getElementById(el);
    if (x.currentStyle)
      var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
      var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
      return y;
}

var value = getStyle('myDiv', '--myCustomProperty');

alert(value);

Output should be the string "myCustomValue"

2 Answers2

0

This should work (Tested in Firefox 32).

    <html>
<head>

<style>
#myDiv {
  --myCustomProperty: 'myCustomValue';
}
</style>
</head>
<body>
<div id='myDiv'></div>
<script>
function getStyle(el,styleProp) {
    var x = document.getElementById(el);
    if (x.currentStyle)
      var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
      var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
      return y;
}

var value = getStyle('myDiv', '--myCustomProperty');

alert(value);
</script>
</body>
</html>

http://jsfiddle.net/tugvgs1z/1/ However, this does not work in Chrome and IE 11. There is a good discussion on CSS custom-property API on link.

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91
0

As discussed, maybe this will help you:

You could store the custom properties for elements in a .json file which you let people edit.

the structure could be something like:

{
    "element": 'myDiv',
    "properties": [
            {
                "propertyName": 'SomeName',
                "propertyValue" : 'SomeValue'
            },
            {
                "propertyName" : 'SomeOtherName',
                "propertyValue"  : 'SomeOtherValue'
            },

        ]

    }

You would then need to write a javascript function to find the correct value with the property for any given element. This will at least work in all browsers.

Examples of searching the json object can be found here: Searching JSON objects with JQuery

and you can load the json file into a variable using these methods: Loading local JSON file

It's probably still not an ideal solution, but it would work across browsers.

Community
  • 1
  • 1
JanR
  • 6,052
  • 3
  • 23
  • 30
  • Sounds good. Although not exactly what I was hoping for, it will serve as a possible fallback solution. – JavaScript_is_4_Real_men Oct 23 '14 at 05:23
  • it's difficult without completely understanding what you are trying to achieve. but the css option is definately not viable at this stage until the other browsers are on board – JanR Oct 23 '14 at 05:24