I'm looking for the simplest and cross browser way to convert html element css margin value to absolute pixels. Say for instance the margin is "auto". I can't use any js framework.
To calculate profit margin, start with your gross profit, which is the difference between revenue and COGS. Then, find the percentage of the revenue that is the gross profit. To find this, divide your gross profit by revenue. Multiply the total by 100 and voila—you have your margin percentage.
You want to use the window.getComputedStyle
method:
var style = window.getComputedStyle(element, null);
// style.marginLeft
Here's a working demo: http://jsfiddle.net/VxccZ/
Update
For IE 8 and older (that do not support this method), use the currentStyle
property of your element:
var style = element.currentStyle;
// style.marginLeft
To summarize:
var getMarginLeft = function (element) {
var style;
if (window.getComputedStyle) { style = window.getComputedStyle(element, null); }
else { style = element.currentStyle; }
return style.marginLeft;
};
I'm not 100% sure that currentStyle
features a numeric value for margin: auto
. You will have to try that yourself. I cannot do that myself on the Mac.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With