we need to talk about css

38
t We need to talk about CSS

Upload: sean-durham

Post on 18-Dec-2014

1.234 views

Category:

Technology


3 download

DESCRIPTION

As developers, we put a lot of effort into keeping our code clean, clear, and flexible. So why do we let our CSS become such a mess? It's time to grow up as web developers and take responsibility for writing high-quality CSS and keeping it that way. Here's how I did it on a recent project.

TRANSCRIPT

Page 1: We Need to Talk About CSS

t

We needto talk about CSS

Page 2: We Need to Talk About CSS

We do our best to keep it together

Page 3: We Need to Talk About CSS

But try as we might

Page 4: We Need to Talk About CSS

chaos takes us in the end

Page 5: We Need to Talk About CSS

Day by day our CSS grows

Page 6: We Need to Talk About CSS

It grows disorganized

Our files are long and contain unrelated styles

It isn't clear how and when to break CSS up into separate files

Page 7: We Need to Talk About CSS

It growsfragile

Making changes feels unsafe

Page 8: We Need to Talk About CSS
Page 9: We Need to Talk About CSS

WE

Page 10: We Need to Talk About CSS

WENEED

Page 11: We Need to Talk About CSS

WENEEDHELP

Page 12: We Need to Talk About CSS
Page 13: We Need to Talk About CSS

Is the answer out there?

Page 14: We Need to Talk About CSS

OOCSS SMACSS BEM MCSS

Page 15: We Need to Talk About CSS
Page 16: We Need to Talk About CSS

We need guiding principals for organizing our CSS

What do we call it? Where do we put it?

Page 17: We Need to Talk About CSS

We need robust stylesheets we can change gracefully

How can I style this page without breaking other pages?

Page 18: We Need to Talk About CSS

Any worthwhile methodology would help with our problems

We need a methodology but which one?

Page 19: We Need to Talk About CSS

Let’s give it a shot

Page 20: We Need to Talk About CSS

SMACSS Scalable, Modular

Architecture for CSS

Page 21: We Need to Talk About CSS

1. Consider the intent of a style

Page 22: We Need to Talk About CSS

Base Layout Module

Module subclassModule component

State Theme

Page 23: We Need to Talk About CSS

Express your intent by following a naming scheme

2.

Page 24: We Need to Talk About CSS

BaseSet the default style for an element type

a { color: red;}a:hover { color: purple; text-decoration: none;}

This is the only place you should see tag names in your stylesheets.

Page 25: We Need to Talk About CSS

LayoutDefine a component that structures a page

.l-sidebar { float: left; width: 320px;}

Prefix layout components with ’l-’.

Page 26: We Need to Talk About CSS

ModuleDefine a type of thing

.activity { border-bottom: 1px solid #eee;}

Most CSS we write will define modules

Page 27: We Need to Talk About CSS

Module subclassDefine a variation of a module

.activity--metric-share { background-color: white;}

Prefix subclassed modules with ‘module-name--‘.

Page 28: We Need to Talk About CSS

Module componentDefine a component that exists only within a specific module.

.activity__icon { float: right; color: #d5d5d5;}

Prefix a component with ‘module-name__’.

Page 29: We Need to Talk About CSS

StateDefine a state of a layout or module

.is-collapsed { height: 0; margin-bottom: 0;} .activity-is-read { background-color:#eee;}

Prefix global states with ‘is-‘.Prefix module-specific states with ‘module-name-is-‘.

Page 30: We Need to Talk About CSS

Module subclasses and states are both variations on modules. !

How do we tell them apart? !

A subclass is applied at markup time. A state is applied and removed at runtime.

*

Page 31: We Need to Talk About CSS

ThemeDefine the appearance of a module

// in _thing.less .thing { display: block;}// in fun_theme.less .thing { border: 4px dashed magenta;}

We won’t use this intent

Page 32: We Need to Talk About CSS

ThemeDefine the appearance of a moduleWe won’t use this intent

Define modules without decorative properties in a module file.

Decorate all modules in a single theme file.

Include a single theme file on a page.

Page 33: We Need to Talk About CSS

Where do we put this stuff?

Base /_base.less !

Layout /_layout.less

Global states /_states.less

Modules /modules/_module_name.less

Subclassed modules, components, and module-specific states go with their parent module.

Page 34: We Need to Talk About CSS

I propose a couple more patterns

Page 35: We Need to Talk About CSS

JS selectors

<div class=“js-toggle-agreers"></div> !$('.js-toggle-agreers') .click(this.toggleAgreers);

When you need to select an element in a view give that element a classname beginning with ‘.js’.Apply no style to that class.

We want to know which classes are safe to change, and which will cause behavior to break

Page 36: We Need to Talk About CSS

Region selectors

<div class=“js-comments-region"></div> !layout: { comments: ‘.js-comments-region'}

When you need to select a class to become a view region append that class with ‘-region’.Apply no style to that class.

Page 37: We Need to Talk About CSS

Property ordering.module { // Box properties display: block; width: 100%; margin: 0 0 18px 0; // Background & border background-color: perrywinkle; border: 1px solid: lavendar; // Typography color: #666; line-height: 20px; text-decoration: none; // Other -webkit-transition: all .4s ease-in-out;}

Group related properties

Page 38: We Need to Talk About CSS

That was cathartic