Slide 1

Slide 1 text

Taming your CSS Matthew Rudy Jacobs Wednesday 12th December 2012 @ Codeaholics

Slide 2

Slide 2 text

The Problem

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

䬞ჵ!!!

Slide 5

Slide 5 text

Wild CSS • 81 CSS files • 11 Subdirectories • 6978 lines of CSS • *NO* structure

Slide 6

Slide 6 text

The Solution

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

SMACSS!!!

Slide 9

Slide 9 text

SMACSS Written by Jonathan Snook online at http://smacss.com or to buy from PragProg

Slide 10

Slide 10 text

SMACSS gives us structure

Slide 11

Slide 11 text

Structure • Base styles • Layout styles • Module styles • State styles • Theme styles

Slide 12

Slide 12 text

BLMST!!!

Slide 13

Slide 13 text

Base styles • Reset.css • Default typography • Default colours • Usually just single element selectors

Slide 14

Slide 14 text

Base styles // body body { margin: 0; font-size: 14px; color: $navy; } // headings h1 { text-transform: uppercase; } h1, h2, h3 { font-weight: bold; color: $orange; } // link styles a { color: $ice; } a:hover { color: $navy; }

Slide 15

Slide 15 text

Bootstrap is a good BASE

Slide 16

Slide 16 text

Layout styles • Positioning • Sizing • Layout

Slide 17

Slide 17 text

Layout styles // basic structure #header { position: fixed; top: 0; height: 20px; } #sidebar { float: left; width: 20% } #content { float: right; width: 80% } // grid .row { clear: both; margin-left: -20px; } .span { float: left; margin-left: 20px; } // modals maybe? .modal { position: fixed; z-index: 9999; }

Slide 18

Slide 18 text

Bootstrap is a good LAYOUT

Slide 19

Slide 19 text

Module styles • Distinct visual elements • Correspond to your style guide elements • Submodules should use a prefix •

Slide 20

Slide 20 text

Module styles // a Module can contain elements .hero-unit { padding: 60px; } .hero-unit h1 { font-size: 60px; } // a Module can have Submodules .btn { display: inline-block; padding: 4px 14px; } .btn-large { padding: 9px 14px; font-size: 16px }

Slide 21

Slide 21 text

Bootstrap has some great MODULES

Slide 22

Slide 22 text

State styles • define state of a Layout or Module • prefixed with “is-” • live with their Layout or Module • correspond to javascript state changes

Slide 23

Slide 23 text

State styles // Layout states .is-collapsed { display: none; } .is-expanded { display: block; } // Module states .is-disabled { background: $grey } .is-active { background-color: $green } // or prefix the Module name .btn-is-disabled { background: $grey } .btn-is-active { background-color: $green }

Slide 24

Slide 24 text

Don’t know what to write here

Slide 25

Slide 25 text

Theme styles • Define distinct changes in the style of the site • User defined styles • Dark or Light

Slide 26

Slide 26 text

Theme styles • Define distinct changes in the style of the site • User defined styles • Dark or Light

Slide 27

Slide 27 text

Theme styles • Define distinct changes in the style of the site • User defined styles • Dark or Light

Slide 28

Slide 28 text

Forget about THEMES for now

Slide 29

Slide 29 text

SMACSS teaches us important practices

Slide 30

Slide 30 text

Avoid Coupling // the base module .well { border: 1px $grey; font-size: 14px; } // this is coupled #sidebar .well { font-size: 12px } // create a sub-module instead .well-small { font-size: 12px; }

Slide 31

Slide 31 text

!important • if you need to use important DON’T • a State is the only excusable use

Slide 32

Slide 32 text

Semantic & Specific // this is bad .hero-unit div { } // this is better .hero-unit .body { } // this is best .hero-unit-body { }

Slide 33

Slide 33 text

Maximum depth of 2 // this is bad .hero-unit header h1 small .icon { } // this is better .hero-unit .header-icon { } // this is worse? .hero-unit-header-icon { }

Slide 34

Slide 34 text

Module Structure // base Module .btn { ... } // Sub Modules .btn-large { ... } // Module states .btn-is-active { ... }

Slide 35

Slide 35 text

Media Queries // define the default display .span { float: left; margin-left: 20px; } // define the changes @media screen and (max-width: 400px) { .span { float: none; display: block; width: 100%; } }

Slide 36

Slide 36 text

Directory Structure base/ forms.css reset.css typography.css layout/ grid.css scaffold.css modules/ buttons.css hero-unit.css themes/ default.css inverted.css

Slide 37

Slide 37 text

Style Guides • a Section for each Module • an example of each State • an example of each Submodule • auto-generate with KSS?

Slide 38

Slide 38 text

You don’t have a style guide?

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Make One!!!

Slide 41

Slide 41 text

Thanks

Slide 42

Slide 42 text

Check out smacss.com

Slide 43

Slide 43 text

Follow me @matthewrudy

Slide 44

Slide 44 text

See you next Codeaholics!