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
CSS nesting provides the ability to nest one style rule inside another, with the selector of the child rule relative to the selector of the parent rule. Similar behavior previously required a CSS pre-processor.
To nest a selector, you simply separate them with a space. This will make paragraph tags inside main have one font size, and paragraph tags inside either header or footer have another font size. Descendant selectors target all elements inside the other, no matter how deeply nested it is.
When we use a CSS preprocessor like Sass or Less, we can nest a CSS style rule within another rule to write clean and understandable code. This nesting rule is not supported yet in native CSS. At the moment, it is a working draft and only available for discussion.
We can very easily define nested rules in LESS. Nested rules are defined as a set of CSS properties that allow the properties of one class to be used for another class and contain the class name as its property. In LESS, you can use class or ID selectors to declare mixin in the same way as CSS styles.
.searchbox {}
Styles anything with .searchbox
.mainbody .searchbox{}
Styles any .searchbox that descends from any .mainbody, direct child, grandchild, quadruple great grandchild, doesn't matter.
#toprightsearch > .searchbox {}
Styles only .searchboxes that are direct children of #toprightsearch
#container * .searchbox {}
Styles .searchbox's that are grandchild or later of #container.
Here's a good document on the topic: w3C selectors
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 selector2
means a selector2
at any level inside a selector1
, and you can tune that for whatever behavior you need.
In your example, you should list .mainbody #container #header #toprightsearch .searchbox
if what you mean is for the style to only apply to .searchbox
es that are inside that entire hierarchy. If contrariwise you want .searchboxes
that 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With