views:

68

answers:

3

hi everyone,

I was just curious: whats the difference between img.slider and .slider (when all of my images have class "slider").

I noticed that when i tried to target the images with class name "slider" via writing

.slider { margin-top: 10px; }

^ did not work

img.slider { margin-top: 10px; }

^ did work.

Why is this the case? Thanks!

+1  A: 

As reisio's link indicates, the more "specific" CSS rule overrides a less specific rule. Also, if you were to have the "slider" class on any element other than img, the img.slider rule would not apply.

Isaac Truett
+3  A: 

Specificity rules in CSS state that selectors with lower specificity, such as .slider, are overruled by selectors with higher specificity, such as img.slider. If you would like to learn more about how specificity is calculated and applied, consider reading the W3C section on specificity, at http://www.w3.org/TR/CSS21/cascade.html#specificity

cmrn
AKA: exactly what I said, but with many more words you have to read before getting to the link that does more than state the obvious. :p
reisio
as said in this answer specificity is the "problem". whenever you come across a style not being applied and are at a lost figure out what style that element is catching and either be more specific in your declaration, or rewrite the style its catching. Firebug saves 10000x time in figuring out which style is catching.
centr0