decoupling the front-end with modular css

127
Decoupling the Front-end with Modular CSS Julie Cameron @jewlofthelotus #OSCON 2015 bit.ly/decoupling-css

Upload: julie-cameron

Post on 02-Jul-2015

3.079 views

Category:

Internet


5 download

DESCRIPTION

CSS is hard. It’s a simple language, but getting it right and avoiding specificity hell can be a challenge if you don’t have the right framework to back you up. Especially in large scaling projects, you might start adding ID selectors here and !important properties there and the next thing you know you’ve backed yourself into a corner where even the smallest of UI changes will take hours to work out. Ew. Or how about this? Ever jump into a project and find that even the slightest markup change results in broken JavaScript AND sometimes even broken backend feature tests?! WTF. Ew. This talk will look at how taking a modular, object-oriented approach to CSS can turn frontend woes into frontend wins. We’ll examine modern CSS approaches like OOCSS, SMACSS, and BEM and demonstrate how they will help to not only decouple your CSS styles and reduce specificity conflicts, but how they will also help to decouple your CSS and HTML from your JavaScript and feature specs. Video from SEM.js November 2014: http://youtu.be/HoQ-QEusyS0

TRANSCRIPT

Page 1: Decoupling the Front-end with Modular CSS

Decoupling the Front-end with

Modular CSS

Julie Cameron @jewlofthelotus

#OSCON 2015 bit.ly/decoupling-css

Page 2: Decoupling the Front-end with Modular CSS

DERP.

@jewlofthelotus | #OSCON

Page 3: Decoupling the Front-end with Modular CSS

@jewlofthelotus | #OSCON

Page 4: Decoupling the Front-end with Modular CSS

articulate.com@jewlofthelotus | #OSCON

Page 5: Decoupling the Front-end with Modular CSS

girldevelopit.com@jewlofthelotus | #OSCON

Page 6: Decoupling the Front-end with Modular CSS

OOCSS

@jewlofthelotus | #OSCON

Page 7: Decoupling the Front-end with Modular CSS

Got CSS?

@jewlofthelotus | #OSCON

Page 9: Decoupling the Front-end with Modular CSS

#cascade {

div.is {

span.delicate {

position: absolute !important;

}

}

}

@jewlofthelotus | #OSCON

Page 10: Decoupling the Front-end with Modular CSS

@jewlofthelotus | #OSCON

Page 11: Decoupling the Front-end with Modular CSS

“It’s almost a challenge to find a development team that’s working on a codebase that’s more than a couple of years old where the CSS isn’t the most frightening and hated part of that system.”

Andy Hume - CSS for Grownups

@jewlofthelotus | #OSCON

Page 12: Decoupling the Front-end with Modular CSS

TOP DOWN CSS =

REPETITION & BLOAT

@jewlofthelotus | #OSCON

Page 13: Decoupling the Front-end with Modular CSS

SELECTOR MISUSE =

SPECIFICITY HELL

@jewlofthelotus | #OSCON

Page 14: Decoupling the Front-end with Modular CSS

BLOAT +

BAD SELECTORS =

BAD PERFORMANCE@jewlofthelotus | #OSCON

Page 15: Decoupling the Front-end with Modular CSS

!IMPORTANT

= THE DEVIL

@jewlofthelotus | #OSCON

Page 16: Decoupling the Front-end with Modular CSS

NONE OF THIS IS

SCALABLE OR MAINTAINABLE.

A hot mess is what it is…

@jewlofthelotus | #OSCON

Page 17: Decoupling the Front-end with Modular CSS

TOP DOWN OVERLY SPECIFIC

CSSHTML@jewlofthelotus | #OSCON

Page 18: Decoupling the Front-end with Modular CSS

WE’VE BEEN COUPLING OUR

HTML & CSS@jewlofthelotus | #OSCON

Page 19: Decoupling the Front-end with Modular CSS

WE’VE BEEN COUPLING OUR

HTML & CSSAnd sometimes our CSS & JavaScript.

@jewlofthelotus | #OSCON

Page 20: Decoupling the Front-end with Modular CSS

WE’VE BEEN COUPLING OUR

HTML & CSSAnd sometimes our CSS & JavaScript. …and Feature Specs, too.

@jewlofthelotus | #OSCON

Page 21: Decoupling the Front-end with Modular CSS

Coupled CSS Selectors

@jewlofthelotus | #OSCON

Page 22: Decoupling the Front-end with Modular CSS

STOP USING CSS SELECTORS FOR NON-CSS

Data Attributes. FTW.

@jewlofthelotus | #OSCON

Page 23: Decoupling the Front-end with Modular CSS

Functionality Selectors

@jewlofthelotus | #OSCON

Page 24: Decoupling the Front-end with Modular CSS

START USING MODULAR CSS

@jewlofthelotus | #OSCON

Page 25: Decoupling the Front-end with Modular CSS

WHY?

@jewlofthelotus | #OSCON

Page 32: Decoupling the Front-end with Modular CSS

Modular Solutions• OOCSS

• BEM

• SMACSS

• Atomic Design

• CSS for Grownups

• CCSS

• DRY CSS

• and many more…

@jewlofthelotus | #OSCON

Page 33: Decoupling the Front-end with Modular CSS

Object Oriented Cascading Stylesheets

OOCSS

https://github.com/stubbornella/oocss/wiki@jewlofthelotus | #OSCON

Page 34: Decoupling the Front-end with Modular CSS

A scalable, maintainable, semantic, and predictable approach to writing CSS.

@jewlofthelotus | #OSCON

Page 35: Decoupling the Front-end with Modular CSS

OOCSS was created by Nicole Sullivan while working for Facebook.

She created a reusable CSS module called the “media object” to save hundreds of lines of code.

stubbornella.org

@jewlofthelotus | #OSCON

Page 36: Decoupling the Front-end with Modular CSS

The Media Object

“a content block containing a fixed-size media element (e.g. image or video) along with other variable-size content (e.g. text)” https://github.com/stubbornella/oocss/wiki

AKA a Facebook status.

@jewlofthelotus | #OSCON

Page 37: Decoupling the Front-end with Modular CSS

A Media Object Media to the left and some variable length content to the right.

img.png

@jewlofthelotus | #OSCON

Page 38: Decoupling the Front-end with Modular CSS

The Foundation of Modular CSS

OBJECTS

@jewlofthelotus | #OSCON

Page 39: Decoupling the Front-end with Modular CSS

Objects are the abstraction of repetition.

@jewlofthelotus | #OSCON

Page 40: Decoupling the Front-end with Modular CSS

Object Makeup

1. HTML - one or more nodes of the DOM

https://github.com/stubbornella/oocss/wiki/FAQ

@jewlofthelotus | #OSCON

Page 41: Decoupling the Front-end with Modular CSS

Object Makeup

1. HTML - one or more nodes of the DOM

https://github.com/stubbornella/oocss/wiki/FAQ

2. CSS that styles those nodes

@jewlofthelotus | #OSCON

Page 42: Decoupling the Front-end with Modular CSS

Object Makeup

1. HTML - one or more nodes of the DOM

https://github.com/stubbornella/oocss/wiki/FAQ

2. CSS that styles those nodes

3. JavaScript functionality tied to those nodes

@jewlofthelotus | #OSCON

Page 43: Decoupling the Front-end with Modular CSS

Modules with Variations

.btn

@jewlofthelotus | #OSCON

Page 44: Decoupling the Front-end with Modular CSS

Modules with Variations

.btn

.btn—primary .btn-smallCSS

.btn:hover

@jewlofthelotus | #OSCON

Page 45: Decoupling the Front-end with Modular CSS

Modules with Variations

.btn

.btn.is—disabled .btn.is-active

CSS JS

.btn—primary .btn-smallCSS

.btn:hover

@jewlofthelotus | #OSCON

Page 46: Decoupling the Front-end with Modular CSS

Module Components

Object aka module / component / block .btn, .media

@jewlofthelotus | #OSCON

Page 47: Decoupling the Front-end with Modular CSS

Module Components

Object aka module / component / block .btn, .media

Child Objects .media__img, .media__body

Relationships

@jewlofthelotus | #OSCON

Page 48: Decoupling the Front-end with Modular CSS

Module Components

Object aka module / component / block .btn, .media

Modifiers .media—-inline, .media__img—-right

CSS Design Variations

Child Objects .media__img, .media__body

Relationships

@jewlofthelotus | #OSCON

Page 49: Decoupling the Front-end with Modular CSS

Module Components

Object aka module / component / block .btn, .media

Modifiers .media—-inline, .media__img—-right

CSS Design Variations

States .media—-collapsed, .media.is-active

CSS / JS Design Adjustments

Child Objects .media__img, .media__body

Relationships

@jewlofthelotus | #OSCON

Page 50: Decoupling the Front-end with Modular CSS

BEST PRACTICES

@jewlofthelotus | #OSCON

Page 51: Decoupling the Front-end with Modular CSS

SINGLE RESPONSIBILITY

Do one thing well and one thing only.

@jewlofthelotus | #OSCON

Page 52: Decoupling the Front-end with Modular CSS

.CLASSES

.CLASSES

.CLASSESTake that, specificity!

@jewlofthelotus | #OSCON

Page 53: Decoupling the Front-end with Modular CSS

NAMING CONVENTIONS

Just breathe. Everything is going to be okay.

@jewlofthelotus | #OSCON

Page 54: Decoupling the Front-end with Modular CSS

PICK A SYSTEM

BE CONSISTENT

@jewlofthelotus | #OSCON

Page 55: Decoupling the Front-end with Modular CSS

Naming Formats

.modulename

.moduleName

.module-name

.module_name

.bem——style

.bem_ _style

@jewlofthelotus | #OSCON

Page 56: Decoupling the Front-end with Modular CSS

Naming PatternsHow do you identify children? States? Modifiers?

.objectChild

.object-child

.object_child

.object_ _child

.myObject—childObj

.object.is—active

.objectModifier

.object-modifier

.object_modifier

.object——modifier

.myObj-modClass

.object.active

@jewlofthelotus | #OSCON

Page 57: Decoupling the Front-end with Modular CSS

BEM.block_ _element——modifier

http://bem.info@jewlofthelotus | #OSCON

Page 58: Decoupling the Front-end with Modular CSS

.block .media

@jewlofthelotus | #OSCON

Page 59: Decoupling the Front-end with Modular CSS

.block .media

.block_ _element .media_ _img

@jewlofthelotus | #OSCON

Page 60: Decoupling the Front-end with Modular CSS

.block .media

.block_ _element .media_ _img

.block——modifier .media——inline

@jewlofthelotus | #OSCON

Page 61: Decoupling the Front-end with Modular CSS

.block .media

.block_ _element .media_ _img

.block——modifier .media——inline

.block_ _element——modifier .media_ _img——right

@jewlofthelotus | #OSCON

Page 62: Decoupling the Front-end with Modular CSS

SEMANTIC VS

PRESENTATIONAL

NAMINGUh oh…

@jewlofthelotus | #OSCON

Page 63: Decoupling the Front-end with Modular CSS

Presentational Naming

Describes how the object looks.

.grid_ _col——9

.btn——small

.border——all

.color——red

.float——left

.border——thick

@jewlofthelotus | #OSCON

Page 64: Decoupling the Front-end with Modular CSS

Semantic NamingDescribes what the object is.

.bookmark

.article

.callToAction

.error

.article_ _title

.article——flagged

@jewlofthelotus | #OSCON

Page 65: Decoupling the Front-end with Modular CSS

Fewer Semantic Classes

DRY HTML VS

DRY CSSMany Presentational Classes

@jewlofthelotus | #OSCON

Page 66: Decoupling the Front-end with Modular CSS

NO ONE KNOWS

WHAT THEY’RE

DOING.@jewlofthelotus | #OSCON

Page 67: Decoupling the Front-end with Modular CSS

2 Principles of OOCSS

@jewlofthelotus | #OSCON

Page 68: Decoupling the Front-end with Modular CSS

Separation of

STRUCTURE from

SKIN

1ST PRINCIPLE

@jewlofthelotus | #OSCON

Page 69: Decoupling the Front-end with Modular CSS

Structure Vs. Skin

@jewlofthelotus | #OSCON

Page 70: Decoupling the Front-end with Modular CSS

Structure Vs. SkinDefine repeating “invisible” patterns

as reusable structures. What makes a button look like a button?

@jewlofthelotus | #OSCON

Page 71: Decoupling the Front-end with Modular CSS

Structure Vs. SkinDefine repeating “invisible” patterns

as reusable structures. What makes a button look like a button?

Define repeating visual patterns as reusable skins. What makes these buttons look different?

@jewlofthelotus | #OSCON

Page 72: Decoupling the Front-end with Modular CSS

StructureRepeating “invisible” patterns.

What makes a button look like a button?

@jewlofthelotus | #OSCON

Page 73: Decoupling the Front-end with Modular CSS

StructureRepeating “invisible” patterns.

What makes a button look like a button?

@jewlofthelotus | #OSCON

Page 74: Decoupling the Front-end with Modular CSS

SkinRepeating visible patterns.

What makes these buttons look different?

@jewlofthelotus | #OSCON

Page 75: Decoupling the Front-end with Modular CSS

SkinRepeating visible patterns.

What makes these buttons look different?

@jewlofthelotus | #OSCON

Page 76: Decoupling the Front-end with Modular CSS

Structure + Skin

@jewlofthelotus | #OSCON

Page 77: Decoupling the Front-end with Modular CSS

Structure + Skin.btn .btn—primary

.btn—small

.btn—block

.btn—inverse

@jewlofthelotus | #OSCON

Page 78: Decoupling the Front-end with Modular CSS

Separation of

CONTAINER from

CONTENT

2ND PRINCIPLE

@jewlofthelotus | #OSCON

Page 79: Decoupling the Front-end with Modular CSS

Container Vs. Content

Objects should look the same no matter where you put them.

@jewlofthelotus | #OSCON

Page 80: Decoupling the Front-end with Modular CSS

Location Dependent Styles

@jewlofthelotus | #OSCON

Page 81: Decoupling the Front-end with Modular CSS

Location Dependent Styles

@jewlofthelotus | #OSCON

Page 82: Decoupling the Front-end with Modular CSS

Location Dependent Styles

@jewlofthelotus | #OSCON

Page 83: Decoupling the Front-end with Modular CSS

Better?

@jewlofthelotus | #OSCON

Page 84: Decoupling the Front-end with Modular CSS

Separation Of Container & Content

What is the container?

@jewlofthelotus | #OSCON

Page 85: Decoupling the Front-end with Modular CSS

Separation Of Container & Content

What is the container?

@jewlofthelotus | #OSCON

Page 86: Decoupling the Front-end with Modular CSS

Separation Of Container & Content

What is the content?

@jewlofthelotus | #OSCON

Page 87: Decoupling the Front-end with Modular CSS

Separation Of Container & Content

Abstract reusable styles into a new object.

@jewlofthelotus | #OSCON

Page 88: Decoupling the Front-end with Modular CSS

With OOCSS, we’re encouraged to give more forethought to what is common among different elements, then separate those common features into modules, or objects, that can be reused anywhere.

Louis Lazaris — Smashing Magazine

@jewlofthelotus | #OSCON

Page 89: Decoupling the Front-end with Modular CSS

OOCSSStructure vs. Skin

Container vs. Content

@jewlofthelotus | #OSCON

Page 90: Decoupling the Front-end with Modular CSS

CLASSITIS

@jewlofthelotus | #OSCON

Page 91: Decoupling the Front-end with Modular CSS

HTML <3 CSS

@jewlofthelotus | #OSCON

Page 92: Decoupling the Front-end with Modular CSS

HTML <3 CSSCSS STYLE

@jewlofthelotus | #OSCON

Page 93: Decoupling the Front-end with Modular CSS

The Semantics Debate

• Do classes actually have “semantic” value?

@jewlofthelotus | #OSCON

Page 94: Decoupling the Front-end with Modular CSS

The Semantics Debate

• Do classes actually have “semantic” value?

• To whom are classes semantic to?

@jewlofthelotus | #OSCON

Page 95: Decoupling the Front-end with Modular CSS

The Semantics Debate

• Do classes actually have “semantic” value?

• To whom are classes semantic to?

• Should we stick with one way or the other?

@jewlofthelotus | #OSCON

Page 96: Decoupling the Front-end with Modular CSS

Presentational Arguments

• Classes have NO meaning to the browser.

@jewlofthelotus | #OSCON

Page 97: Decoupling the Front-end with Modular CSS

Presentational Arguments

• Classes have NO meaning to the browser.

• Classes ARE semantic to developers.

@jewlofthelotus | #OSCON

Page 98: Decoupling the Front-end with Modular CSS

Presentational Arguments

• Classes have NO meaning to the browser.

• Classes ARE semantic to developers.

• Content describes itself, classes don’t need to.

@jewlofthelotus | #OSCON

Page 99: Decoupling the Front-end with Modular CSS

Semantic

DRY HTML VS

DRY CSSPresentational

@jewlofthelotus | #OSCON

Page 100: Decoupling the Front-end with Modular CSS

SMACSS Scalable & Modular Architecture with CSS

http://smacss.com

Jonathan Snook

@jewlofthelotus | #OSCON

Page 102: Decoupling the Front-end with Modular CSS

• Base

• Layout

• Module

• State

• Theme

SMACSS Categories

@jewlofthelotus | #OSCON

Page 103: Decoupling the Front-end with Modular CSS

CSS Resets +

Default Styles

What things should generally look like.

SMACSS Base Objects

@jewlofthelotus | #OSCON

Page 104: Decoupling the Front-end with Modular CSS

Containers +

Grids

Hold the modules together.

SMACSS Layout Objects#IDs for single use “major” components

.classes for reusable “minor” components

@jewlofthelotus | #OSCON

Page 105: Decoupling the Front-end with Modular CSS

Reusable Parts +

Subclasses (Modifiers)

The good stuff.

SMACSS Modules

@jewlofthelotus | #OSCON

Page 106: Decoupling the Front-end with Modular CSS

Tacked on to layout & module objects

Indicate a JavaScript dependency.

SMACSS State Objects!important is allowed and even recommended!

@jewlofthelotus | #OSCON

Page 107: Decoupling the Front-end with Modular CSS

Tacked on to layout & module objects

Indicate a JavaScript dependency.

SMACSS State Objects!important is allowed and even recommended!

Module-specific State objects

Include module name & reside with it.pseudo & @media query states included.

@jewlofthelotus | #OSCON

Page 108: Decoupling the Front-end with Modular CSS

Themes can apply to all other

object types

In the case that your site has multiple holistic themes.

SMACSS Themes

@jewlofthelotus | #OSCON

Page 109: Decoupling the Front-end with Modular CSS

ATOMIC DESIGN

http://bradfrost.com/blog/post/atomic-web-design

Brad Frost

@jewlofthelotus | #OSCON

Page 111: Decoupling the Front-end with Modular CSS

Atomic Design

atoms molecules organisms templates pages

@jewlofthelotus | #OSCON

Page 112: Decoupling the Front-end with Modular CSS

Atomic Design

atoms molecules organisms templates pages

+@jewlofthelotus | #OSCON

Page 113: Decoupling the Front-end with Modular CSS

The basic building blocks of matter.

Single element. Global styles.

Atoms

@jewlofthelotus | #OSCON

Page 114: Decoupling the Front-end with Modular CSS

Groups of atoms bonded together.

Simple combinations built for reuse.

Molecules

@jewlofthelotus | #OSCON

Page 115: Decoupling the Front-end with Modular CSS

Groups of molecules joined together.

Complex, distinct sections of an interface.

Organisms

@jewlofthelotus | #OSCON

Page 116: Decoupling the Front-end with Modular CSS

http://patternlab.io

A collection of tools to help you create atomic design systems.

Pattern Lab

Page 117: Decoupling the Front-end with Modular CSS

WHAT NOW?

@jewlofthelotus | #OSCON

Page 118: Decoupling the Front-end with Modular CSS

ORGANIZE OPTIMIZE

REPEAT@jewlofthelotus | #OSCON

Page 119: Decoupling the Front-end with Modular CSS

ITERATING ON ABSTRACTION

@jewlofthelotus | #OSCON

Page 120: Decoupling the Front-end with Modular CSS

FINDING THE RIGHT

GRANULARITY

@jewlofthelotus | #OSCON

Page 121: Decoupling the Front-end with Modular CSS

ESTABLISH CONVENTIONS

@jewlofthelotus | #OSCON

Page 122: Decoupling the Front-end with Modular CSS

DOCUMENT EVERYTHING

@jewlofthelotus | #OSCON

Page 123: Decoupling the Front-end with Modular CSS

ARCHITECT A STYLEGUIDE

@jewlofthelotus | #OSCON

Page 124: Decoupling the Front-end with Modular CSS

UTILIZE A CSS PREPROCESSOR

@jewlofthelotus | #OSCON

Page 125: Decoupling the Front-end with Modular CSS

STOP USING CSS SELECTORS FOR NON-CSS

@jewlofthelotus | #OSCON

Page 126: Decoupling the Front-end with Modular CSS

http://github.com/davidtheclark/scalable-css-reading-list

http://bit.ly/dc-scrl

LEARN MORE

@jewlofthelotus | #OSCON

Page 127: Decoupling the Front-end with Modular CSS

Questions?

Decoupling the Front-end with

Modular CSS

Julie Cameron @jewlofthelotus

#OSCON 2015 bit.ly/decoupling-css