Slide 1

Slide 1 text

Sass Syntactically Awesome Style Sheets Devstaff Heraklion • October 2016 Zaharenia Atzitzikaki • @sugarenia

Slide 2

Slide 2 text

Sass Syntactically Awesome Style Sheets Devstaff Heraklion • October 2016 Zaharenia Atzitzikaki • @sugarenia

Slide 3

Slide 3 text

I’m Zaharenia. Lead designer @ Workable

Slide 4

Slide 4 text

Sass? Why?

Slide 5

Slide 5 text

CSS is repetitive

Slide 6

Slide 6 text

CSS .module { ... } .module .title { ... } .module .title span { ... } .module .footer { ... }

Slide 7

Slide 7 text

CSS is very repetitive

Slide 8

Slide 8 text

CSS was not made for this

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Variables

Slide 11

Slide 11 text

Variables Functions

Slide 12

Slide 12 text

Variables Functions Modules

Slide 13

Slide 13 text

What is Sass?

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Sass // SCSS syntax $color: #f00; .link { color: $color; text-decoration: underline; &:hover { text-decoration: none; } } // Sass syntax $color: #f00 .link color: $color text-decoration: underline &:hover text-decoration: none

Slide 16

Slide 16 text

You can start now.

Slide 17

Slide 17 text

How to install

Slide 18

Slide 18 text

$> gem install sass

Slide 19

Slide 19 text

$> sass --watch scss:css

Slide 20

Slide 20 text

style.scss > style.css

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Don’t edit the .css file! PITFALL

Slide 24

Slide 24 text

Using Sass

Slide 25

Slide 25 text

Nesting

Slide 26

Slide 26 text

Sass .modal { background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.3); .modal-header { padding: 15px 20px; border-bottom: 1px solid #eee; } .modal-footer { padding: 14px 15px 15px; text-align: center; } }

Slide 27

Slide 27 text

CSS .modal { background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.3); } .modal .modal-header { padding: 15px 20px; border-bottom: 1px solid #eee; } .modal .modal-footer { padding: 14px 15px 15px; text-align: center; }

Slide 28

Slide 28 text

Don’t mimic DOM! PITFALL

Slide 29

Slide 29 text

Sass body { .container { .content { .articles { & > .post { .title { h1 { a { } } } .content { p { ... } ul { li { ... } }

Slide 30

Slide 30 text

Sass } h4 { a { ... } } p { a { ... } } ul { li { ... } } } } } } } }

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Parent reference

Slide 33

Slide 33 text

Sass .box { .box-section a { color: fuchsia; text-decoration: none; &:hover { text-decoration: underline; } &.disabled { color: grey; } } }

Slide 34

Slide 34 text

CSS .box .box-section a { color: fuchsia; text-decoration: none; } .box .box-section a:hover { text-decoration: underline; } .box .box-section a.disabled { color: grey; }

Slide 35

Slide 35 text

.box { … &-header { … } }

Slide 36

Slide 36 text

.box { … &-header { … } } .box { … } .box-header { … }

Slide 37

Slide 37 text

.box { … .checkout & { … } }

Slide 38

Slide 38 text

.box { … .checkout & { … } } .box { … } .checkout .box { … }

Slide 39

Slide 39 text

.box { … & + & { … } }

Slide 40

Slide 40 text

.box { … & + & { … } } .box { … } .box + .box { … }

Slide 41

Slide 41 text

Variables

Slide 42

Slide 42 text

Sass $color: #f00; $border: 1px solid $color; .link { color: $color; border-bottom: $border; }

Slide 43

Slide 43 text

Sass // Colors $color-link: #01579b; $color-text: #607d8b; $color-danger: #d32f2f; $color-success: #43a047; $color-alert: #ffca28;

Slide 44

Slide 44 text

Mixins

Slide 45

Slide 45 text

Sass @mixin box { background-color: #fff; border: 1px solid #ccc; border-radius: 4px; width: 100px; height: 100px; } .box { @include box; }

Slide 46

Slide 46 text

Sass @mixin box($width, $height: $width) { background-color: #fff; border: 1px solid #ccc; border-radius: 4px; width: $width; height: $height; } .box { @include box(100px, 300px); }

Slide 47

Slide 47 text

Don’t worry about bloat

Slide 48

Slide 48 text

Don’t use mixins for vendor prefixes PITFALL

Slide 49

Slide 49 text

Extends

Slide 50

Slide 50 text

This is a generic alert! This is a positive alert!

Slide 51

Slide 51 text

Sass .alert { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: $color-accent; @include shadow(0, 1px, 2px, rgba(0,0,0,.5)); @include rounded(10px); } .alert-positive { background: #9c3; }
...

Slide 52

Slide 52 text

Sass .alert { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: $color-accent; @include shadow(0, 1px, 2px, rgba(0,0,0,.5)); @include rounded(10px); } .alert-positive { @extend .alert; background: #9c3; }
...

Slide 53

Slide 53 text

CSS .alert, .alert-positive { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: yellow; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); border-radius: 10px; } .alert-positive { background: #9c3; }

Slide 54

Slide 54 text

Multiple extends will bite you PITFALL

Slide 55

Slide 55 text

Sass .top-navigation, .subnav, .sidenav, #header, .main-footer, .inbox__actions, .feed__item__selector input[type="checkbox"], .feed__item__main .avatar, .feed__item__main .control-faux, .feed__item__actions, .feed__item__title .js-visibility, .feed__item__preview__text, .scroll-shelf, .profile__toolbar, .profile__footer, .profile__section__header .download, .overlay__shelf,

Slide 56

Slide 56 text

Sass .timeline__item--import, .hashtag-add__field, .hashtag-add__toggle, .hashtag__remove, .ticker.hide, .followers, .dropdown-menu, .alert .btn-compact { ... }

Slide 57

Slide 57 text

Extending placeholders

Slide 58

Slide 58 text

Sass %alert { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: $color-accent; @include shadow(0, 1px, 2px, rgba(0,0,0,.5)); @include rounded(10px); } .alert-positive { @extend %alert; background: #9c3; } .alert-negative { @extend %alert; background: #c00; }

Slide 59

Slide 59 text

Sass .alert-positive, .alert-negative { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: $color-accent; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); border-radius: 10px; } .alert-positive { background: #9c3; } .alert-negative { background: #c00; }

Slide 60

Slide 60 text

Mixin or extend?

Slide 61

Slide 61 text

Sass %alert { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; @include shadow(0, 1px, 2px, rgba(0,0,0,.5)); @include rounded(10px); } .alert-positive { @extend %alert; background: #9c3; } .alert-negative { @extend %alert; background: #c00; }

Slide 62

Slide 62 text

CSS .alert-positive, .alert-negative { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); border-radius: 10px; } .alert-positive { background: #9c3; } .alert-negative { background: #c00; }

Slide 63

Slide 63 text

Sass @mixin alert($background: "yellow") { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: $background; @include shadow(0, 1px, 2px, rgba(0,0,0,.5)); @include rounded(10px); } .alert-positive { @include alert(#9c3); } .alert-negative { @include alert(#c00); }

Slide 64

Slide 64 text

CSS .alert-positive { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: #9c3; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); border-radius: 10px; } .alert-negative { padding: 15px; font-size: 1.2em; line-height: 1; color: #fff; background: #c00; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); border-radius: 10px; }

Slide 65

Slide 65 text

@extend for related modules @mixin when you need arguments RULE OF THUMB

Slide 66

Slide 66 text

@import

Slide 67

Slide 67 text

@import "variables"; @import "mixins"; @import "layout"; style.scss

Slide 68

Slide 68 text

_variables.scss _layout.scss _mixins.scss style.css

Slide 69

Slide 69 text

PITFALL The 4096 selector limit in IE

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Functions

Slide 72

Slide 72 text

Custom functions

Slide 73

Slide 73 text

Sass $grid-columns: 12; $grid-width: 960px; @function columns($cols) { @return (($grid-width / $grid-columns) * $cols) + “px”; } main { width: columns(9); // 720px } aside { width: columns(3); // 240px }

Slide 74

Slide 74 text

Sass @function black($opacity) { @return rgba(0, 0, 0, $opacity); } @function white($opacity) { @return rgba(255, 255, 255, $opacity); } .faded-text { color: black(0.5); } .faded-panel { background-color: white(0.3); }

Slide 75

Slide 75 text

Helper functions

Slide 76

Slide 76 text

Sass .button { background-image: linear-gradient( to bottom, lighten(#c00, 5%), darken(#c00, 5%) ); } darken/lighten

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Sass @mixin gradient-vertical($start-color: #555, $end-color: #333) { background-color: mix($start-color, $end-color, 60%); background-image: linear-gradient(to bottom, $start-color, $end-color); background-repeat: repeat-x; } .box { @include gradient-vertical(#f8af1e, #097380); } mix

Slide 79

Slide 79 text

With gradient support Without gradient support

Slide 80

Slide 80 text

Comments

Slide 81

Slide 81 text

Sass $variable: "hi!"; /* This is a comment which can span * several lines. * It can also make use of variables! #{$variable} */ .box { ... } // This is a single-line comment // It won't appear in the CSS output 
 a { ... }

Slide 82

Slide 82 text

Sass tools

Slide 83

Slide 83 text

Sassmeister

Slide 84

Slide 84 text

sassmeister.com

Slide 85

Slide 85 text

Linting

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Libraries & frameworks

Slide 89

Slide 89 text

compass-style.org

Slide 90

Slide 90 text

bourbon.io

Slide 91

Slide 91 text

neat.bourbon.io

Slide 92

Slide 92 text

bitters.bourbon.io

Slide 93

Slide 93 text

refills.bourbon.io

Slide 94

Slide 94 text

BONUS! Workable Style Guide https://github.com/Workable/css-style-guide

Slide 95

Slide 95 text

Devstaff Heraklion • October 2016 Zaharenia Atzitzikaki • @sugarenia Thanks! You rock.