Skip to content

Instantly share code, notes, and snippets.

@marco-kretz
Created July 27, 2018 07:46
Show Gist options
  • Save marco-kretz/c169d421244c749ae713bce8acd9f5d4 to your computer and use it in GitHub Desktop.
Save marco-kretz/c169d421244c749ae713bce8acd9f5d4 to your computer and use it in GitHub Desktop.
Get the absolute width of an element.
const getAbsoluteWidth = (element) => {
const style = window.getComputedStyle(element);
const attrs = ['width', 'padding-left', 'padding-right', 'margin-left', 'margin-right'];
return attrs
.map((k) => parseInt(style.getPropertyValue(k), 10))
.reduce((p, c) => p + c);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment