I am having a problem trying to find any specific details on how to properly write CSS rules in a stylesheet where the class or ID is nested within many other IDs and styles, e.g.
.mainbody #container #header #toprightsearch .searchbox {}
So here we have a searchbox class within a toprightsearch ID, in a header ID, in a container ID, in a mainbody class.
But it appears to work properly if I omit some of the IDs.
What is the correct way of listing these?
If I include all of the parents does it make it more specific?
Can it error on different browsers if I don’t include all?
And any additional information on this topic would be appreciated.
Thanks
If you include more parents, it does increase selector specificity. You shouldn’t have cross-browser problems omitting parents.
There is no correct number of parents to list; it depends on what you require for your markup. As you’re seeing,
selector1 selector2means aselector2at any level inside aselector1, and you can tune that for whatever behavior you need.In your example, you should list
.mainbody #container #header #toprightsearch .searchboxif what you mean is for the style to only apply to.searchboxes that are inside that entire hierarchy. If contrariwise you want.searchboxesthat exist other under conditions to get the same style, you should be less restrictive in the hierarchy. It’s only about what you’re trying to accomplish.Re comment: IDs produce more specificity, so omitting them reduces your rule’s specificity more.