2

That would go like this * {margin:0; padding:0;} in CSS.

VoidPointer
  • 17,651
  • 15
  • 54
  • 58
Frank Furd
  • 541
  • 2
  • 4
  • 11

2 Answers2

7

To get the (first) stylesheet object use

document.styleSheets[0]

To access the (first) rule in the stylesheet use on of:

document.styleSheets[0].cssRules[0] // firefox
document.styleSheets[0].rules[0]    // IE

You can add rules with

insertRule(rule, index)                 // firefox
addRule(selector, declaration, [index]) // IE

Thus, to do what you describe in firefox:

document.styleSheets[0].insertRule("*{margin:0; padding:0;}", 0) 

And to do it in IE:

document.styleSheets[0].addRule("*", "margin:0; padding:0;", 0)

See also: Dom StyleSheet Object.

VoidPointer
  • 17,651
  • 15
  • 54
  • 58
  • 1
    see http://stackoverflow.com/questions/714655/how-can-i-set-a-css-hover-on-a-dom-created-element-in-javascript/714717#714717 and http://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript/524721#524721 for two different approaches on dynamically creating the stylesheet if you don't want to append to an existing one – Christoph Feb 06 '10 at 14:30
-1

If you'd like to change the padding and margin for the body element:

document.body.setAttribute('padding', '0');
document.body.setAttribute('margin', '0');
Leventix
  • 3,789
  • 1
  • 32
  • 41