Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why Your Sass is Bad and You Should Feel Bad

Why Your Sass is Bad and You Should Feel Bad

This talk was a collaboration with Ben Bayard, originally presented to the Tech Services team at Moovweb.

Sass makes CSS even easier to write, which means that in fewer keystrokes, a Sass codebase can become a bloated, unreadable mess. These are some basic principles on how to write better Sass (and better underlying CSS) and stop sucking so bad.

Liam Campbell

January 23, 2014
Tweet

More Decks by Liam Campbell

Other Decks in Programming

Transcript

  1. MEANINGFUL NAMES #ctl00_FPCLogin1_hlnkLogin {} ! .mw-pdp h2 a .text {}

    ! h2 a {} ! .mw-login-link {} BAD BAD GOOD BAD
  2. NESTING } } } } } } } } }

    } } } (You’re a web developer, not a bird. Stop nesting like you’re trying to protect your eggs.)
  3. NESTING Don’t nest to emulate the DOM. ! If you’re

    nesting more than three levels, look at your life. Look at your choices. ! Nest to increase specificity in order to override a style, to expand upon a style, or to style a child class within context.
  4. .mw-pink-button { color: white; padding: $spacing; background-color: $light-pink; } Regular

    flat class .mw-order-form { .mw-pink-button { background-color: $darker-pink; } } Override a style .mw-pink-button { &.small { padding: $spacing/2; } } Expand upon a style .mw-pink-button { .sprites-cool-image { position: absolute; right: 10px; top: 50% } } Style a child in context
  5. GOOD MIXIN @include border-radius(4px, 4px); generates... -webkit-border-radius: 4px 4px; -moz-border-radius:

    4px / 4px; -khtml-border-radius: 4px / 4px; border-radius: 4px / 4px; (Even though the border-radius mixin is deprecated, it’s a good example)
  6. %mw-button { border-radius: 3px; font-family: $font-text; font-size: 12px; font-weight: bold;

    padding: 0 10px; } ! .mw-button-yellow { @extend %mw-button; border: 1px solid $color-yellow1; @include background(linear-gradient($color-yellow2, $color-yellow1)); color: #fff; text-shadow: 0 1px rgba(0, 0, 0, 0.2); }
  7. PLACEHOLDERS The “%” in %mw-button means “placeholder” Placeholders don’t appear

    in your CSS, but they add whatever they are extended within to a selector chain. ! This makes it really easy to repeat code in your Sass while keeping your CSS totally DRY.
  8. NEXT STEPS! Forthcoming styleguide and formalized procedure based on team

    discussion ! Experiments with structure and new techniques