Yes, you can. This is defined by CSS Cascade:
Each property declaration applied to an element contributes a
declared value for that property associated with the
element.
These values are then processed by the cascade to choose a single
“winning value”.
The cascaded value represents the result of the
cascade: it is the declared value that wins the cascade (is
sorted first in the output of the cascade).
Then, if you use
height: 8em;
height: 8rem;
there will be 2 declared values for the property height: 8em and 8rem. The latter will will the cascade, becoming the cascaded value.
If some browser does not support one of them, the other one will be the only declared value, so it will win the cascade.
If some browser does not support any of them, the output of the cascade will be the empty list, and there won't be any cascaded value. The specified value will be the result of the defaulting processes (the initial value in this case, because height is not an inherited property).
However, with JavaScript there is a problem. setProperty, used under the hood by camel case IDL properties in .style, uses the set a CSS declaration algorithm, which overwrites existing declarations:
If property is a case-sensitive match for a property name of a CSS declaration in declarations, let declaration be that CSS
declaration.
Otherwise, append a new CSS declaration with the property name property to declarations and let declaration be that CSS declaration.
There are some workarounds:
Use cssText instead of camel case IDL attributes:
testElem.style.cssText += "; height: 12em; height: 12rem; ";
Check the style after setting it. If you get the empty string, it means it wasn't recognized.
testElem.style.height = "12rem";
if(!testElem.style.height) testElem.style.height = "12em";
Don't set the styles in the inline style declaration. Create a new stylesheet instead, or add some class to the element to trigger some CSS from an existing stylesheet.
pxandremand any browser which cannot useremwill fallback to usingpx.