Skip to content

Instantly share code, notes, and snippets.

@daanta-real
Last active February 23, 2023 10:38
Show Gist options
  • Save daanta-real/b9f12d885f62fbd043b7c9fb6aa14d54 to your computer and use it in GitHub Desktop.
Save daanta-real/b9f12d885f62fbd043b7c9fb6aa14d54 to your computer and use it in GitHub Desktop.
Give dynamic rainbow colored borders to ALL the elements being found in query. Quite useful for testing CSS
// Give dynamic rainbow colored borders to ALL the elements being found in query. Quite useful for testing CSS
function rainbow(query, styles) {
var colors = ['Red', 'Lime', 'Blue', 'Yellow', 'Cyan', 'Magenta', 'Silver', 'Gray', 'Maroon', 'Olive', 'Green', 'Purple', 'Teal', 'Navy']
document.querySelectorAll(query).forEach(function(el) {
var rand = Math.floor(Math.random() * colors.length);
var color = colors[rand];
el.style.border = '2px ridge ' + color;
if(styles != undefined) {
for(var [key, val] of Object.entries(styles)) {
el.style[key] = val;
}
}
});
}
// Try this
rainbow("body *", {background: 'red'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment