Repository: sezgi/CSS-Best-Practices Branch: master Commit: c1e5bb761e91 Files: 2 Total size: 6.3 KB Directory structure: gitextract_x251sfnh/ ├── LICENSE └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Writing and refactoring for clean CSS This note is meant as a guide for writing clean CSS, as well as refactoring bad CSS as you touch code. The problem is that people don't want to touch an old CSS style and break something. So bad styles stick around for a long time. We need to put in the extra effort of getting rid of those bad styles. ### General Guidelines Our highest priorities are: + Semantics (readability) + Modularity (reusability, flexibility) + Efficiency (performance) - Keep performance in mind, but don't worry too much about it. The main advantage of using performant selectors is that it increases readability and modularity. For example, `.container *` is terrible not just because of performance, but also because it hurts flexibility and requires future styles to use hacky overrides. - Steve Souders examined the performance impact of CSS selectors and determined that the delta between the best case and the worst case was 50ms. In other words, consider selector performance but don’t waste too much time on it. + http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/ - Nicole Sullivan's comment on the above article: "Micro-optimization of selectors is going a bit off track in a performance analysis of CSS. The focus should be on scalable CSS. There are two ways to measure 0(n) in CSS. What happens as you add more pages and modules to the site? Then you measure both file size, and HTTP requests. The way you write selectors has a huge impact on both of these that is much more significant than the time it takes the browser to process a reasonable number of selectors (even deeply nested)." ### Readability + Be specific with class names. - use `.profile-loading-indicator` instead of `.loading`. This is more reusable, less ambiguous, and easier to find in searches. + Namespace classnames. - use `.menu-wide` instead of `.menu.wide`. This is easier to reuse and find in searches as well, and harder to create hacky combinations and overrides. + Use prefixes to group things together. - e.g. `.sidebar-foo`, `.sidebar-bar` + Keep documenting. Whenever you add a new class or group of classes, leave comments to clarify what they're for, examples of where they're being used, etc. You want people to be able to read your CSS and understand your intentions, just as if you were writing in a programming language. ### Modularity + Avoid `!important`. People add it to override styles that are too specific. The correct solution is to avoid too many levels of nesting in the first place, so you never have to use `!important`. If you see too much nesting, this is a good place to refactor. If you have to use it, document your use of `!important`. + Write "object-oriented CSS". Whenever you add a rule, think of ways to make it modular and reusable. - Why "objected-oriented"? Because the same principles that apply to OO programming also apply here. + `button strong span .callout` is like an inheritance tree that's too deep. + It could have just been `button .callout` -- this way you can move `.callout` around more easily in the future and it's not tightly coupled with the DOM tree. - Separation of container from content: Children elements should not depend on parent element for styles. If the child needs an override, extend it with a new classname, e.g. `.alert-error,` instead of, e.g. `.followers .alert`. ```