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"

13
  • which browser are you using? Commented Oct 23, 2014 at 4:29
  • @misaq Primary browser is Ch38 but have also tested in FF and IE various versions. Commented Oct 23, 2014 at 4:31
  • Is Ch38 a browser? I never heard of that. Commented Oct 23, 2014 at 4:34
  • @misaq Sorry, I mean Chrome. Commented Oct 23, 2014 at 4:38
  • 1
    check this on why this does not work in Chrome and IE 11 and how it can be worked around. Commented Oct 23, 2014 at 5:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sounds good. Although not exactly what I was hoping for, it will serve as a possible fallback solution.
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
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.

5 Comments

@misaq Nice catch on that typo but mine won't work. Which browser?
@misaq Don't know?? Wasn't me.
Firefox. Just check the fiddle link I put there.
I downvoted as it still returns null and not the "myCustomValue"
Doesnt' work in chrome, from what I can tell chrome is actually not reading the style on the dom element: it's listed as: 0: CSSStyleRulecssText: "#myDiv { }"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.