Created
June 28, 2019 09:32
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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