Slide 1

Slide 1 text

Defensive Sass Modular styles for the modern web @johnwlong Function Pink, April 2013

Slide 2

Slide 2 text

Help & Feedback http://uservoice.com/touchpoint-toolkit

Slide 3

Slide 3 text

TheSassWay Latest news on Sass + Compass @ B

Slide 4

Slide 4 text

An Example

Slide 5

Slide 5 text

Simple Menu

Slide 6

Slide 6 text

1 http://bit.ly/menu-1

Slide 7

Slide 7 text

2 http://bit.ly/menu-2

Slide 8

Slide 8 text

3 http://bit.ly/menu-3

Slide 9

Slide 9 text

Think in terms of objects. Don’t think in terms of selectors. b

Slide 10

Slide 10 text

Parent-Child Parent .tableview Child .tableview-item Grandchild .tableview-item-cell

Slide 11

Slide 11 text

TableView

Slide 12

Slide 12 text

http://bit.ly/modular-tableview

Slide 13

Slide 13 text

http://bit.ly/modular-tableview

Slide 14

Slide 14 text

http://bit.ly/modular-tableview

Slide 15

Slide 15 text

Plural Parent Pattern Parent .faqs Child .faq Grandchild .faq-title

Slide 16

Slide 16 text

Parent-Child Used to declare (not enforce) object hierarchy for the DOM. b b b

Slide 17

Slide 17 text

Subclassing Object .item Subclass .ticket-item .article-item Child .item-title .ticket-item-title

Slide 18

Slide 18 text

Subclassing .item { ... } .ticket-item { @extend .item; }
...
.item { ... } .ticket-item { ... }
...
Standard CSS Extending Two classes One class

Slide 19

Slide 19 text

Subclasses Used to declare that one object should inherit the properties of another. b b

Slide 20

Slide 20 text

Contextual Object .item Contextual .ticket .item .article .item Child .item .title .ticket .item .title

Slide 21

Slide 21 text

Contextual .item { ... } .ticket .item { ... } .ticket .item .title { ... }

...

Slide 22

Slide 22 text

Subclassing vs. Contextual .item .item .ticket-item .article-item .ticket .item .article .item .item-title .ticket-item-title .item .title .ticket .item .title

Slide 23

Slide 23 text

Subclassing vs. Contextual Better for more modular design Sometimes better for things like themes

Slide 24

Slide 24 text

Subclassing vs. Contextual Subclassing is almost always better.

Slide 25

Slide 25 text

Modifiers Positive .has-menu Negative .no-border, .no-margin State .is-selected, .is-active Other .primary, .secondary

Slide 26

Slide 26 text

Generic Modifiers .is-selected { background: $highlight-color; } .is-hidden { display: none !important; } .float-left { float: left !important; } .float-right { float: right !important; } .mt0 { margin-top: 0 !important; } .ml0 { margin-left: 0 !important; } .ma0 { margin: 0 !important; } .clearfix { @include clearfix; } Important!

Slide 27

Slide 27 text

Specific Modifiers .button { &:hover { ... } &.primary { ... } &.secondary { ... } &.small { ... } &.large { ... } &.is-disabled, &[disabled] { ... } } Use nesting & the ampersand operator

Slide 28

Slide 28 text

Modifiers Used to change attributes or state on an existing object. b

Slide 29

Slide 29 text

Modifiers vs. Subclasses Better for small changes in visual attributes or state Better for major visual changes or for highly specific layout

Slide 30

Slide 30 text

Naming Scheme Object .tableview .ticket .noun Child .tableview-item .ticket-title .noun-noun Subclass .multiselect-tableview .priority-ticket .adjective-noun Modifier .is-selected .scrollable .prefix-adjective or .adjective

Slide 31

Slide 31 text

Modular Typography

Slide 32

Slide 32 text

Modular Typography http://bit.ly/modular-typography

Slide 33

Slide 33 text

Modular Typography .h1, .h2, .h3, .h4, .h5, .h6 { ... } .text-left { text-align: left !important; } .text-center { text-align: center !important; } .text-right { text-align: right !important; } .quiet { color: $quiet-color !important; } .loud { color: $loud-color !important; } .fixed { font-family: $fixed-font-family; } http://bit.ly/modular-typography

Slide 34

Slide 34 text

Modular Typography .typeset { h1 { @extend .h1; margin: 1em 0 0.5em; } h2 { @extend .h2; margin: 1em 0 0.5em; } h3 { @extend .h3; margin: 1em 0 0.5em; } p, ul, ol, pre { margin: 1em 0; } ul { list-style: disc; ... } pre, code { @extend .fixed; } ... } http://bit.ly/modular-typography

Slide 35

Slide 35 text

Modular Typography Scope general typography with a single class name. Use modifiers for one-offs. a

Slide 36

Slide 36 text

Reset

Slide 37

Slide 37 text

It’s easy @import “compass”; @include global-reset;

Slide 38

Slide 38 text

Reset vs. Normalize Zeros out styles on all elements. Normalizes styles across browsers so that all browsers share a similar stylesheet.

Slide 39

Slide 39 text

Reset vs. Normalize Reset is better because it allows you to use tags for semantic value instead of style.

Slide 40

Slide 40 text

Some surprises

Slide 41

Slide 41 text

IDs or tags Don’t style with (classes are better)

Slide 42

Slide 42 text

Contextual styles are mostly evil (avoid nesting)

Slide 43

Slide 43 text

More markup and less styles (Often much easier to maintain)

Slide 44

Slide 44 text

• SMACSS: http://smacss.com • Object Oriented CSS: http://github.com/stubbornella/oocss • Typeset: http://joshuarudd.github.io/typeset.css/ • The Sass Way: http://thesassway.com i See also

Slide 45

Slide 45 text

Thanks! http://bit.ly/defensive-sass-april-2013 http://codepen.io/collection/lmkqe