Slide 1

Slide 1 text

The Joy Cecy Correa RailsConf 2019 CSS of

Slide 2

Slide 2 text

Hi! I’m Cecy Engineer at Thinkful

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

CSS in JS?

Slide 6

Slide 6 text

Tips for making CSS better

Slide 7

Slide 7 text

1. Base Need a strong foundation to build upon

Slide 8

Slide 8 text

2. Add happy trees Fill in more details

Slide 9

Slide 9 text

3. The future What’s coming next

Slide 10

Slide 10 text

Let’s get started!

Slide 11

Slide 11 text

1. Base Need a strong foundation to build upon

Slide 12

Slide 12 text

Address basic paint points

Slide 13

Slide 13 text

Pain point #1 Targeting a specific element

Slide 14

Slide 14 text

CSS specificity

Slide 15

Slide 15 text

p > .class > #id

Slide 16

Slide 16 text

CSS specificity with Star Wars! https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 17

Slide 17 text

https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 19

Slide 19 text

.foo https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 20

Slide 20 text

a.foo https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 21

Slide 21 text

p a .foo https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 22

Slide 22 text

.foo .bar https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 23

Slide 23 text

p .foo a .bar https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 24

Slide 24 text

#foo https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 25

Slide 25 text

a#foo https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 26

Slide 26 text

.foo a#bar https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 27

Slide 27 text

.foo .bar #baz https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 28

Slide 28 text

inline styles https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 29

Slide 29 text

!important https://stuffandnonsense.co.uk/archives/css_specificity_wars.html

Slide 30

Slide 30 text

When everything is !important nothing is

Slide 31

Slide 31 text

More at Stuff & Nonsense https://stuffandnonsense.co.uk/archives/ css_specificity_wars.html

Slide 32

Slide 32 text

More CSS specificity practice at: CSS Diner flukeout.github.io

Slide 33

Slide 33 text

Pain point #2 Code is not DRY, hard to read

Slide 34

Slide 34 text

Bad habits • Bad naming conventions • Not coding with reusability in mind

Slide 35

Slide 35 text

–Phil Karlton “There are only two hard things in Computer Science: cache invalidation and 
 NAMING THINGS”

Slide 36

Slide 36 text

Let’s break it down

Slide 37

Slide 37 text

Let’s break it down .box1 .box2 .box3

Slide 38

Slide 38 text

Let’s break it down .yellow-box .green-box .blue-box

Slide 39

Slide 39 text

Improvements • Find the common elements, group them • Create a style for exceptions • Use descriptive, yet reusable naming

Slide 40

Slide 40 text

Let’s break it down |—— .pricing-base —-| .blue .yellow .green

Slide 41

Slide 41 text

Let’s break it down |—— .pricing-base —-| .pricing-highlight .micro .large

Slide 42

Slide 42 text

Further reading:
 BEM
 Block, Element, Modifier https://css-tricks.com/bem-101/

Slide 43

Slide 43 text

BEM alternatives • Pleasant BEM • Suit CSS • ABEM

Slide 44

Slide 44 text

Pain point tip #3 Color

Slide 45

Slide 45 text

“make the button more blue”

Slide 46

Slide 46 text

How do hex color codes work?

Slide 47

Slide 47 text

#RRGGBB

Slide 48

Slide 48 text

#RGB

Slide 49

Slide 49 text

#00F 0 1 2 3 4 5 6 a b c d e f 100% 0%

Slide 50

Slide 50 text

blue

Slide 51

Slide 51 text

#FF0000 0 F 100% 0%

Slide 52

Slide 52 text

red

Slide 53

Slide 53 text

#00F 0 1 2 3 4 5 6 a b c d e f 100% 0%

Slide 54

Slide 54 text

Pro-tip Type ‘color picker’ into Google

Slide 55

Slide 55 text

2. Add happy trees Fill in more details

Slide 56

Slide 56 text

Level up #1
 SCSS

Slide 57

Slide 57 text

SCSS optimization: Break up your files

Slide 58

Slide 58 text

SCSS file structure • MODULES • Vars, Mixins • PARTIALS • header, sidebar, footer • breakdown further: typography • VENDOR • 3rd party code http://thesassway.com/beginner/how-to-structure-a-sass-project

Slide 59

Slide 59 text

Main SCSS file @import “modules/variables” @import “partials/header” @import “partials/sidebar” @import “partials/typography” …

Slide 60

Slide 60 text

Naming your vars!

Slide 61

Slide 61 text

Common mistake $red: #f4e242;

Slide 62

Slide 62 text

Better naming! $primary: #f4e242; $secondary: …; $tertiary: …;

Slide 63

Slide 63 text

Better naming! $timesNewRoman: ‘Times New Roman’; $body: ‘Times New Roman’; $headline: ‘…’; $subHead: ‘…’;

Slide 64

Slide 64 text

Powerful mixins!

Slide 65

Slide 65 text

Powerful mixins @mixin fontStyles($font, $size, $color) { font-family: $font; font-size: $size; color: $color; } … h1 { @include fontStyles($body, 1rem, black); }

Slide 66

Slide 66 text

Level up #2 Flex

Slide 67

Slide 67 text

Flex killed the clearfix star

Slide 68

Slide 68 text

What is flex? • Allows a parent container to fill its own space • Move items horizontally or vertically!

Slide 69

Slide 69 text

Example

Slide 70

Slide 70 text

Old way #container { width: 600px; } .sidebar { float: left; width: 20%; } .main { float: right; width: 80%; }

Slide 71

Slide 71 text

With flex #container { display: flex; } .sidebar { width: 20%; } .main { /* width calculated automatically */ }

Slide 72

Slide 72 text

Example in prod https://codepen.io/cecyc/pen/LXPGQL

Slide 73

Slide 73 text

More flex practice at: Flexbox Froggy https://flexboxfroggy.com

Slide 74

Slide 74 text

Level up #3 pseudo elements

Slide 75

Slide 75 text

pseudo classes • :last-of-type, :last-child • :nth-of-type, :nth-child

Slide 76

Slide 76 text

https://codepen.io/cecyc/pen/LvvaRN

Slide 77

Slide 77 text

pseudo classes • :checked • :active • :hover • :disabled

Slide 78

Slide 78 text

pseudo classes • :checked • :active • :hover • :disabled

Slide 79

Slide 79 text

https://codepen.io/cecyc/pen/yrrwXr

Slide 80

Slide 80 text

3. The future What’s coming next

Slide 81

Slide 81 text

Beyond #1 CSS Vars

Slide 82

Slide 82 text

Vars in CSS! Define them:
 --primary-color: blue; 
 Use them:
 color: var(--primary-color);

Slide 83

Slide 83 text

Var scoping Vars are global and local global :root { --primary-color: blue; }
 
 local .btn { --primary-color: red; }

Slide 84

Slide 84 text

Can I use?

Slide 85

Slide 85 text

Beyond #2 CSS Grid

Slide 86

Slide 86 text

CSS Grid • Tables, divs, floats are the old way • Flex is 1-dimensional (meant for smaller things) • Grid is 2-dimensional! Specifically made for layout!

Slide 87

Slide 87 text

Grid vs. Flex • Grid: Larger scale layout (page) • Flex: within a component (nav)

Slide 88

Slide 88 text

CSS Grid + Flex = ❤

Slide 89

Slide 89 text

Further reading:
 https://css-tricks.com/snippets/css/complete- guide-grid/ https://cssgridgarden.com

Slide 90

Slide 90 text

Beyond #3 CSS Houdini

Slide 91

Slide 91 text

“Extend CSS at CSS speeds” http://houdini.glitch.me/

Slide 92

Slide 92 text

“Much like Service Workers are a low-level JavaScript API for the browser's cache, Houdini introduces low-level JavaScript APIs for the browser's render engines” http://houdini.glitch.me/

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Further reading:
 houdini.glitch.me css-houdini.rocks github.com/GoogleChromeLabs/houdini-samples

Slide 96

Slide 96 text

We don’t make mistakes, just happy little accidents