Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the universal CSS selector with JavaScript?

I often use the CSS universal selector to reset the dimensions in my HTML document:

* {
    border: 0;
    margin: 0; 
    padding: 0; 
}

Can this be done with JavaScript too?

For normal HTML elements there is the style property. But how to speak to the universal selector?

like image 352
Benny Neugebauer Avatar asked Feb 09 '13 20:02

Benny Neugebauer


1 Answers

getElementsByTagName("*") will return all elements from DOM. Then you may set styles for each element in the collection:

var allElements = document.getElementsByTagName("*");
for (var i = 0, len = allElements.length; i < len; i++) {
    var element = allElements[i];
    // element.style.border = ...
}
like image 137
VisioN Avatar answered Sep 26 '22 23:09

VisioN