If you're looking to borrow an existing property that accepts any string value, consider options like font-family
, counter-reset
, counter-increment
, and animation-name
. For example, with counter-reset
, you can achieve things like:
CSS
* {counter-reset: inherit;} /* by default, counter-reset does NOT inherit */
HTML
<div style="counter-reset: my-value; "> <!-- setting the value here -->
<p id="para">Some text.</p> <!-- retrieving the result of cascade here -->
</div>
JS
var p_styles = window.getComputedValue(document.getElementById("para"),null);
var p_reset_value = p_styles.counterReset;
var p_myvalue = p_reset_value.split(" ")[0]; /* computed value will be 'my-value 0' */
The only potential compatibility issue may arise if you are using counters in your code, due to unwanted side-effects of the CSS rule. It's also important to note that the value must adhere to the CSS definition of an "identifier". Giving a value with spaces will interpret it as two different counters, and enclosing it in quotes will render the CSS value invalid.
Exploring -webkit-locale
For those feeling adventurous, there's the option to utilize -webkit-locale
. As it inherits and accepts a single string value, it simplifies the process compared to the above method, eliminating the necessity for the CSS rule and the JS operation to split the computed value. Additionally, it removes the constraint of the value needing to be a CSS "identifier":
HTML
<div style="-webkit-locale: 'bar foo'; "> <!-- setting the value here -->
<p id="para">Some text.</p> <!-- retrieving the result of cascade here -->
</div>
JS
var style = window.getComputedValue(document.getElementById("para"),null);
var prop = style.webkitLocale; // "bar foo"
Hopefully, in the foreseeable future, we'll have CSS cascading variables available, rendering this workaround unnecessary.