42
DISPLACING WORST PRACTICES (and making better CSS for all) Pam Selle @pamasaur thewebivore.com

Displacing Worst Practices in CSS

Embed Size (px)

DESCRIPTION

Slides from a talk given at CSS Dev Conf 2012

Citation preview

Page 1: Displacing Worst Practices in CSS

DISPLACING WORST PRACTICES(and making better CSS for all)

Pam Selle@pamasaur

thewebivore.com

Page 2: Displacing Worst Practices in CSS

STORYTIME

Page 3: Displacing Worst Practices in CSS

No.

Page 4: Displacing Worst Practices in CSS

Yes.

Page 5: Displacing Worst Practices in CSS

THE PLAN

•What are the ‘worst practices’?

• Identifying

• Solving

• Practices for prevention

Page 6: Displacing Worst Practices in CSS

IDENTIFYING WORST PRACTICES

Page 7: Displacing Worst Practices in CSS

REDUNDANCY

• Symptom: Declaring styles that are inherent, styles that negate other ones in the chain.

• Solution: Take your freebies and remove things that don’t matter.

div { display: block; }.my-class { display: block; float: left; }img { display: inline-block; }span { display: inline; }.nav { position: absolute; float: left; }

Page 8: Displacing Worst Practices in CSS

REDUNDANCY

Prescription:

Mozilla Developer Network

Page 9: Displacing Worst Practices in CSS

REPETITION AND DISORGANIZATION

• Symptom: See many of the same declarations over and over.

• Symptom: See styles being overridden in web inspector.

• Symptom: Having to reset styles because of previous declarations.

Page 10: Displacing Worst Practices in CSS

REPETITION AND DISORGANIZATION

.blue-title { color: blue; font-size: 16px; font-family: Arial }.blue-content { color: blue; font-size: 14px; font-family: Arial }

.blue-title, .blue-content {color: blue; font-family: Arial; }.blue-title { font-size: 16px; }.blue-content { font-size: 14px; }

%blue-thing {color: blue; font-family: Arial; }.blue-title { @extend %blue-thing; font-size: 16px; }.blue-content { @extend %blue-thing; font-size: 14px; }

Page 11: Displacing Worst Practices in CSS

h2 { border-bottom: 1px solid #ddd; padding: 10px 0; float: left;}h2.other-title { border-bottom:none; padding:0 0 5px; float:none;}

.secondary-title { border-bottom: 1px solid #ddd; padding: 10px 0; float: left;}.other-title { padding:0 0 5px;}

Page 12: Displacing Worst Practices in CSS

REPETITION AND DISORGANIZATION

Prescription:

Refactoring, with a side of architecture.

Page 13: Displacing Worst Practices in CSS

DETOUR INTO PREPROCESSORS

• Favorite benefits of preprocessors:

• Partials (architecture)

• Extendables (in Sass 3.2) %

• Sprite benefits (Compass)

Page 14: Displacing Worst Practices in CSS

OVERSPECIFICITY

•Wasting your time and the browser’s time

.header .logo .title.name { }#nav ul li a { }.content a.title { }

.logo > .title { }

.nav li > a { }

.content .title { }

Page 15: Displacing Worst Practices in CSS

OVERSPECIFICITY

Prescription:

Understanding how browsers interpret CSS.

Page 16: Displacing Worst Practices in CSS

HOW BROWSERS EVALUATE CSS

Resources:https://developers.google.com/speed/docs/best-practices/renderinghttps://developer.mozilla.org/en-US/docs/CSS/Writing_Efficient_CSS

The engine evaluates each rule from right to left, starting from the rightmost selector (called the "key") and moving through each selector until it finds a match or discards the rule. (The "selector" is the document element to which the rule should apply.)

Page 17: Displacing Worst Practices in CSS

HOW BROWSERS EVALUATE CSS

Specificity:

1. Ids

2. Classes

3. Tags

4. UniversalsFrom SpeciFISHity by Weyl

Page 18: Displacing Worst Practices in CSS

INVALID DECLARATIONS• Symptom: Crossed out in web inspector, because it’s wrong,

not overridden.

• Symptom: A style is written, but not rendering. (because it’s wrong)

• Solution: ⌫. And read the MDN.

.classy { background-position: inital initial; font: Arial; font: 14px/16px Arial sans-serif; padding: -10px;}

Page 19: Displacing Worst Practices in CSS

NO FALLBACKS• Know what you support, and debug before you see it in the

browser.

•Most common with colors, so add a simple color (background-, border-) declaration fallback.

• Includes setting position properly -- a common problem in some browsers

Page 20: Displacing Worst Practices in CSS

NO FALLBACKS

Common Suspects:rgba()linear-gradientradial-gradientbox-shadowposition: absolute (forgetting a vertical or horizontal position)

Page 21: Displacing Worst Practices in CSS

INLINE STYLES

The purpose of styles is to to separate structure from presentation.

Oh, and it will also give you specificity problems.

Page 22: Displacing Worst Practices in CSS

DISPLACING WORST PRACTICES

Page 23: Displacing Worst Practices in CSS

TRAIN EARLY AND OFTEN

Page 24: Displacing Worst Practices in CSS

TRAIN EARLY

• “Bootcamp”

• Presentations on full stack/tools

• Identify available resources

•Documentation, videos, useful tutorials

•Mentoring

Page 25: Displacing Worst Practices in CSS

ONBOARDING

•Documentation, Documentation, Documentation

•Onboarding session

•Make past presentations available

Page 26: Displacing Worst Practices in CSS

KEEP IT UP

• Lightning talks

• Hack Days

• See “Zombies in My Workplace” for more ideas

• http://www.slideshare.net/sbastn/zombies-in-my-workplace

Page 27: Displacing Worst Practices in CSS

STYLEGUIDES

Page 28: Displacing Worst Practices in CSS

TEXTUAL STYLEGUIDES

•Document!

• Style

• Idioms

• Structure

Page 29: Displacing Worst Practices in CSS
Page 30: Displacing Worst Practices in CSS

TEXTUAL STYLEGUIDES

• Public Styleguides

• Google: http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml

• GitHub: https://github.com/styleguide/css

• ThinkUp: https://github.com/ginatrapani/ThinkUp/wiki/Code-Style-Guide:-CSS

• Manifestos

• Idiomatic CSS: https://github.com/necolas/idiomatic-css

• SMACSS: http://smacss.com/

Page 31: Displacing Worst Practices in CSS

VISUAL STYLEGUIDES

• Visual Dictionary

• Explain visuals and usage

• Interface with visual designers

Page 32: Displacing Worst Practices in CSS
Page 33: Displacing Worst Practices in CSS

KSS

•Generated CSS documentationhttp://warpspire.com/kss/

•Learn to generate your own styleguide:http://warpspire.com/kss/styleguides/

Page 34: Displacing Worst Practices in CSS

INTERACTIVE STYLEGUIDES

• Put your elements in full context

•Maintain a site-wide brand

• Reduce, Reuse, & Recycle

Page 35: Displacing Worst Practices in CSS
Page 36: Displacing Worst Practices in CSS
Page 37: Displacing Worst Practices in CSS

CODE REVIEW

Page 38: Displacing Worst Practices in CSS

INTRODUCING CODE REVIEW

• Use your styleguide/documentation as guideline

• Continues mentoring process

• Saves QA time by reviewing before staging

Page 39: Displacing Worst Practices in CSS

MODULAR ORGANIZATION

Page 40: Displacing Worst Practices in CSS

INTRODUCING ARCHITECTURE

• Consider pre-processors

• Consider your build process

• Consider adopting a methodology (OOCSS, etc.)

•Measure everything

• Interface with visual design strategy

Page 41: Displacing Worst Practices in CSS

FINAL INVENTORY

•Identify and repair misguided styles

•Tools to improve and embark on the joyous path of maintainability

Page 42: Displacing Worst Practices in CSS

Thanks.

Pam Sellethewebivore.com

@pamasaur