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

What If CSS Was Object-Oriented?

What If CSS Was Object-Oriented?

Made in collaboration with the inimitable Ben Bayard, presented originally for the Moovweb Tech Services team.

A lot of times developers with stronger CS backgrounds than web experience write brute-forced CSS because it's not a real programming language with real language features. This is a talk about using object-oriented principles with Sass to write excellent styles.

Liam Campbell

January 23, 2014
Tweet

More Decks by Liam Campbell

Other Decks in Programming

Transcript

  1. class blueHeading implements Font(); blueHeading.fontColor = "#56A9F6"; blueHeading.fontSize = "32px";

    blueHeading.fontWeight = "bold"; blueHeading.underline = true; blueHeading.capitalize = "capitalize"; blueHeading.lineHeight = goldenRatio("32px");
  2. var size = "32px"; class blueHeading implements Font({ capitalize: "capitalize",

    fontColor: "#56A9F6", fontSize: size, fontWeight: "bold", underline: true, lineHeight: goldenRatio(size) });
  3. Let’s do the same thing for this heading: Even Cooler

    Heading A NEW CHALLENGER APPROACHES!
  4. var size = "32px"; class redHeading implements Font({ capitalize: "capitalize",

    fontColor: "#84120A", fontSize: size, fontWeight: "bold", underline: true, lineHeight: goldenRatio(size) });
  5. var size = "32px"; class redHeading implements Font({ capitalize: "capitalize",

    fontColor: "#84120A", fontSize: size, fontWeight: "bold", underline: true, lineHeight: goldenRatio(size) }); var size = "32px"; class blueHeading implements Font({ capitalize: "capitalize", fontColor: "#56A9F6", fontSize: size, fontWeight: "bold", underline: true, lineHeight: goldenRatio(size) });
  6. var size = "32px"; ! class Heading implements Font({ capitalize:

    "capitalize", fontSize: size, fontWeight: “bold", lineHeight: goldenRatio(size), underline: true }); ! var redHeading = new Heading({ color: "#84120A" }); ! var blueHeading = new Heading({ color: "#56A9F6" });
  7. // an extensible class and functions! class Heading implements Font({

    capitalize: "capitalize", fontSize: size, fontWeight: "bold", underline: true, lineHeight: goldenRatio(size), }); %heading { font-size: $size; font-weight: bold; text-transform: capitalize; text-decoration: underline; @include line-height(golden-ratio(size)); }
  8. // the implementing objects var redHeading = new Heading({ color:

    "#84120A" }); ! var blueHeading = new Heading({ color: "#56A9F6" }); .red-heading { color: "#84120A"; @extend %heading; } .blue-heading { color: "#56A9F6"; @extend %heading; }
  9. WTFUH EXTEND?! // the implementing objects var color: }); !

    var color: }); .red-heading } .blue-heading }
  10. $size: 32px; ! %heading { font-size: $size; font-weight: bold; text-transform:

    capitalize; text-decoration: underline; @include line-height(golden-ratio(size)); } ! .red-heading { color: "#84120A"; @extend %heading; } .blue-heading { color: "#56A9F6"; @extend %heading; }
  11. YAY

  12. WHAT DOES IT BEHAVE LIKE? HEADINGS CONTAINERS BUTTONS DROP-DOWNS BARS

    MODALS POTATOES CAROUSELS INPUTS SLIDERS TOGGLERS TEXT SNIPPETS LINKS
  13. Q+A