D3: will selection.style ('font-size') always return in px?
The docs just say that selection.style returns "the current computed value".
I tried to specify the font size of the element in css using rem
s, and style('font-size')
it seems to return the corresponding size in px (for example '10.2px'
).
Is this behavior that I can rely on across browsers and no matter what units the font size is in the css?
source to share
To answer your question, let's take a look at the selection.style
source code:
export default function(name, value, priority) {
var node;
return arguments.length > 1
? this.each((value == null
? styleRemove : typeof value === "function"
? styleFunction
: styleConstant)(name, value, priority == null ? "" : priority))
: defaultView(node = this.node())
.getComputedStyle(node, null)
.getPropertyValue(name);
}
As you can see, it actually uses getComputedStyle
.
By reading the MDN documentation about getComputedStyle , we can read that:
The values ββreturned by getComputedStyle are known as allowed values. These are usually the same as computed CSS 2.1 values, but for some older properties like width, height, or padding, they are used instead.
Then, going to the documentation about computed values , we can read that:
The computation required to arrive at a computed value for a property typically involves converting relative values (such as em units or percentages) to absolute values . (emphasizes mine)
So since it's getComputedStyle
supported (basic) with Chrome, Edge, Firefox, Internet Explorer, Opera and Safari, we can say that the answer to your question ("Is this behavior I can rely on in browsers?") Seems to be yes ...
source to share