98
Introduction T o OOCSS Julie Cameron @jewlofthelotus Slides http://bit.ly/oocss_wks Download Exercises http://bit.ly/oocss_ex Install Sublime Text http://sublimetext.com/2 Workshop Prep

Intro to OOCSS Workshop

Embed Size (px)

DESCRIPTION

Let’s face it… while CSS is as basic a language as you can get, it can be a challenge to master - especially when it comes to implementing large scalable projects. Without some sort of framework, it’s easy to end up in specificity spaghetti with severely duplicated code, browser performance issues, and generally unmaintainable, unsemantic, and unscalable CSS code. Adopting a design pattern like OOCSS will help you eliminate these nightmares and make crafting your CSS a joy once again. This workshop will examine the importance of a modular CSS architecture, profile the core principles of OOCSS, allow you to try your hand at module implementation, and touch on a few of the pros and “cons” of the system.

Citation preview

Page 1: Intro to OOCSS Workshop

Introduction To

OOCSSJulie Cameron @jewlofthelotus

Slides http://bit.ly/oocss_wks Download Exercises http://bit.ly/oocss_ex Install Sublime Text http://sublimetext.com/2

Workshop Prep

Page 2: Intro to OOCSS Workshop

About Me

@jewlofthelotus

Page 3: Intro to OOCSS Workshop

About Me

@jewlofthelotus

DERP.

Page 4: Intro to OOCSS Workshop

About You

What is your name?

What is your 140 character bio?

What is your favorite CSS prop/value?

Page 5: Intro to OOCSS Workshop

Object Oriented Cascading Stylesheets

OOCSS

Page 6: Intro to OOCSS Workshop

What is OOCSS?

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

Page 7: Intro to OOCSS Workshop

Why Do We Need It?

Page 8: Intro to OOCSS Workshop

Why Do We Need It?

CSS is complicated.

Page 9: Intro to OOCSS Workshop

What are we doing wrong?

Page 10: Intro to OOCSS Workshop

What are we doing wrong?

• A top down approach to writing styles often leads to repetition and bloat.

Page 11: Intro to OOCSS Workshop

What are we doing wrong?

• A top down approach to writing styles often leads to repetition and bloat.

• Selector misuse often leads to specificity trouble.

Page 12: Intro to OOCSS Workshop

What are we doing wrong?

• A top down approach to writing styles often leads to repetition and bloat.

• Selector misuse often leads to specificity trouble.

• Bloat + bad selectors = bad performance.

Page 13: Intro to OOCSS Workshop

What are we doing wrong?

• A top down approach to writing styles often leads to repetition and bloat.

• Selector misuse often leads to specificity trouble.

• Bloat + bad selectors = bad performance.

• !important is the devil.

Page 14: Intro to OOCSS Workshop

NONE OF THIS IS

SCALABLE OR

MAINTAINABLE.

Page 15: Intro to OOCSS Workshop

Why OOCSS?

OOCSS was created by Nicole Sullivan while working for Facebook.

She created a reusable module called the “media object” which she proved could save hundreds of lines of code.

Page 16: Intro to OOCSS Workshop

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

Page 17: Intro to OOCSS Workshop

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.

Page 18: Intro to OOCSS Workshop

The Media ObjectMedia Block Media to the left and some variable length content to the right.

<div class="media"> <img class="media__img" src="img.png" /> <div class="media__body"> <div class="media__title">Some title here</div> <p>Some body content here</p> </div> </div>

Page 19: Intro to OOCSS Workshop

The Foundation of Object Oriented CSS

OBJECTS

Page 20: Intro to OOCSS Workshop

CSS Objects

The abstraction of repetition into reusable “objects”.

Page 21: Intro to OOCSS Workshop

HTML + CSS + JS

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

Page 22: Intro to OOCSS Workshop

HTML + CSS + JS1. HTML, which can be one or more nodes of the DOM

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

Page 23: Intro to OOCSS Workshop

HTML + CSS + JS1. HTML, which can be one or more nodes of the DOM

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

2. CSS declarations about the style of those nodes, all of which begin with the class name of the wrapper node

Page 24: Intro to OOCSS Workshop

HTML + CSS + JS1. HTML, which can be one or more nodes of the DOM

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

2. CSS declarations about the style of those nodes, all of which begin with the class name of the wrapper node

3. Components like background images and sprites required for display

Page 25: Intro to OOCSS Workshop

HTML + CSS + JS1. HTML, which can be one or more nodes of the DOM

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

2. CSS declarations about the style of those nodes, all of which begin with the class name of the wrapper node

3. Components like background images and sprites required for display

4. JavaScript behaviors, listeners, or methods associated with the object.

Page 26: Intro to OOCSS Workshop

Modules with Variations

.btn

Page 27: Intro to OOCSS Workshop

Modules with Variations

.btn

.btn—primary .btn-warningCSS

Page 28: Intro to OOCSS Workshop

Modules with Variations

.btn

.btn—primary .btn-warningCSS

.btn—disabled .btn-active

CSS JS

Page 29: Intro to OOCSS Workshop

Module Components

Page 30: Intro to OOCSS Workshop

Module Components

Objects or Modules or Components .media

Page 31: Intro to OOCSS Workshop

Module Components

Objects or Modules or Components .media

Parent - Child .media__img .media__body

Relationships

Page 32: Intro to OOCSS Workshop

Module Components

Objects or Modules or Components .media

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

CSS Design Variations

Parent - Child .media__img .media__body

Relationships

Page 33: Intro to OOCSS Workshop

Module Components

Objects or Modules or Components .media

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

CSS Design Variations

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

CSS / JS Design Adjustments

Parent - Child .media__img .media__body

Relationships

Page 34: Intro to OOCSS Workshop

Key Considerations

Page 35: Intro to OOCSS Workshop

CLASSES CLASSES CLASSES

1ST CONSIDERATION

Page 36: Intro to OOCSS Workshop

CLASSES CLASSES CLASSES

Take that, Specificity!

1ST CONSIDERATION

Page 37: Intro to OOCSS Workshop

NAMING CONVENTIONS

2ND CONSIDERATION

Page 38: Intro to OOCSS Workshop

NAMING FORMAT

.camelCase

.dashed-dash

.under_score

Choose your style and be consistent.

.bem—-style

.bem__style

Page 39: Intro to OOCSS Workshop

NAMING PATTERNS

.object-child

.object__child

.object_child

.objectChild

.myObject-childObj

How do you identify children? Modifiers?

.object-modifier

.object—-modifier

.object_modifier

.objectModifier

.myObj-modClass

Page 40: Intro to OOCSS Workshop

BEM NAMING

.object

.object__child

.object—-modifier

.object__child—-modifier

Page 41: Intro to OOCSS Workshop

SEMANTIC VS

PRESENTATIONAL

3RD CONSIDERATION

Page 42: Intro to OOCSS Workshop

DRY HTML VS

DRY CSS

Semantic

Presentational

Page 43: Intro to OOCSS Workshop

SEMANTIC

.bookmark

.article

.callToAction

Describes what the object is.

.error

.article__title

.article—-flagged

Page 44: Intro to OOCSS Workshop

PRESENTATIONAL

.grid__col—-9

.btn—-small

.border—-all

Describes how the object looks.

.copy—-red

.float—-left

.border—thick

Page 45: Intro to OOCSS Workshop

MY SYSTEM = A HYBRID SYSTEM

Module Classes + Utility Classes

<div class=“media__body copy—-small content—-secondary”>

Semantic Presentational

Page 46: Intro to OOCSS Workshop

Questions?

Page 47: Intro to OOCSS Workshop

2 Principles of OOCSS

Page 48: Intro to OOCSS Workshop

Separation of

STRUCTURE from

SKIN

1ST PRINCIPLE

Page 49: Intro to OOCSS Workshop

Structure Vs. Skin

Define repeating visual patterns as reusable skins.

visible == different

!!!!

Define repeating “invisible” patterns as reusable structures.

invisible == same

Page 50: Intro to OOCSS Workshop

StructureRepeating “invisible” patterns.

invisible == same

Page 51: Intro to OOCSS Workshop

StructureRepeating “invisible” patterns.

invisible == same

.btn { border-radius: 4px; display: inline-block; font-size: 1.25em; padding: 1em; text-align: center; vertical-align: middle; }

Page 52: Intro to OOCSS Workshop

SkinRepeating visible patterns.

visible == different

.btn—-primary { background: blue; }

.btn—-success { background: green; }

.btn—-info { background: lightblue; }

.btn—-warning { background: orange; }

.btn—-danger { background: red; }

Page 53: Intro to OOCSS Workshop

Structure + Skin

<button class=“btn btn—-primary”></button>

<a class=“btn btn—-success” href=“#”></a>

<input class=“btn btn—-info” type=“submit” />

<span class=“btn btn—-warning”></span>

<label class=“btn btn—-danger”></label>

Page 54: Intro to OOCSS Workshop

1. Download and unzip package !

2. Open entire folder in Sublime Text

3. Open index.html in browser

Let’s Do This.

http://bit.ly/oocss_ex

Page 55: Intro to OOCSS Workshop

Structure, Skins, States, & Modifiers

Button Exercise

Page 56: Intro to OOCSS Workshop

Button Structure

Exercise 1

Hints: use class .btn, no hover or focus styles

Page 57: Intro to OOCSS Workshop

Button Skins

Exercise 2

Hints: use classes .btn—-primary, .btn—-secondary, .btn—-tertiary

only three CSS properties need to be modified

Page 58: Intro to OOCSS Workshop

Button States

Exercise 3

Hint: style each variation for :hover only three CSS properties need to be adjusted for each variation

Page 59: Intro to OOCSS Workshop

Button Modifiers

Exercise 4Hints: use classes .btn—-medium, .btn—-small, .btn—-block

BONUS: .btn—-inverse

Page 60: Intro to OOCSS Workshop

Questions?

Page 61: Intro to OOCSS Workshop

Break Time

Page 62: Intro to OOCSS Workshop

Separation of

CONTAINER from

CONTENT

2ND PRINCIPLE

Page 63: Intro to OOCSS Workshop

Container Vs. Content

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

Page 64: Intro to OOCSS Workshop

Container Vs. Content

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

AVOID Location-Dependent Styles (which often lead to duplication)

Page 65: Intro to OOCSS Workshop

Location Dependent Styles

.categoryList h2 { // stylez }

Page 66: Intro to OOCSS Workshop

Location Dependent Styles

.categoryList h2 { // stylez }.footer h2 { // copy stylez }

Page 67: Intro to OOCSS Workshop

Location Dependent Styles

.categoryList h2 { // stylez }.footer h2 { // copy stylez }.sidebar h3 { // copy stylez again }

Page 68: Intro to OOCSS Workshop

Location Dependent Styles

.categoryList h2 { // stylez }.footer h2 { // copy stylez }.sidebar h3 { // copy stylez again }BAD

Page 69: Intro to OOCSS Workshop

.categoryList h2,

.footer h2,

.sidebar h3 { // stylez }

Better?

Page 70: Intro to OOCSS Workshop

.categoryList h2,

.footer h2,

.sidebar h3 { // stylez }

Better?

SORTA

Page 71: Intro to OOCSS Workshop

Separation Of Container & Content

.categoryList h2 { // stylez }.footer h2 { // copy stylez }.sidebar h3 { // copy stylez again }

What is the container?

Page 72: Intro to OOCSS Workshop

Separation Of Container & Content

.categoryList h2 { // stylez }.footer h2 { // copy stylez }.sidebar h3 { // copy stylez again }

What is the container?

Page 73: Intro to OOCSS Workshop

Separation Of Container & Content

.categoryList h2 { // stylez }.footer h2 { // copy stylez }.sidebar h3 { // copy stylez again }

What is the content?

Page 74: Intro to OOCSS Workshop

Separation Of Container & Content

.categoryList { //… }

.footer { //… }

.sidebar { //… }

How do we separate the content styles?

.sectionHeader { // YESSS! We can reuse this header // style anywhere!! }

Page 75: Intro to OOCSS Workshop

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.

http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/

Page 76: Intro to OOCSS Workshop

OOCSS Objects

.sectionHeader { // IS AN OBJECT!! }

Page 77: Intro to OOCSS Workshop

How Do We Do This?

1. Base Styles - html, body, p, a, h1-h6, etc.

In practice.

Page 78: Intro to OOCSS Workshop

How Do We Do This?

1. Base Styles - html, body, p, a, h1-h6, etc.

2. Building Block Objects - lists, nav, buttons, media

In practice.

Page 79: Intro to OOCSS Workshop

How Do We Do This?

1. Base Styles - html, body, p, a, h1-h6, etc.

2. Building Block Objects - lists, nav, buttons, media

3. Composite Objects - headers, footers, masonry cards, sidebars

In practice.

Page 80: Intro to OOCSS Workshop

Put It All Together

Page 81: Intro to OOCSS Workshop

Put It All Together!%nav %ul.list--horzRight.list--blockAll %li %a.media--inline %span.media__img.icon—-bookmark %span.media__body Bookmark %li.dropdown %a.media--inline %span.media__img.icon—-share %span.media__body Share %ul.dropdown__content—-right.list—-boxed %li %a.media--block %span.media__img.icon—-facebook %span.media__body Facebook %li %a.media--block %span.media__img.icon—-twitter %span.media__body Twitter %li %a.media--block %span.media__img.icon—-email %span.media__body Email

Page 82: Intro to OOCSS Workshop

Put It All Together!%nav %ul.list--horzRight.list--blockAll %li %a.media--inline %span.media__img.icon—-bookmark %span.media__body Bookmark %li.dropdown %a.media--inline %span.media__img.icon—-share %span.media__body Share %ul.dropdown__content—-right.list—-boxed %li %a.media--block %span.media__img.icon—-facebook %span.media__body Facebook %li %a.media--block %span.media__img.icon—-twitter %span.media__body Twitter %li %a.media--block %span.media__img.icon—-email %span.media__body Email

Objects In Use:

• List • Media • Dropdown • Icon

Page 83: Intro to OOCSS Workshop

Open index.html in browser

Let’s Do This.

Page 84: Intro to OOCSS Workshop

Containers & Contents

Sidebar ~> Object Exercise

Page 85: Intro to OOCSS Workshop

Separate - Setup the same styles in the footer

Exercise 1

Hints: Assume heading and paragraph styles are site defaults

Page 86: Intro to OOCSS Workshop

Modify - Create styles for the Right Sidebar

Exercise 2

Hints: abstract float styles, add an alignment modifier We only need 3 new classes!

Page 87: Intro to OOCSS Workshop

Questions?

Page 88: Intro to OOCSS Workshop

Uber Classitis OOCSS http://appendto.com/2014/04/oocss/

Bonus Demo

Page 89: Intro to OOCSS Workshop

CLASSITIS

Page 90: Intro to OOCSS Workshop

Classitis

DRY HTML vs. DRY CSS

DRY CSS will be more maintainable.

Page 91: Intro to OOCSS Workshop

The Semantics Debate

• Should classes always be semantic rather than presentational?

• Is it better to have one semantic class or multiple mixed classes?

• To whom are classes semantic to?

Page 92: Intro to OOCSS Workshop

The Semantics Debate

• Classes have NO meaning to the browser.

• Classes ARE semantic to developers, though.

• Presentational classes are reusable and don’t couple to content.

• The content describes itself, classes don’t need to.

Page 93: Intro to OOCSS Workshop

The choice is yours. (and your team’s)

Page 94: Intro to OOCSS Workshop

Closing Don’ts

• Avoid styling ID selectors

• Avoid styling descendent selectors, ie. .sidebar ul li

• Avoid attaching classes to element selectors, ie. h1.title

• Avoid !important

Page 95: Intro to OOCSS Workshop

Closing Do’s

• Always look for UI patterns

• Always be abstracting

• Establish and be consistent with your naming system

• Use a preprocessor like Sass for even more modularity and organization

• Look into other modular systems like SMACSS or Atomic

Page 96: Intro to OOCSS Workshop

Resources

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

• http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/

• http://appendto.com/2014/04/oocss/

• http://nicolasgallagher.com/about-html-semantics-front-end-architecture/

Page 97: Intro to OOCSS Workshop

Questions?

Slides: http://bit.ly/oocss_wks !@jewlofthelotus

Page 98: Intro to OOCSS Workshop

Questions?

Slides: http://bit.ly/oocss_wks !@jewlofthelotus