homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / CSS
Forum Library, Charter, Moderators: not2easy

CSS Forum

Why does the * work?
Why does using the * selector work on margins and padding

 1:38 pm on Nov 4, 2009 (gmt 0)


I have found that by using the this style at the begining of my style sheet my page resolves well aligned in many different browsers but if I leave it out I have to compensate in the style for div spacing problems.

* {
margin: 0;
padding: 0;

Why does this work and where is if documented?

If I put the same style in the html or body style it does not work the same way.




 2:29 pm on Nov 4, 2009 (gmt 0)

Welcome to WebmasterWorld! This is defined in the CSS specification [w3.org]. * is the Universal Selector and matches ANY element. It's like a wildcard. If you have a rule like this:

* { /* ... */ }

That will match every element in your document, while if you apply the style like this:

html, body { /* ... */ }

that will match ONLY the html and body elements (thus, child elements of body might still have different default padding/margin).


 2:44 pm on Nov 4, 2009 (gmt 0)

That code fragments sets *all* margins and paddings to zero, on all elements in your html.
It has the lowest possible specificity so it can't override anything but the defaults of the browser itself.

The * is the "universal selector":


 3:02 pm on Nov 4, 2009 (gmt 0)

Thanks Fotiman,

So an old DOS command finds its way to style sheets.

Well like I said...it works.

Sometimes if I can't get things to space and align right I throw in an empty div with an inline clear both style but with the * I at least can start the page at 0.



 3:13 pm on Nov 4, 2009 (gmt 0)

You might also want to research "reset style sheets". Basically, the notion is that you want to "reset" the browser defaults to your own defaults.


 3:33 pm on Nov 4, 2009 (gmt 0)

I certainly like the idea of always starting a page in all browsers at the same place.

I was starting my style sheets with something like this...

h1, h2, h3, p, ul {
padding: 0;
margin: 0;

Now using the * selector to set the page to zero is just easier.

Every browser I have tested this in it works.



 4:37 pm on Nov 4, 2009 (gmt 0)

There is a growing debate about whether you should use the global reset, the yahoo style reset or simply resetting just the elements you use.

Each has its own pro's and con's. Becoming aware of the cons for each would give you a better understanding of best choice for you.

I personally use the * reset because I know everything is reset to 0 so for me bug fixing becomes easier. I like the idea of the yahoo reset but I might miss something that I otherwise wouldn't have.

Each to their own but I would strongly urge you to research all options and then form an opinion.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / CSS
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved