CSS Optimization Guide

Get Started. It's Free
or sign up with your email address
Rocket clouds
CSS Optimization Guide by Mind Map: CSS Optimization Guide

1. Naming

1.1. Based on what they are, not what they look like

2. Keep it readable

2.1. keep your CSS very vanilla

2.1.1. Use LESS for only

2.1.1.1. imports

2.1.1.1.1. so that variables and mixins are available everywhere and it all outputs to a single file

2.1.1.2. data-uri

2.1.1.3. variables

2.1.1.4. some mixins

3. Components

3.1. Use the .component-descendant-descendant pattern

3.2. help encapsulate your CSS and prevent run-away cascading styles and keep things readable and maintainable

3.3. create hyphen-separated class for the descendant element like .header.image {...}

3.4. make sure every selector is a class

3.5. indent descendant classes so their relationship is even more obvious and it's easier to scan the file

3.6. Stateful things like :hover should be on the same level

3.7. Modifiers

3.7.1. use the .component-descendant.mod-modifier pattern

3.7.2. put it after the part of the component it mofidies

3.7.3. put modifiers on the same indention level as the selector it's modifying

3.7.4. Put modifiers at the bottom of the component file, after the original components

3.7.5. You'll often want to override a descendant of the modifier selector. Do that like so: .global-header-nav-item.mod-sign-up { background: hsl(120, 70%, 40%); color: #fff; .global-header-nav-item-text { font-weight: bold; } }

3.8. State

3.8.1. Use the .component-descendant.is-state pattern

3.8.2. Manipulate .is- classes in JavaScript (but not presentation classes)

3.8.3. Do not indent state classes

3.8.4. Should appear at the bottom of the file, after the original components and modifiers

3.9. Media Queries

3.9.1. Use media query variables in your component

3.9.1.1. each component should have its own file i.e. component.less

3.9.2. Include them inside your components

3.9.3. media-queries.less file

3.9.3.1. @highdensity: ~"only screen and (-webkit-min-device-pixel-ratio: 1.5)", ~"only screen and (min--moz-device-pixel-ratio: 1.5)", ~"only screen and (-o-min-device-pixel-ratio: 3/2)", ~"only screen and (min-device-pixel-ratio: 1.5)"; @small: ~"only screen and (max-width: 750px)"; @medium: ~"only screen and (min-width: 751px) and (max-width: 900px)"; @large: ~"only screen and (min-width: 901px) and (max-width: 1280px)"; @extra-large: ~"only screen and (min-width: 1281px)"; @print: ~"print";

3.9.4. Put media rules at the bottom of the component file

3.10. Keep it Encapsulated

3.10.1. Components shouldn't know anything about each other

3.10.2. If the smaller button can be reused in multiple places, add a modifier in the button component (like, .button.mod-small)

3.10.3. You should en up with a lot of components

3.10.3.1. Always be asking yourself if everything inside a component is absolutely related and can't be broken down into more components

3.10.3.2. If you start to have a lot of modifiers and descendants, it might be time to break it up

4. JavaScript

4.1. Separate style and behavior concerns by using .js- prefixed classes for behavior

4.2. The .js- class makes it clear to the next person changing this template that is being used for some JavaScript event and should be approached with caution

4.3. Use a descriptive class name

4.3.1. i.e.

4.3.1.1. .js-open-context-menu is more clear than .js-menu

4.3.1.2. the class should always include a verb since it's tied to an action

4.4. .js- classes should never appear in your stylesheets. They are for JavaScript only.

4.5. You will see state classes like .is-state in your JavaScript and your stylesheets as .component.is-state

5. Mixins

5.1. prefix mixins with .m- and only use them sparingly for shared styles

5.2. Mixins are share styles that are used in more than one compoanent

5.3. use sparingly

5.4. it should include the parenthesis to make ti more obvious that it's a mixin

6. Utilities

6.1. prefix utility classes with .u-

6.2. all utils should be in a single file

7. File Structure

7.1. include normalize.css at the top of the file

7.2. Include variables, mixins and utils (respectively)

7.3. Then include the components

7.3.1. Each component should have its own file and include all the necessary modifiers, states, and media queries

7.4. This should output a single app.css file

8. Style

8.1. Alphabetize declarations

8.2. No underscores or camelCase for selectors

8.3. Use shorthand when appropriate

8.4. Prefer hsl(a) over hex and rgb(a)

8.4.1. working with colors is easier with hsl, especially when making things lighter or darker, since you only have one variable to adjust

8.5. Use 2 spaces to indent, not 4 spaces and not tabs

8.6. In your markup, order classes like so <div class="component mod util state js"></div>

8.7. avoid body classes. There is rarely a need for them. Stick to modifiers within your component

9. Less CSS Pre-Processor