Slide 1

Slide 1 text

SMACSS

Slide 2

Slide 2 text

Hi, there! I’m Luciano Battagliero @battaglr

Slide 3

Slide 3 text

Scalable and Modular Architecture for CSS MEANING

Slide 4

Slide 4 text

Jonathan Snook WHO

Slide 5

Slide 5 text

~2011 WHEN

Slide 6

Slide 6 text

A little bit of context BEM ~2009 OOCSS ~2010 SUIT CSS ~2012

Slide 7

Slide 7 text

What it’s not

Slide 8

Slide 8 text

A framework, a boilerplate or a library

Slide 9

Slide 9 text

There’s no code

Slide 10

Slide 10 text

What is it, then?

Slide 11

Slide 11 text

A series of guidelines, recommendations and advices

Slide 12

Slide 12 text

It’s a methodology

Slide 13

Slide 13 text

What’s the goal?

Slide 14

Slide 14 text

Author organized and structured code

Slide 15

Slide 15 text

Easier to build, maintain and scale

Slide 16

Slide 16 text

Okay, got it! Let’s dive into that “smacks” thing

Slide 17

Slide 17 text

Categoriza- tion

Slide 18

Slide 18 text

Every code-base needs some organization

Slide 19

Slide 19 text

One of the core aspects is categorization

Slide 20

Slide 20 text

There are five categories

Slide 21

Slide 21 text

Base, layout, module, state and theme

Slide 22

Slide 22 text

Base

Slide 23

Slide 23 text

The low-level defaults, or the global scope if you prefer it

Slide 24

Slide 24 text

body { margin: 0; }

Slide 25

Slide 25 text

b, strong { font-weight: bold; }

Slide 26

Slide 26 text

*, *::before, *::after { box-sizing: border-box; }

Slide 27

Slide 27 text

Using things like `reset.css` or `normalize.css` It’s okay to use them as long as you know what they do, and don’t remain as a black-box kind of stuff

Slide 28

Slide 28 text

Layout

Slide 29

Slide 29 text

The main building blocks of a page

Slide 30

Slide 30 text

Generally defining structure-related styles

Slide 31

Slide 31 text

Based on reuse, we can divide them into major and minor

Slide 32

Slide 32 text

Major layouts

Slide 33

Slide 33 text

#header { … }

Slide 34

Slide 34 text

#sidebar { … }

Slide 35

Slide 35 text

#footer { … }

Slide 36

Slide 36 text

To `id`, or not to `id` There’s no real advantage on using an `id` over a `class`, and you will be avoiding a potential specificity-related headache

Slide 37

Slide 37 text

Minor layouts

Slide 38

Slide 38 text

#header { … } .l-fixed #header { … }

Slide 39

Slide 39 text

.l-grid { … }

Slide 40

Slide 40 text

Namespacing With the exception of `id` selectors, the recommendation is to namespace layouts using an `l-` prefix

Slide 41

Slide 41 text

Nesting Layouts will mainly contain modules, although they can contain other layouts too

Slide 42

Slide 42 text

Module

Slide 43

Slide 43 text

The reusable blocks of a page

Slide 44

Slide 44 text

Should be built to work as stand-alone blocks

Slide 45

Slide 45 text

Providing flexibility and context independence

Slide 46

Slide 46 text

Building modules

Slide 47

Slide 47 text

Avoid element selectors

Slide 48

Slide 48 text

42 KB

Slide 49

Slide 49 text

.folder span { … }

Slide 50

Slide 50 text

/* This may be a better idea */ .folder > span { … }

Slide 51

Slide 51 text

42 KB 01/01/2021

Slide 52

Slide 52 text

.folder > span { … } .folder > span + span { … }

Slide 53

Slide 53 text

42 KB 01/01/2021 John Doe

Slide 54

Slide 54 text

.folder > span { … } .folder > span + span { … } .folder > span:last-child { … }

Slide 55

Slide 55 text

42 KB 01/01/2021 John Doe

Slide 56

Slide 56 text

.folder-size { … } .folder-date { … } .folder-owner { … }

Slide 57

Slide 57 text

Kind of a bold statement You can use element selectors, but only if they can and will be predictable; also avoid going more than one level deeper

Slide 58

Slide 58 text

Namespacing Descendant elements of a module use its name as a prefix

Slide 59

Slide 59 text

Subclass modules

Slide 60

Slide 60 text

Modifies certain styles of a module

Slide 61

Slide 61 text

.nav { … } .nav > li { … }

Slide 62

Slide 62 text

.nav { … } .nav > li { … } #sidebar .nav { … } #sidebar .nav > li { … }

Slide 63

Slide 63 text

.nav { … } .nav > li { … } .nav-stacked { … } .nav-stacked > li { … }

Slide 64

Slide 64 text

Slide 65

Slide 65 text

Namespacing Subclass modules use as a prefix the name of the base module

Slide 66

Slide 66 text

Nesting Modules will be inside layouts, although they can be inside other modules, too

Slide 67

Slide 67 text

State

Slide 68

Slide 68 text

Describes modules or layouts in a particular state

Slide 69

Slide 69 text

Subclass modules are applied at render-time and will not change on run-time

Slide 70

Slide 70 text

States may be applied at render or run-time and are likely to be modified at run-time

Slide 71

Slide 71 text

States are most likely triggered by user’s actions

Slide 72

Slide 72 text

Slide 73

Slide 73 text

.is-hidden { display: none !important; }

Slide 74

Slide 74 text

Slide 75

Slide 75 text

.tab { … } .is-tab-active { … }

Slide 76

Slide 76 text

Namespacing Global states are prefixed with `is-`, while scoped states are prefixed with `is-` followed by the module or layout name

Slide 77

Slide 77 text

Theme

Slide 78

Slide 78 text

The different variations of look and feel

Slide 79

Slide 79 text

Can affect rules of any category

Slide 80

Slide 80 text

/* On buttons.css */ .button { border: 1px solid; }

Slide 81

Slide 81 text

/* On theme-navy.css */ .button { border-color: #001f3f; }

Slide 82

Slide 82 text

Depth of applicability

Slide 83

Slide 83 text

The number of generations that are affected by a given rule

Slide 84

Slide 84 text

section#contact ul.details li { … }

Slide 85

Slide 85 text

#contact .details li { … }

Slide 86

Slide 86 text

/* Depth of 3 */ section#contact ul.details li { … } /* Depth of 3 */ #contact .details li { … }

Slide 87

Slide 87 text

Heads up! Don’t confuse depth of applicability with specificity

Slide 88

Slide 88 text

Keeping the depth as low as possible

Slide 89

Slide 89 text

Increases the flexibility and makes the code easier to reuse

Slide 90

Slide 90 text

Avoids relying heavily on a specific HTML structure

Slide 91

Slide 91 text

Lowers the specificity

Slide 92

Slide 92 text

#footer form { padding: .625em; } #footer form > input { display: inline-block; }

Slide 93

Slide 93 text

#footer form, #sidebar form { padding: .625em; } #footer form > input, #sidebar form > input { display: inline-block; }

Slide 94

Slide 94 text

.search { padding: .625em; } .search > input { display: inline-block; }

Slide 95

Slide 95 text

.search { padding: .625em; } .search-input { display: inline-block; }

Slide 96

Slide 96 text

.panel > div, .panel > ul, .panel > ol { padding: .75em; }

Slide 97

Slide 97 text

.panel-body { padding: .75em; }

Slide 98

Slide 98 text

That’s not just it!

Slide 99

Slide 99 text

There’s a lot more about SMACSS. Go to smacss.com and find out!

Slide 100

Slide 100 text

Questions?

Slide 101

Slide 101 text

Thanks!