5

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?

3
  • @OneTrickPony This needs jQuery... Commented Feb 9, 2013 at 20:15
  • 1
    Why JavaScript at all? Commented Feb 9, 2013 at 20:16
  • I need it to init some JavaScript performance tests. Commented Feb 9, 2013 at 20:27

4 Answers 4

7

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 = ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

6

You don't need to iterate all the elements. You can demand this operation to the CSS engine of your browser. Something like that:

;(function(exports) {
  var style = document.querySelector("head")
                      .appendChild(document.createElement("style"));

  var styleSheet = document.styleSheets[document.styleSheets.length - 1];
  styleSheet.insertRule("* {}", 0);

  exports.universal = styleSheet.cssRules[0];
}(window));

From now, you have a window.universal object that you can use to style all the elements. For instance:

window.universal.style.border = "1px solid red";

Of course you don't need to create at runtime the <style> tag. You can always have that in plain HTML too.

You can test it running this snippet:

;(function(exports) {
  var style = document.querySelector("head")
                      .appendChild(document.createElement("style"));

  var styleSheet = document.styleSheets[document.styleSheets.length - 1];
  styleSheet.insertRule("* {}", 0);

  exports.universal = styleSheet.cssRules[0];
}(window));

console.log("universal" in window); // true

window.universal.style.border = "1px solid red";
<div>
  Hello
  <span>World</span>
</div>

5 Comments

Uncaught TypeError: window.universal is undefined showing in Firefox 86.0.1 when using window.universal.style.border = "1px solid red";
Did you run the script that creates the window.universal object? I ran in Firefox 86 and 88 and it's still working (although it's a very old code and I wouldn't do something like that nowadays). If you try to access to window.universal without the script above it won't work.
well, i added the code inside <script> tag in the main html file, probably in the header tag (changed the code, so don't remember) and after loading the page, that error shows in the browser console. Don't understand why.
I added a snippet in the original answer so you can test it. I don't know the details to your code so I cannot debug it. But it looks like when you try to access window.universal you don't have execute the snippet to create it yet.
thanks a lot, now it works, I probably missed the function workarounds it the previous code
2

In raw javascript you can do this:

document.getElementsByTagName('*')

but I wouldn't recommend adding css to all elements using js.

Comments

0

Thanks VisioN for the solution! I just remembered that you can do the same with the new JavaScript Query Selector API:

var allElements = document.querySelectorAll('*');

for (var i = 0; i < allElements.length; i++) {
  var element = allElements[i];        
  element.style.border = '0px';
  element.style.margin = '0px';
  element.style.padding = '0px';
}

1 Comment

Definitely you can use querySelectorAll, however you have to consider possible compatibility problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.