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

Modular Future of CSS

Rico Sta. Cruz
September 17, 2016

Modular Future of CSS

Originally presented in Meta Refresh 2016, India.

Links:

- RSCSS: http://rscss.io
- BEM: http://bem.info
- SMACSS: https://smacss.com
- OOCSS: http://www.slideshare.net/stubbornella/object-oriented-css

Rico Sta. Cruz

September 17, 2016
Tweet

More Decks by Rico Sta. Cruz

Other Decks in Technology

Transcript

  1. The modular future of CSS @rstacruz Rico Sta. Cruz ricostacruz.com

    Subtitle here Title goes here ACTION 2 ACTION 1 ACTION Single-line snackbar 60 Groceries: almond milk coconut water cucumber Campbell Groceries: almond milk coconut water cucumber Trevor Hansen Arial Courier Calibri Arial Verdana Courier Calibri Arial 1 4:15 One Day DISABLED DISABLED Subtitle here Title goes here ACTION 2 ACTION 1 ACTION Single-line snackbar 60 Campbell Groceries: almond milk coconut water cucumber Campbell Groceries: almond milk coconut water cucumber Trevor Hansen Arial Verdana Courier Calibri Arial Verdana Courier Calibri Arial 1 4:15 One Day DISABLED DISABLED
  2. The Tragedy of Hamlet, Prince of Denmark William Shakespeare c.

    1599–1602 Shakespeare's Hamlet took up to 3 years to write.
  3. Show everything from $NAME in this folder filter it by

    lines added count the number of words git log --author=$NAME -c \ -- app/assets/stylesheets/ \ | grep -E '^\+ ' \ | wc -w Let's inspect how one of my coworkers wrote CSS code.
  4. git log ... | grep | wc -w 43671= 1.25

    Hamlets One coworker wrote 1.25 Hamlets worth of CSS.
  5. WordPress 4.6.1 Significant line of code count More than half

    our code bases is front-end code. PHP 165k lines CSS 76k JS 50k
  6. Pinterest.com: 1.6MB of CSS. That's 47k lines of CSS code,

    148k words of CSS. That's around 4.3 Hamlets of CSS code.
  7. One Hamlet is roughly equivalent of 10k lines of CSS

    code, which, in my experience, is the point when CSS gets really complicated. Assuming around 3.2 words per line.
  8. One of the first books that talked about CSS positioning.

    <font face="Arial" color="#338899"> <b>Zen Garden</b> </font>
  9. People started separating content from style. CO N TE NT

    <h1>Zen Garden</h1> h1 { font-weight: bold; color: #338899; font-face: Arial, sans-serif; } ST Y LE <font face="Arial" color="#338899"> <b>Zen Garden</b> </font>
  10. CSS tricks started to appear. B EF ORE <img src="spacer.gif"

    width="8" height="1"> div { margin-left: 8px; } A FT E R
  11. CSS tricks started to appear. B EF ORE <tr> <td>...</td>

    <td>...</td> </tr> .container { overflow: hidden; } .box { float: left; } A FT E R
  12. Take this component, for example. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU
  13. We started using classes to describe content based on what

    they were. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU <div class='image'> <div class='heading'> <div class='description'> <div class='actions'>
  14. We used the correct HTML tags based on content, too.

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU <div class='heading'> <h2 class='title'> <p class='author'>
  15. This led to a movement of making your markup "semantic."

    One problem— no one knew how to properly write CSS for Semantic HTML. <>
  16. No one knew how to write CSS for it, though.

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU .photo-card { }
  17. We took advantage of Sass, Less, and other preprocessors. UIIUIUIN

    UIIUNIUN UIUNUN INU UINUIU .photo-card { .image { height: 240px; background: $green; } }
  18. No one warned us about nesting too much, though. UIIUIUIN

    UIIUNIUN UIUNUN INU UINUIU .photo-card { .image { ... } .text { padding: 16px; .heading { font-size: 2em; } } }
  19. With more complicated things like media queries... UIIUIUIN UIIUNIUN UIUNUN

    INU UINUIU .photo-card { .image { ... } .text { padding: 16px; .heading { font-size: 2em; @media (min-width: 768px) { font-size: 3em; } } } }
  20. CSS became more and more of a nightmare. UIIUIUIN UIIUNIUN

    UIUNUN INU UINUIU .photo-card { .image { ... } .text { padding: 16px; .heading { font-size: 2em; @media (min-width: 768px) { font-size: 3em; } } .subheading { font-size: .9em; } } }
  21. Too much nesting can kill your brain. UIIUIUIN UIIUNIUN UIUNUN

    INU UINUIU .photo-card { .image { ... } .text { padding: 16px; .heading { font-size: 2em; @media (min-width: 768px) { font-size: 3em; } } .subheading { font-size: .9em; @media (min-width: 768px) { font-size: .95em; } } } }
  22. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU Before you know it, you've written a Hamlet of CSS code. .photo-card { .image { ... } .text { padding: 16px; .heading { font-size: 2em; @media (min-width: 768px) { font-size: 3em; } } .subheading { font-size: .9em; @media (min-width: 768px) { font-size: .95em; } } } } Too many lines, too much CSS.
  23. What have we learned so far: ✓ Separate content and

    style. 2003 ✗ Writing CSS properly is hard. 2003
  24. We started using classes to describe content based on what

    they were. Button Save Delete More info
  25. This was very similar to some OOP patterns that have

    been in use in programming. class Button { ... } class SaveButton extends Button { ... } class DeleteButton extends Button { ... } "Object-oriented Programming"
  26. How about we apply the same ideas to CSS? Button

    Save Delete More info class="button" class="button button-delete" class="button button-save" class="button button-info" PA RE N T SUB - CL AS S
  27. One of the first books that talked about CSS positioning.

    OOCSS: Object Oriented CSS Nicole Sullivan 2009
  28. OOCSS tells us to build components like Lego's. Button Text

    box Campbell Groceries: almond milk coconut water cucumber Abcdefghijklmno Third Second First "Lego's first"
  29. OOCSS tells us to build components like Lego's. Button Text

    box Campbell Groceries: almond milk coconut water cucumber Abcdefghijklmno Third Second First "Components are like legos"
  30. What have we learned so far: ✓ Separate content and

    style. 2003 ✓ Create a component library. 2009 ✗ Writing CSS properly is still hard. 2009
  31. checkin-form invoice-heading invoice-table book-button Everything is a component in a

    component in a component. listing-page listing-actions
  32. "Sub-classing" is still a thing, though no one seems to

    call it that. Button Save Delete More info CO M PO NENT STAT ES
  33. One of the first books that talked about CSS positioning.

    SMACSS: Scalable and Modular Architecture for CSS 2012
  34. Modules was a way to think of your UI in

    pieces. "Modules" listing-actions
  35. Modules can have states. "Layers" Base Layouts Modules States button

    { font-family: sans-serif; } .button-group > button { margin: 0 5px; } .button { ... } .button.delete { background: $red; }
  36. What have we learned so far: ✓ Separate content and

    style. 2003 ✓ Create a component library. 2011 ✓ Everything is a component. 2012 ✗ Writing CSS properly is still hard. 2012
  37. We started turning them into actual conventions. UIIUIUIN UIIUNIUN UIUNUN

    INU UINUIU <div class='photo-card__image'> <div class='photo-card__heading'> <div class='photo-card__description'> <div class='photo-card__actions'> <div class='photo_card'>
  38. Class names are based on block, element and modifiers. UIIUIUIN

    UIIUNIUN UIUNUN INU UINUIU Block Element .photo-card .photo-card__photo .photo-card__heading .photo-card__subheading .photo-card__description .photo-card__description_long .photo-card__description_short .photo-card__action Modifier
  39. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU .photo-card { &__image { ... } &__heading { ... }
 &__description { ... } &__actions { ... } } BEM with Sass made CSS very fun to write. BEM wi t h SAS S
  40. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU .photo-card__image { ... } .photo-card__heading { ... }
 .photo-card__description { ... } .photo-card__actions { ... } The ampersand operator removes redundancies. O U T PU T
  41. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU .photo-card { &__image { ... } &__heading { ... }
 &__description { ... } &__actions { ... } } The ampersand operator removes redundancies. BEM wi t h SAS S
  42. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU <div class='photo-card'> <div class='photo-card__photo'> <img src='foo.jpg' class='photo-card__image'> </div> <div class='photo-card__text'> <h2 class='photo-card__heading'>...</h2> <p class='photo-card__subheading'>...</p> </div> <div class='photo-card__description'> <p>...</p> </div> <div class='photo-card__actions'> <a class='photo-card__button'></a> </div> </div> HTML was painfully dirty, though.
  43. What have we learned so far: ✓ Separate content and

    style. 2003 ✓ Create a component library. 2011 ✓ Everything is a component. 2012 ✓ CSS conventions are extremely useful. 2013 ✗ Writing CSS and markup properly is hard. 2013
  44. Frontend JavaScript was just about taking an interesting turn. React.js:

    JavaScript library for building user interfaces 2014
  45. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU var PhotoCard = React.createClass({ render () { return <div> <PhotoImage /> <PhotoHeading /> <PhotoDescription /> <PhotoActions /> </div> } }) React let you write things in nested components. RE ACT. js
  46. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU Vue.component('PhotoCard', { el: '#card-template' }) <script id='card-template'> <div> <PhotoImage /> <PhotoHeading /> <PhotoDescription /> <PhotoActions /> </div> </script> So did Vue.js. VU E .j s
  47. UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU

    UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU <div> {{photo-image}} {{photo-heading}} {{photo-description}} {{photo-actions}} </div> So did Ember.js. EM BE R .j s
  48. What have we learned so far: ✓ Separate content and

    style. 2003 ✓ Create a component library. 2011 ✓ Everything is a still component. 2012, 2014 ✓ CSS conventions are extremely useful. 2013 ✗ Writing CSS and markup properly is hard. 2014
  49. Everything is a component. Subtitle here Title goes here ACTION

    2 ACTION 1 Subtitle here Title goes here ACTION 2 ACTION 1 ACTION Single-line snackbar 60 ACTION Single-line snackbar 60 Campbell Groceries: almond milk coconut water cucumber Campbell Groceries: almond milk coconut water cucumber Trevor Hansen Verdana Courier Calibri Arial Verdana Courier Calibri Arial Verdana Courier Calibri Arial 1 4:15 One Day 1 4:15 One Day 1 4:15 One Day DISABLED DISABLED Subtitle here Title goes here ACTION 2 ACTION 1 Subtitle here Title goes here ACTION 2 ACTION 1 ACTION Single-line snackbar 60 Campbell Groceries: almond milk coconut water cucumber Campbell Groceries: almond milk coconut water cucumber Trevor Hansen Verdana Courier Calibri Arial Verdana Courier Calibri Arial Verdana Courier Calibri Arial 1 4:15 One Day 1 4:15 One Day 1 4:15 One Day DISABLED DISABLED Subtitle here Title goes here ACTION 2 ACTION 1 Single-line snackba 60 Groceries: almond m coconut w
  50. What have we learned so far: ✓ Separate content and

    style. 2003
 ✓ Create a component library. 2011
 ✓ Everything is a component. 2012 ✓ CSS conventions are extremely useful. 2013 ✗ Writing CSS and markup properly is hard.
  51. Shakespeare's Hamlet took up to 3 years to write. RSCSS:

    Reasonable System for CSS Stylesheet Structure 2015 http://rscss.io
  52. Remember this example component? UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN

    UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU
  53. RSCSS class names are still "semantic" style. UIIUIUIN UIIUNIUN UIUNUN

    INU UINUIU <div class='image'>...</div> <div class='heading'>...</div> <div class='description'>...</div> <div class='actions'>...</div> <div class='photo-card'> </div>
  54. The CSS structure mirrors the HTML structure. UIIUIUIN UIIUNIUN UIUNUN

    INU UINUIU <div class='image'> <div class='heading'> <div class='description'> <div class='actions'> <div class='photo-card'> </div> > .image { ... } > .heading { ... } > .description { ... } > .actions { ... } .photo-card { }
  55. <div class='image'> <div class='heading'> <div class='description'> <div class='actions'> <div class='photo-card'>

    </div> The secret is in the direct child selector (>). UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU UIIUIUIN UIIUNIUN UIUNUN INU UINUIU .photo-card { > .image { ... } > .actions > .button { ... } } RS CSS w it h SAS S
  56. If it needs to be more than one word, don't

    use underscores or hyphens. .button Elements are always one word. Rule No. 2—
  57. This is a good idea, let me explain later. >

    Use the child descendant selector. Rule No. 4—
  58. .photo-card Components .image > .-large Elements Variants This way, we

    remove ambiguity on the meaning of class names. .search-box .field > .-selected .audio-controls .button > .-play
  59. That component is part of a bigger component. <div class="callout-box">

    <div class="image"> <div class="text"> <h2 class="heading">...</h2> <div class="button-box">
  60. That component has more elements. <div class="callout-box"> <div class="image"> <div

    class="text"> <h2 class="heading">...</h2> <div class="button-box">
  61. The CSS mirrors the HTML structure. .callout-box { > .image

    { ... } > .text { ... } > .text > .heading { ... } }
  62. Organize your components per file. .callout-box { > .image {

    ... } > .text { ... } > .text > .heading { ... } } callout-box.scss • • • One component per CSS file. Rule No. 5—
  63. .callout-box { > .image { ... } > .text {

    ... } > .text > .heading { ... } } callout-box.scss • • • This makes it easy to include them all. One component per CSS file. Rule No. 5— @import 'components/*'; application.scss • • •
  64. One of the first books that talked about CSS positioning.

    Web Components and Shadow DOM w3.org www.w3.org/wiki/WebComponents/
  65. <div class="warning"> <style> h3 { color: red !important; } </style>

    <h3>Warning!</h3> <p>Don't try this at home.</p> </div> <link rel="import" href="warning.html"> People started separating content from style. warning.html index.html
  66. There's a proposal to make CSS components easier. CSS @scope

    Proposal w3.org www.w3.org/TR/css-scoping-1/
  67. @scope .user-profile { ... } CSS scoping lets you write

    CSS without components bleeding into other components.
  68. Always think in components. @rstacruz Rico Sta. Cruz ricostacruz.com Subtitle

    here Title goes here ACTION 2 ACTION 1 ACTION Single-line snackbar 60 Groceries: almond milk coconut water cucumber Campbell Groceries: almond milk coconut water cucumber Trevor Hansen Arial Courier Calibri Arial Verdana Courier Calibri Arial 1 4:15 One Day DISABLED DISABLED Subtitle here Title goes here ACTION 2 ACTION 1 ACTION Single-line snackbar 60 Campbell Groceries: almond milk coconut water cucumber Campbell Groceries: almond milk coconut water cucumber Trevor Hansen Arial Verdana Courier Calibri Arial Verdana Courier Calibri Arial 1 4:15 One Day DISABLED DISABLED