csscss-selectorsuserstyles

How to select * but exclude <body>? Or set <body> to default style?


Browsers usually set different default styles for elements. Users can also override the defaults using custom stylesheets.

I often override these default styles using my own styles, e.g.:

* {padding:0; margin:0; }

However, I wish to respect the default stylesheet for certain element attributes, e.g. the <body>'s default padding and margin. I tried:

*    { padding:0;       margin:0;       }
body { padding:default; margin:default; }

But it doesn't work. Neither does initial.

Is there a way to solve this problem by selecting all elements but excluding <body>?


Solution

  • Is there a way to solve this problem by selecting all elements but excluding <body>?

    Since everything that is displayed in the viewport consists of <html>, <body> and <body>'s contents by default, you can just select html, and all of body's descendants:

    html, body * { margin: 0; padding: 0; }
    

    default doesn't work because there is no such keyword. initial doesn't work because the initial margins are 0. It is not possible to reset a property to its browser-given default value after you have changed it using CSS.