scalable css you and your back-end coders can love - @cssconf asia 2014

67
Scalable CSS (That both you and your ‘back-end’ coders can love.) about.me/XML #xmlilley

Upload: christian-lilley

Post on 02-Jul-2015

2.146 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

ScalableCSS

(That both you and your ‘back-end’ coders can love.) !

about.me/XML #xmlilley

Page 2: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

UI DEV HAS GOTTEN COOL !!!?!

Page 3: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 4: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 5: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 6: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 7: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

-my really smart colleague

“… an anti-language, full of dark magic…”

Page 8: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 9: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

WHYYYYYY?????And why do we care?

Page 10: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 11: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 12: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 13: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

I asked them. And they said it’s all about …

The whole ‘Cascading’ thing

The ‘Specificity’ thing (see also: ‘the Cascading thing’)

Selector Chaos (mostly re: Specificity & Cascading)

Layout (old-school Layouts, that is…)

Page 14: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Hmmmmm….

Page 15: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Option 1:

They must need better training about this stuff

Page 16: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 17: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Option 2:

Maybe they’ve got a point.

Page 18: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

“If all you’ve got is a hammer,

everything looks like a nail.”

Page 19: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

(See also:

“StockholmSyndrome”)

Page 20: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

First Principles

be DRY - ‘Don’t Repeat Yourself’

be Maintainable - write for updates & debugging

be Predictable - Don’t keep hacking what’s broken (unless you have to)

Don’t ‘Optimize’ Prematurely

Page 21: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Strategic Rules For CSS

Name All The Things™

Stop Hoarding Classes & Rationing Letters

Be a Lover, not a Fighter

Automate or Die

Page 22: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Challenge 1:

Use Layouts That Make Sense

Page 23: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

The Super-Secret Key to Eliminating 99% of Layout Angst

STOP

USING

FLOAT:

ALREADY !!!!

Page 24: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Page 25: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Instead of this:

Or this:

Page 26: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Or worse, this (which is unfixable in code):

#3 #2 #1

You get this:

Page 27: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Stop Fighting With Floats

!

Page 28: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Love Display: Inline-Block

Supported Since IE8 !!!

Does what you expect

vertical-align: a feature, not a bug :-)

text-align: gets you left, right, or center

Page 29: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Love Display: Inline-Block

(The whitespace thing *is* a bug. Easy, transparent fix: zero-sized fonts on the container. Use @mixin !)

Page 30: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Display: Inline-Block (Coda)

Those deceptively-reasonable .clearfix classes that don’t need extra HTML?

They need :after …

… which requires CSS 2.1 …

… which has display: inline-block

Page 31: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Bonus Confusing Layout Idol To Consider Burning

Ems (Browser scaling works great)

(In practice, people use a range of assistive technologies, and very rarely rely on custom stylesheets.)

Page 32: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Challenge 2:

Make CSS Code Maintainable

Page 33: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Where we *think* we spend all our time:

TYPING

Page 34: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Where we *actually* spend all our time:

1. READING 2. TRYING to understand 3. RE-reading 4. MIS-understanding 5. Revert to: Step 1

Page 35: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

“Maintainability” ===

OPTIMIZE FIRST FOR THE PROCESSOR

INSIDE YOUR SKULL

Page 36: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

“In God, we trust.

All others: bring evidence.”

Page 37: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

OK… Maintainability… How?

1. AUTOMATE (OR DIE)

2. “NAME ALL THE THINGS”… CLEARLY

3. USE ELEMENT SELECTORS ONLY FOR RESETS

4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL*

5. AVOID UNNECESSARY COMPLEXITY

Page 38: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Maintainability… How?

1. AUTOMATE (OR DIE)

2. “NAME ALL THE THINGS”… CLEARLY

3. USE ELEMENT SELECTORS ONLY FOR RESETS

4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL*

5. AVOID UNNECESSARY COMPLEXITY

Page 39: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

AUTOMATION

Enough with the FUD. It’s easier than you think. Just use a (pre-)compiler. Always. (See Yeoman for help.)

A new age is upon us.

There is literally not enough time in this session to detail all the ways in which a compiler will make your CSS better.

But here’s a few: you’ve heard about ‘variables’ and ‘mixins’, but not *why* you need them:

Page 40: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

AUTOMATION Examples: Human-Readable Colors

Stop working directly with color codes! Use human-readable references like $brand-primary-highlight or $ugly-greenish-blue.

Lighten, Darken, Opacify, Transparentize, using a single original reference color. Make a whole chart w/: color: $myRed $redTwo: darken($myRed, 10%) $redThree: darken($myRed, 20%)…

‘Theme’ a design with just a few variables, and @import

Page 41: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

AUTOMATION Example: Easy Responsive

#key-component__guide-text { @include apply-at-max-size { width:78%; } @include apply-at-med-size { width: 50%; } @include disappear-at-sm-size; }

Page 42: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

AUTOMATION Example: Easy Responsive

@mixin apply-at-max-size { @media (min-width: $screen-md-min + 1) { @content; }} @mixin apply-at-med-max-size { @media (min-width: $screen-sm-min) { @content; }} @mixin disappear-at-sm-size { @include apply-at-sm-size { display: none; }}

Page 43: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

AUTOMATION Example: Easy Responsive

$screen-xs-min: 320px; $screen-sm-min: 550px; $screen-md-min: 768px; …etc.

Page 44: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

MOAR AUTOMATION

Browser prefixes suck. Use either @mixin’s… or Grunt!

Easy theming: take theme-specific variables & @mixin’s, then @import your default rules, and voilà!

Page 45: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

1. AUTOMATE (OR DIE)

2. “NAME ALL THE THINGS”… CLEARLY

3. USE ELEMENT SELECTORS ONLY FOR RESETS

4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL*

5. AVOID UNNECESSARY COMPLEXITY

Maintainability… How?

Page 46: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Who was it who told us that this:

… was a good idea? Instead of this:

#nav-list li a { … stuff… }

.nav-list-link { … same stuff … }

… or better… this:.main-header__nav-list__link { … same stuff … }

Page 47: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Stop Rationing Classes!

It’s as if we were afraid that classes were radioactive: too many, too close together, and they’d go super-critical

Or as if they were contaminating our pristine HTML

We also like to feel clever. (Be afraid of that instinct.)

Classes are as efficient as it gets, speed-wise.

Page 48: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Use Clear, Descriptive Classes

It shouldn’t be necessary to hunt for things. Class names should tell us where to expect to find things.

Sometimes, the hardest thing about using good names is just inventing ones that make sense.

Use any system you like (BEM, OOCS, Suit), which is both highly descriptive, and which helps you design good class names.

(Remember they’re helpful patterns, not religious faiths.)

Page 49: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

A Note on Namespacing

As a way of collecting a ‘module’ of related content or functionality, namespacing is super-cool.

But it quickly goes fractal with automation.

Consider whether using detailed, descriptive class names gets you what you were probably really after: the avoidance of collisions.

(See the section on over-nesting.)

Page 50: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

1. AUTOMATE (OR DIE)

2. “NAME ALL THE THINGS”… CLEARLY

3. USE ELEMENT SELECTORS ONLY FOR RESETS

4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL*

5. AVOID UNNECESSARY COMPLEXITY

Maintainability… How?

Page 51: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

About That Whole ‘Cascade’ Thing

No matter how much we explain it, the ‘back-end’ folks will never understand why we would allow five different user-created style declarations to apply to a single element

They’re right: it’s not maintainable.

The “browser is designed that way” isn’t a good enough reason.

We *can* do that, but we *shouldn’t*.

Page 52: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

About That Whole ‘Cascade’ Thing

Element selectors are the number one reason we end up fighting the cascade, and hoping that the Specificity algorithm is on our side today.

You don’t need them.

One good use: resets and global styles, like:a { text-decoration: none; color: red; }

Page 53: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

About That Whole ‘Cascade’ Thing

Use of !important is always a “code-smell”

Maybe use @extend or @include to create a narrower class, rather than fight the existing one

“Be a lover, not a fighter”

Page 54: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

1. AUTOMATE (OR DIE)

2. “NAME ALL THE THINGS”… CLEARLY

3. USE ELEMENT SELECTORS ONLY FOR RESETS

4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL*

5. AVOID UNNECESSARY COMPLEXITY

Maintainability… How?

Page 55: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

form label input

General use of descendant selectors is anathema to real specificity, and guarantees conflicts and overrides:

!

Descendant Selectors lock your content into a single context, making them hard to re-use, or re-locate.

Plus, you can’t relocate other code into yours: ever try to add a JQuery date-picker into code that’s styled with descendant selectors?

Descendant Selectors: The Problems

#login-form input .name-field

Page 56: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Pre-Compilers Gone Wrongbody { font-size: 12px; #main-content: { width: 400px; height: 200px; ! .left-nav { width: 200px; margin: 0 0 0 50px; ! ul { li { display: block; list-style-type: none; … ∞ … }}}}}

body #main .left-nav ul li …

5 selectors and counting, where 1 would do it.

Page 57: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Descendant Selectors: Performance

The most important thing you can know about selector optimization is this: they’re evaluated right-to-left.

The most important piece of any selector is thus the last piece, not the first. Which is pretty much the opposite of what we usually do with descendant selectors:

The fact that we *can* nest and qualify selectors… doesn’t mean that we *should*.

#very-specific-id .semi-specific-class li a

Page 58: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Nesting Without Stacking

A (pre-)compiler w/ BEM-like syntax gives us readability of related styles, without descendant-selector headaches:

(If you don’t like those syntaxes, at least just use indentation instead of nesting.)

.main-content: { &__left-nav { &__item }}}

compiles to:.main-content {} .main-content__left-nav {} .main-content__left-nav__item {}

Page 59: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Nesting & Stacking

Rule of thumb: nest selectors and stack classes when you *have to*, not just when you *can*. Rule of thumb: nest and stack your @mixin’s and @extend’s, not your selectors.

@mixin body-text { color: green; font: { size: 14px; family: Arial; } }

@mixin nav-list__item { line-spacing: 1; list-style-type: none; display: inline-block; vertical-align: top; }

.header__nav-list__item { @include body-text; @include nav-list__item; padding: 5px; }

Page 60: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Nesting & Stacking

Relying on @mixin and @extend with descriptive classes, rather than on stacked, generic classes means that refactoring is easy, and overrides (if necessary) happen where you can see them, and plan them: in your code. If it comes time to refactor for performance, your most-used mixins can easily convert to standalone classes. Going the other direction… not so much.

Page 61: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Stacking Classes: Pros & Cons

Stacking (<div class=“class1 class2”) is great if you can’t re-write your classes (ie. Bootstrap, components)

Stacking is declarative, at least in your HTML

It’s not at all declarative back in the CSS, forcing us to use the Dev Tools’ Style Inspector to see what happens

It puts you into ‘fighting’ mode: overriding the conflicts

For Optimization: use tactically, not strategically

Page 62: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Stacking Classes: Pros & Cons

A Thought: how many of the reasons why we stack our classes are because we didn’t have pre-compilers back when we started doing it?

Page 63: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

1. AUTOMATE (OR DIE)

2. “NAME ALL THE THINGS”… CLEARLY

3. USE ELEMENT SELECTORS ONLY FOR RESETS

4. STOP OVER-NESTING… OR MAYBE NESTING *AT ALL*

5. AVOID UNNECESSARY COMPLEXITY

Maintainability… How?

Page 64: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Don’t Fight Your Elements

Do you really know if <section>, <article>, etc. do anything for you? (hint: they don’t) Use them only if they’re helpful in some human-readable way.

Don’t feel guilty! The era of semantic *elements* for machine-readability is largely over, in favor of semantic *attributes*. See: WAI-ARIA, microformats

Page 65: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Don’t Fight Your Elements

Use the most flexible elements available; ones you can rearrange and refactor without breaking them.

Just because some content is vaguely ‘tabular’ in nature doesn’t mean you *have* to use a <table>. You can’t scroll the <tbody>, and styling is painful. So, don’t do it.

Just because somebody once said that <ul> is more ‘semantic’ than <div> for navigation items doesn’t mean it’s still worth fighting with it (particularly now that we have <nav>) Does it *look like* a list? No? Then don’t.

Page 66: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

Pseudo-Helpful

When you don’t control a template, and can’t assign classes, pseudo-selectors are great. Otherwise, maybe not.

Expensive for what you get.

Are there other options? Can Javascript help? For example, would using Angular’s $first and $last maybe work *at least* as well as :first-child/:last-child?

(Remember: a new age is upon us.)

Page 67: Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014

THANKS!(Now go teach those ‘back-end’ folks some tricks.)

!

about.me/XML #xmlilley