views:

24

answers:

4

Hello!

If I do this:

*{padding:0}
div#myDiv{padding:10px;}

Will that behave like any normal styling in CSS with the last style applied overriding others? i.e. The element with the myDiv ID will have 10px padding? Or do I have to specify !important?

Will this work on all popular browsers?

Thank you!

+1  A: 

Yes: the <div id="myDiv"> element will have a 10px padding, and this works on nearly every browser. Cascading means more specific selectors will override more general selectors.

The only time you'd really need to use !important is when you have a more general style that you want to take precedence over a more specific style, or when you have two different styles for the same selector, like to take advantage of quirks in older browsers that don't support !important.

Mark Trapp
Thanks for all the answers. I marked the first one as correct as all were and had no other criteria.I recently had a problem with *{color:black;} and .myClass{color:red;} where .myClass was being ignored and the all selector took priority, but this was in ActionScript 3 CSS, not HTML.Thanks again.
Francisc
A: 

i wouldn't recommend doing this. if you want to set padding for all elements to zero by default, i would recommend using a css reset page

but yes, that will work.

Jason
+1  A: 

If declared as

div {padding: 30px;}
div#pro { padding: 0px; }

Every div will have 30px of padding all around, except for div#pro, regardless of the order of the stylesheet, it's based on how specific you are with your declarations. More general specifications are overridden by more specific ones.

Robert
this isn't always true. the order can matter because sometimes things will have equivalent specificity, so whichever is first will take precedence.
Jason
In his example of `div` vs `div#whatever`, the latter will always win out, regardless of order.
Robert
+1  A: 

The all(*) selector isn't supported in legacy browsers. I believe it is supported in all modern browsers.

As for your specific padding on #myDiv, this should work. The cascading in CSS means that elements inherit properties from parents but these can be overridden by more speciffic selectors targeting child elements.

If you like to reset or normalize all crossbrowser CSS try Eric Meyers reset css sheet. I use this file as a start in all my CSS templates. Its a great help. Als be sure to define XHTML strict as a doctype, this will save you much aggrevation.

jpluijmers