Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

WordCamp NYC - DRY CSS


Published on

DRY CSS - Theries, methods and tools for efficient and scalable Stylesheets. A talk given at WordCamp NYC 2012.

The value of keeping all code D.R.Y (Don't Repeat Yourself) is universally accepted and demanded in the world of programming, but when it comes to CSS we've all learned to compromise and live in a wet mess. We don't have to! This talk will go over how you can use thematic and structural CSS definitions to cut down on wasted code, simplify maintenance and unify your site styles just by DRYing out your standard CSS. I'll explain how tools like Firebug enable a whole new way of understanding complex style interrelationships and give examples of the particular patterns that let me cut one stylesheet from ~4200 to ~2400 lines without losing anything but wasted code! We'll also cover how preprocessors like LESS and SASS can take your DRY CSS even farther by actually programming your styles as if they were made of PHP.

Published in: Technology, Education
  • Be the first to comment

WordCamp NYC - DRY CSS

  2. 2. WHO IS JEREMY CLARKE?• Communications Studies at Concordia University.• HTML+CSS since 2003• Montreal WordPress Community/ WordCamp organizer.
  3. 3. OVERVIEW• An example of DRY CSS, Global Voices.• DRY principle and problems with CSS.• DRY CSS theory, implementation and benefits.• Preparsers (LESS and SASS)
  5. 5. DRY EXAMPLE: GLOBAL VOICES Before DRY CSS After DRY CSS• Applying DRY reduced 4200 5,000 lines to 2400. 3,750• Standardized Shapes, text sizes, colors and modules. 2,500 1,250• Nothinglost, just better organized. 0 Lines of CSS
  6. 6. DRY EXAMPLE: GLOBAL VOICES• Groups define shared properties.• Groups have many selectors.• Each property/value pair is defined only once.
  8. 8. DRY EXAMPLE: GLOBAL VOICES• Elegantly organized IE hacks and Right-To-Left (RTL) styles into groups for the common fixes.• Font-sizegroups re-used in optional Large-Font theme.
  9. 9. 2 PRINCIPLES OF GOOD CSS• Keep style separate from content. • HTML should be structural. Tags, classes and IDs should refer to the content itself rather than how it looks.• Avoid specificity by harnessing the cascade. • Selectors and definitions should be as global as possible for design consistency and to avoid specificity collisions.
  10. 10. DRY: DON’T REPEAT YOURSELF• a.k.a "Single Source of Truth" principle.• "Everypiece of knowledge must have a single, unambiguous, authoritative representation within a system." *• DRY code means all uses of data change simultaneously rather than needing to be replicated.• DRY is the fundamental reason to use of variables and functions in all programming.• Bugs love a wet mess. *
  11. 11. PROBLEMS WITH KEEPING CSS DRY• No variables - We can’t re-use values across properties (I.e a color hex).• No mixins (functions) - No way to re-use sets of property/ value pairs across definitions.• Result: Constant duplication of style definitions across different selectors.
  12. 12. PRINCIPLES OF DRY CSS (TL;DR)• Dont Repeat Yourself. Never repeat a style/property definition if you can avoid it.• Group selectors with shared properties rather than defining them separately.
  13. 13. CREATING DRY CSS• Group selectors that share properties above the properties they share.• Name the groups based on their role in the design.• Use the name as an ID at the top of the list and a class at bottom.
  14. 14. ORGANIZING DRY CSS• Decide on group types and groups as necessary for your design, operational needs and/or other CSS methodologies.• Recommended: Colors, Text, Shapes, Structures, Modules.• At right: GV’s DRY groups, visualized by CSSEdit for Mac* *
  15. 15. KEEPING IT DRY• Make individual selectors as rare and sparse as possible, only use them as exceptions.• Always have an answer to "Why isnt this part of a group?"• Don’t go crazy. Benefits scale even if some definitions remain moist.
  16. 16. BENEFITS OF DRY CSS•2 simple rules to follow that ensure a solid organizational structure.• Less CSS, shorter and smaller (kb) stylesheets.• Promotes good design practice and uniform updates.• Uses only philosophically and syntactically standard CSS.
  17. 17. BENEFITS: PROMOTES CONCEPTUAL DESIGN• Encourages you to think in terms of style patterns (groups), rather than individual objects.• Naming of groups encourages rational organization of designs.• Listing all group members together encourages optimization and generalization of selectors because you see how interrelated/inheritable they are.
  18. 18. BENEFITS:EDITS TO GROUPS AFFECT ALL MEMBERS• Avoids need to find all related styles to match changes.• Changes are less likely to be inconsistently applied.• When testing changes the dev sees all group members change as well.• Moving selectors between groups to restyle is fast and easy.
  19. 19. BENEFITS: TAKES ADVANTAGE OF INSPECTORS• Inspecting an object shows cascade of groups it participates in.• Group "name" (starting ID, i.e. #SMALL-TEXT) is visible in inspector.• Seeing all group members informs dev of related elements that should be considered.• Live-edits expose the change in related items as well.
  20. 20. BENEFITS:DOESN’T REQUIRE CHANGES TO HTML• Uses whatever classes and IDs are already present.• IfHTML needs editing its only to add more detailed semantic classes to use in your DRY groups.• Useful when generated HTML is out of your control (WordPress core/plugins, 3rd party widgets)• Mainly relevant when comparing DRY with OOCSS.
  21. 21. BENEFITS: COMPLETELY STANDARD• Simply an organizing principle for normal CSS.• Adheres to separation of style and content.• Bothbackward and forward compatible, from IE6 hacks to prefixed CSS3 properties.• CSS can be pasted anywhere and just work.• Groupscan be extracted and re-used elsewhere by removing group members and inserting new ones.
  22. 22. BENEFITS: INTEGRATES WITH OTHER METHODOLOGIES• Compatible with most other CSS methodologies like grids, OOCSS* or SMACSS** because the way you organize your groups is up to you.• Progressiveenhancement and browser support is your choice. DRY only defines how you organize your selectors and properties. * **
  23. 23. PERFORMANCE ISSUES?• TL;DR: Probably not.• "Formost web sites, the possible performance gains from optimizing CSS selectors will be small, and are not worth the costs." ~Steve Sounders*• Having many selectors does not inherently hurt performance.• SeeSteve Sounders research** for optimizations that matter (and can be applied to DRY CSS). * **
  24. 24. CSS PREPARSERS• Add new languages (LESS/SASS) based on CSS but with additional syntax and a new parsing layer to read it. • Variables to re-use values across definitions. Variables in LESS* • Mixins or inheritance to include a class inside another class. • All kinds of other programmy things (functions, switch conditions) Mixins in LESS** E.g. and
  25. 25. CSS PREPARSERS: CONSIDERATIONS• Allow for extensive programming by experts.• External frameworks (blueprint, grids etc.) can be included and updated without modification (don’t hack core).• BUT at the cost of destandardization and dependence on external libraries or software. It isn’t CSS.• Theycan still be misused to create wet, sloppy stylesheets. You still have to plan your design.
  26. 26. CSS PREPARSERS: SOLUTIONS• Use for development only, publish natural CSS with themes.• Be careful to create CSS that is still elegant in natural form.• Use “Inheritance” rather than “mixins” to avoid repetition. *SASS examples from LESS should have this feature soon.
  27. 27. CSS PREPARSERS: TOOLS• WP-Less (WP plugin) Adds .less support to WP, but requires a version of the plugin on all sites: plugins/wp-less/• Codekit/ (OSX only) Automatically compiles SASS/LESS into CSS during development
  28. 28. DRY CSS REVIEWGroup re-usable Name the Add selectors to properties groups logically various groups
  29. 29. QUESTIONS?
  30. 30. DRY CSS THEORIES, METHODS AND TOOLS FOR EFFICIENT AND SCALABLE STYLESHEETSJeremy Clarke • Creative Commons Share Alike Download these slides: