slaying the dragon: refactoring css using sass

41
SLAYING THE DRAGON Refactoring CSS for Maintainability Using Sass Alicia Liu @aliciatweet

Upload: fitc

Post on 09-Feb-2017

138 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: Slaying the Dragon: Refactoring CSS Using Sass

SLAYING THE DRAGONRefactoring CSS for Maintainability Using Sass

Alicia Liu @aliciatweet

Page 2: Slaying the Dragon: Refactoring CSS Using Sass

CSS

$#@%!

Page 3: Slaying the Dragon: Refactoring CSS Using Sass

I eat man-hours for breakfast. RAWR.

I’m cute.Play with me!

Page 4: Slaying the Dragon: Refactoring CSS Using Sass

PREPARING FOR YOUR QUEST

Page 5: Slaying the Dragon: Refactoring CSS Using Sass

to rewrites

Page 6: Slaying the Dragon: Refactoring CSS Using Sass

Non-blocking Productive Not go crazy

Goals

Page 7: Slaying the Dragon: Refactoring CSS Using Sass

KNOW YOUR ENEMY

Page 8: Slaying the Dragon: Refactoring CSS Using Sass

#header {position:relative;margin-bottom:0;padding-top:50px;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;background:url(/images/backgrounds/fade.png)topleftrepeat-x;height:auto!important;}#header.container {background:url(/images/backgrounds/profile.png)41px0repeat-y;padding-bottom:25px;margin:0auto0auto;}#header_pic {position:absolute;top:3px;left:10px;margin-right:20px;text-align:middle;margin-top:-7px;box-shadow:#C5C5C51px1px1px;-moz-box-shadow:#C5C5C51px1px1px;-webkit-box-shadow:#C5C5C51px1px1px;border:2pxsolidwhite;border-radius:999px;-webkit-border-radius:999px;-moz-border-radius:999px;}#header_picimg {width:50px;height:50px;border-radius:999px;-webkit-border-radius:999px;-moz-border-radius:999px;}#header_info {margin-left:100px;width:auto;max-width:350px;margin-bottom:5px;}#header_info_name {display:block;color:#5b5b5b;}#header_info_name:hover {text-decoration:none;}#header_info_challenge_name {font-size:2.3em;font-weight:bold;line-height:100%;}#header_momentum_gauge {position:absolute;top:3px;left:10px;}

.tabs{position:absolute;top:2px;left:2px;width:351px;border-radius:5px5px00;padding:12px0015px;margin:005px0;height:41px;list-style:none;background:#ecf0f0;box-shadow:#fff03px2px;-webkit-box-shadow:0px15px15pxwhite;-moz-box-shadow:white0px15px15px;z-index:5;}.tabsli{float:left;padding:9px22px14px22px;margin-right:5px;font-size:1.3em;border-radius:5px5px00;-webkit-border-radius:5px5px00;-moz-border-radius:5px5px00;color:#9c9c9c;cursor:pointer;}.tabs_selected{background:white;background:white;color:#333!important;font-weight:bold;box-shadow:#C5C5C51px1px1px;position:relative;}.tabs_selected_cover {position:absolute;z-index:2;background:white;bottom:-5px;left:0;width:100%;height:5px;}

Page 9: Slaying the Dragon: Refactoring CSS Using Sass

Warning Signs

Over-reliance on IDs

Generic class names

Inconsistent vendor prefixing

Too verbose

??? Formatting

Page 10: Slaying the Dragon: Refactoring CSS Using Sass

Code Smells

!important

.header.groupullia

.header>#nav

Page 11: Slaying the Dragon: Refactoring CSS Using Sass

Where?inline head duplicates

Page 12: Slaying the Dragon: Refactoring CSS Using Sass

TOOLS & TACTICS

Page 13: Slaying the Dragon: Refactoring CSS Using Sass

CSS WITH SUPERPOWERS

Page 14: Slaying the Dragon: Refactoring CSS Using Sass

Choose Your Own Adventure

Page 15: Slaying the Dragon: Refactoring CSS Using Sass

Style Guide

Page 16: Slaying the Dragon: Refactoring CSS Using Sass

Colors Font sizes Buttons Device widths Etc.

Design

Page 17: Slaying the Dragon: Refactoring CSS Using Sass

px, em, rem, %, vw… white space Allow IDs? Max nesting level

Code

Page 18: Slaying the Dragon: Refactoring CSS Using Sass

scss-lint Linter + IDE = 💖

Use A Linter

Page 19: Slaying the Dragon: Refactoring CSS Using Sass

BEM OOCSS SMACSS DIY

Naming Convention

Page 20: Slaying the Dragon: Refactoring CSS Using Sass

“There are only two hard things in Computer Science:cache invalidation and naming things.”

Phil Karlton

…and off-by-one errors

Page 21: Slaying the Dragon: Refactoring CSS Using Sass

.loading

.global-loading-indicator✔

Be Specific

Page 22: Slaying the Dragon: Refactoring CSS Using Sass

.red-alert

.dialog-error-alert✔

Semantic Names

Page 23: Slaying the Dragon: Refactoring CSS Using Sass

.heading

.sidebar-heading✔

.sidebar-callout-heading✔

Namespace

Page 24: Slaying the Dragon: Refactoring CSS Using Sass

Separate Behavior

Prefix.js-✔

Data-attribute✔

Page 25: Slaying the Dragon: Refactoring CSS Using Sass

.menu.blue

.menu.menu-cool✔

Single Purpose Modifiers

Page 26: Slaying the Dragon: Refactoring CSS Using Sass

Single Class<divclass="menumenu-widemenu-blue">

.cool-menu{@includemenu($blue);width:$menu-wide-width;//morestyleshere}

Page 27: Slaying the Dragon: Refactoring CSS Using Sass

@extend?

Author CSS > Output CSS

Page 28: Slaying the Dragon: Refactoring CSS Using Sass

Code Organization

@import'third-party';@import'variables';@import'mixins';@import'fonts';@import'elements';

Page 29: Slaying the Dragon: Refactoring CSS Using Sass

@import'alerts';@import'buttons';

@import'nav';@import'comments';@import'products';@import'footer';

@import'responsive';@import'nav.responsive';

Page 30: Slaying the Dragon: Refactoring CSS Using Sass

How many CSS Files?

IE, webview, email, contexts …

Page 31: Slaying the Dragon: Refactoring CSS Using Sass

EASY TO CHANGE

Page 32: Slaying the Dragon: Refactoring CSS Using Sass

READY? REFACTOR!

Page 33: Slaying the Dragon: Refactoring CSS Using Sass

STOP THE BLEEDING

Page 34: Slaying the Dragon: Refactoring CSS Using Sass

LEAVE IT BETTER THAN YOU FOUND IT

Page 35: Slaying the Dragon: Refactoring CSS Using Sass

( °□°) ︵

Don’t Try To Do It All At Once

Page 36: Slaying the Dragon: Refactoring CSS Using Sass
Page 37: Slaying the Dragon: Refactoring CSS Using Sass

Moving & Adding Styles Create new files

Don’t extend, duplicate

Namespace your new styles (e.g. beta-)

Or rename your deprecated styles

Page 38: Slaying the Dragon: Refactoring CSS Using Sass

Deleting StylesTools: uncss, grunt-ucss

Regular expressions are your friend

Search everywhere: JavaScript, HTML, backend code

Test as you go

Page 39: Slaying the Dragon: Refactoring CSS Using Sass

Asp.Net Python Ruby Javascript

Java Objective-C C++ Perl

Treat Css As Code

Page 40: Slaying the Dragon: Refactoring CSS Using Sass

HAPPILY AFTER…

Photo by Matteo maggionihttps://flic.kr/p/7BzbhK

Page 41: Slaying the Dragon: Refactoring CSS Using Sass

Thanks!

Alicia Liu @aliciatweet

alicialiu.me/talks