Slide 1

Slide 1 text

@mdo-ular CSS Ten guidelines for how I write CSS

Slide 2

Slide 2 text

Simple class structure I.

Slide 3

Slide 3 text

• Classes over everything else • Dashes separate important keywords • Obvious, not clever • No chaining

Slide 4

Slide 4 text

// Basic class structure .component {} .component-modifier {} .component-subcomponent {} .component-subcomponent-modifier {}

Slide 5

Slide 5 text

// In practice… .btn {} .btn-lg {} .btn-primary {} .tabs {} .tabs-link {}

Slide 6

Slide 6 text

Slide 7

Slide 7 text

Identifiable classes II.

Slide 8

Slide 8 text

• Avoid common keywords • Abbreviate, but don’t create a new language • Balance search-ability and readability

Slide 9

Slide 9 text

// Not so good .button {} .header {} // Better .btn {} .site-header {}

Slide 10

Slide 10 text

Base classes & prefixes III.

Slide 11

Slide 11 text

• Isolates common styles • Prefixes and modifiers handle variance • Avoids collisions with similar components

Slide 12

Slide 12 text

// Specific use cases // Not so good .danger {} .btn.danger {} // Better .text-danger {} .btn-danger {}

Slide 13

Slide 13 text

// Suite of classes // Shared styles .btn {} // Variations .btn-primary {} .btn-danger {}

Slide 14

Slide 14 text

Minimize overrides IV.

Slide 15

Slide 15 text

• Avoid shorthand • Rely on those modifier classes • Build for re-use

Slide 16

Slide 16 text

// Not as good .element { margin: 20px 0; } // Better .element { margin-top: 20px; margin-bottom: 20px; }

Slide 17

Slide 17 text

Keep it CSS-y V.

Slide 18

Slide 18 text

• CSS isn’t a programming language • Preprocessing clouds vision • Stick to variables, nesting, and mixins

Slide 19

Slide 19 text

// Consider this… .custom-btn { @extend .btn; margin: 20px auto; .container & { padding: 10px 20px; } }

Slide 20

Slide 20 text

// Requires .btn base class .custom-btn { margin: 20px auto; } // Large buttons for landing pages .custom-btn-lg { padding: 10px 20px; }

Slide 21

Slide 21 text

Minimize nesting VI.

Slide 22

Slide 22 text

• Usually unnecessary • Mostly used for increasing specificity • Makes code more fragile

Slide 23

Slide 23 text

// Good nesting .btn { &:hover {} &:active {} } // Bad nesting .parent { .child { .element & {} } }

Slide 24

Slide 24 text

Formatting matters VII.

Slide 25

Slide 25 text

Every line of code should appear to be written by a single person, no matter the number of contributors.

Slide 26

Slide 26 text

// Property order // http://codeguide.co .element { // 1. Positioning // 2. Box model (display, float, width, etc) // 3. Typography (font, line-height, text-*) // 4. Visuals (background, border, opacity) // 5. Misc (CSS3 properties) }

Slide 27

Slide 27 text

// Not so good .selector,.another-selector { padding:15px; margin:0px 0px 15px; background-color:rgba(0,0,0,.5); box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF; }

Slide 28

Slide 28 text

// Better .selector, .another-selector { padding: 15px; margin-bottom: 15px; background-color: rgba(0,0,0,0.5); box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff; }

Slide 29

Slide 29 text

Document guidelines VIII.

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Embrace utility classes IX.

Slide 34

Slide 34 text

• Single purpose • Immutable declarations and values • Obvious nomenclature

Slide 35

Slide 35 text

.left { float: left; } .right { float: right; } .text-danger { color: red; } .text-success { color: green; } .text-warning { color: orange; }

Slide 36

Slide 36 text

.muted-link { color: #777; &:hover { color: #4183c4; text-decoration: none; } }

Slide 37

Slide 37 text

[hidden] { display: none !important; }

Slide 38

Slide 38 text

Automate and track CSS X.

Slide 39

Slide 39 text

• Linters and validators • CI tools with GitHub • Grunt and Gulp • Stats and reporting

Slide 40

Slide 40 text

× Failed — 1 failed and 3 successful checks

Slide 41

Slide 41 text

$ rake assets:stats +-----------------------+-----------+---------------+-------------+ | Bundle Stats | +-----------------------+-----------+---------------+-------------+ | name | selectors | minified | +gzip | +-----------------------+-----------+---------------+-------------+ | frameworks.js | 24 | 743.39 KB | 189.10 KB | | github.js | 636 | 562.11 KB | 127.63 KB | | github2.css | 3232 | 293.85 KB | 48.26 KB | | github.css | 2872 | 250.29 KB | 44.42 KB | | mobile.js | 40 | 125.94 KB | 37.32 KB | | mobile.css | 1336 | 106.14 KB | 19.26 KB | +-----------------------+-----------+---------------+-------------+

Slide 42

Slide 42 text

# Parker Report - Total Stylesheet Size: 3.0598kb - Total Media Queries: 1 - Total Rules: 403 - Total Selectors: 581 - Total Declarations: 999 - Total Unique Colors: 79 https://github.com/katiefenn/parker

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Bottom line?

Slide 45

Slide 45 text

• Simplicity conquers all • Focus on what’s between the curly braces • Document and evolve guidelines with your team

Slide 46

Slide 46 text

Thanks, nerds!