Selectors
Selectors are arguably the most important concept to grasp in modern CSS, even more than styling. Without selectors, there isn’t a “cascading” part of the style sheets. Incorrect selectors can cause even more problems. A general rule of thumb is to use about 2-3 selectors, and no more than 6. Too few selectors, and your CSS will likely affect more elements than intended. Too many, and your CSS becomes limited, confusing, and difficult to maintain.
Selectors are arguably the most important concept to grasp in modern CSS, even more than styling. Without selectors, there isn’t a “cascading” part of the style sheets. Incorrect selectors can cause even more problems. A general rule of thumb is to use about 2-3 selectors, and no more than 6. Too few selectors, and your CSS will likely affect more elements than intended. Too many, and your CSS becomes limited, confusing, and difficult to maintain.
Thus, the number one rule for selectors is:
Select classes at the lowest level, and make them as specific as possible, while still being reusable.
Otherwise, you’ll often find your CSS more difficult to maintain, and sometimes it’ll even have unintended consequences on other parts of your site. Just because CSS is loaded for a specific component, does not mean that it will not affect other elements on the same page.
This typically means we should try to:
Why is that? Preferring page-specific CSS imports vs. instance-specific:
A well planned and thought out CSS design is just as important as any other part of your site and is a major contributor to the success or failure of a project. When dealing with complex sites, you should really take the time to sit down and think about a strategy for your CSS, while keeping the above-mentioned points in mind.
Select classes at the lowest level, and make them as specific as possible, while still being reusable.
Otherwise, you’ll often find your CSS more difficult to maintain, and sometimes it’ll even have unintended consequences on other parts of your site. Just because CSS is loaded for a specific component, does not mean that it will not affect other elements on the same page.
This typically means we should try to:
- Design global site-level defaults
- Design CSS at a component level
- Override on a page-by-page basis as necessary
- Avoid #id selectors in CSS (limited reusability)
Why is that? Preferring page-specific CSS imports vs. instance-specific:
- The CSS itself is more specific (Which page is this limited CSS pointing to again?)
- Less confusing from a context standpoint (Where is this class in the component? Oh, it’s in some wrapper or parent that is not tightly coupled.)
- Much easier to maintain (If the parent class or layout on the page changes, then you have to refactor CSS.)
- Global default style
- AEM design-level CSS defaults
- Component CSS defaults using only markup in the component
- Tightly-coupled components CSS defaults
- Page-level/page components CSS defaults
- CSS for specific component instances (usually with loosely coupled wrapper)
A well planned and thought out CSS design is just as important as any other part of your site and is a major contributor to the success or failure of a project. When dealing with complex sites, you should really take the time to sit down and think about a strategy for your CSS, while keeping the above-mentioned points in mind.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.