CSS Isolation
function copyStyles(sourceClass, targetClass) {
// Find an element with the source class
var sourceElement = document.querySelector('.' + sourceClass);
if (!sourceElement) {
console.warn('No element found with class:', sourceClass);
return;
}
// Get computed styles of the source element
var styles = window.getComputedStyle(sourceElement);
// Create CSS string from computed styles
var cssText = Array.from(styles).map(key => `${key}:${styles.getPropertyValue(key)}`).join(';');
// Create a style tag and append to head
var styleTag = document.createElement('style');
document.head.appendChild(styleTag);
// Add the new class with copied styles
styleTag.sheet.insertRule(`.${targetClass} { ${cssText} }`, 0);
}
// Usage
copyStyles('foo', 'bar');Last updated