Skip to content

Instantly share code, notes, and snippets.

@codebymikey
Created June 28, 2019 09:32
Show Gist options
  • Save codebymikey/cc7d8b3c99c51a9a3896b912f956cb9f to your computer and use it in GitHub Desktop.
Save codebymikey/cc7d8b3c99c51a9a3896b912f956cb9f to your computer and use it in GitHub Desktop.
Generate computed css styles for all browsers. Puts the output into a textarea on the page (so it can be easily copied).
// Generate computed styles to compare differences between two
function generateComputedStyles(selector, pseudoEl) {
// https://stackoverflow.com/a/26878397
var styles = [], css, el = document.querySelector(selector);
if (el) {
var computedStyle = window.getComputedStyle(el, pseudoEl);
for (var i = computedStyle.length; i--;) {
var propertyName = computedStyle[i];
var propertyValue = computedStyle.getPropertyValue(propertyName);
if (!propertyValue) {
continue; // Skip empty values
}
//propertyName = propertyName.replace(/(^(ms|webkit)|[A-Z])/g, function (x) { return '-' + (x.toLowerCase()) });
styles.push(propertyName + ': ' + propertyValue);
}
var t = document.getElementById('css-styles');
if (!t) {
t = document.createElement('textarea');
t.id = 'css-styles';
t.setAttribute('readonly', '');
document.body.appendChild(t);
}
css = t.value = styles.sort().join(';\n');
// Attempt to copy the styles.
//t.select(); document.execCommand('copy');
}
return css;
}
//generateComputedStyles('#element-selector', ':before');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment