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

CSS Architecture with OOCSS, SMACSS, BEM

CSS Architecture with OOCSS, SMACSS, BEM

Slide about Modern CSS Architecture.
with OOCSS, SMACSS, BEM and Preprocessors.

Katsunori Tanaka

March 09, 2014
Tweet

More Decks by Katsunori Tanaka

Other Decks in Programming

Transcript

  1. “CSS is simple... It’s simple to understand. But CSS is

    not simple to use or maintain.” ʮCSS͸୯७Ͱཧղ͠΍͍͕͢ɺ࢖ͬͨΓϝϯς ͨ͠Γ͢Δͷ͸γϯϓϧͰ͸ͳ͍ɻʯ http://chriseppstein.github.io/blog/2009/09/20/why-stylesheet-abstraction-matters/ -Chris Eppstein
  2. “We have been doing it all wrong.... Our (CSS) best

    practices are killing us” ʮCSSͷϕετϓϥΫςΟεʹΑͬͯࢲୡ͸μϝ ʹ͞Ε͍ͯΔɻʯ http://www.stubbornella.org/content/2011/04/28/our-best-practices-are-killing-us/ -Nicole Sullivan
  3. Three Best Practice Myths ❖ Don’t add any extra elements

    ❖ Don’t add classes ❖ Use descendent selectors exclusively http://www.stubbornella.org/content/2011/04/28/our-best-practices-are-killing-us/
  4. The Goals of Good CSS Architecture ❖ Predictable ❖ Reusable

    ❖ Maintainable ❖ Scalable http://philipwalton.com/articles/css-architecture/
  5. “CSS isn't just visual design. Don't throw out programming best

    practices just because you're writing CSS. Concepts like OOP, DRY, the open/closed principle, separation of concerns, etc. still apply to CSS.” ʮΦϒδΣΫτࢤ޲ɺDRYɺ։์/ด࠯ݪଇɺؔ ৺ͷ෼཭ͳͲͷ֓೦͸ɺCSSʹ΋ͳ͓ద༻͞Ε Δɻʯ http://philipwalton.com/articles/css-architecture/
  6. “abstract the structure of the block from skin which is

    being applied. Class can be extended by adding additional classes to the block element.” ʮεΩϯ͔ΒϒϩοΫߏ଄Λநग़͢Δɻ ϒϩοΫཁૉ΁ผͷΫϥεΛ෇͚Ճ͑ΔࣄͰ Ϋϥε͸֦ுͰ͖Δɻʯ Separate Structure and Skin
  7. “break the dependency between the container module and the content

    objects it contains.” ʮ ίϯςφʔϞδϡʔϧͱͦͷத਎Ͱ͋Δίϯ ςϯπͱͷґଘؔ܎Λஅͪ੾Δʯ Separate Container and Content
  8. ςΩετ <div class="media"> <div class="media-img"> <img src="http://lorempixel.com/100/100/" /> </div> <div

    class="media-body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div>
  9. .media-shadow { box-shadow: 0 1px 5px rgba(0,0,0,0.75); } .media {

    overflow: hidden; _overflow: visible; zoom: 1; } .media-img { float: left; margin-right: 10px; } .media-img > img { display: block; margin: 10px; } .media-body { overflow: hidden; }
  10. ςΩετ <div class="media "> <div class="media-img"> <img src="http://lorempixel.com/100/100/" /> </div>

    <div class="media-body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> media-shadow
  11. Sub Class (descendent) Sub Class (descendent) ςΩετ <div class="media media-shadow">

    <div class="media-img"> <img src="http://lorempixel.com/100/100/" /> </div> <div class="media-body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> Base Class Sub Class (modifier)
  12. Three Best Practice Myths ❖ Don’t add any extra elements

    ❖ Don’t add classes ❖ Use descendent selectors exclusively http://www.stubbornella.org/content/2011/04/28/our-best-practices-are-killing-us/
  13. Three Main Principles ✤ Categorizing CSS Rules ✤ Naming Rules

    ✤ Minimizing the Depth of Applicability
  14. Sub Class (descendent) Sub Class (descendent) ςΩετ <div class="media media-shadow">

    <div class="media-img"> <img src="http://lorempixel.com/100/100/" /> </div> <div class="media-body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> Module Class Sub Class (modifier)
  15. State Class (modifier) .media { overflow: hidden; _overflow: visible; zoom:

    1; } .media-img { float: left; margin-right: 10px; } .media-img > img { display: block; margin: 10px; } .media-body { overflow: hidden; } .is-hidden { display: none; }
  16. ςΩετ <div class="media media-shadow is-hidden"> <div class="media-img"> <img src="http://lorempixel.com/100/100/" />

    </div> <div class="media-body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> State Class (modifier)
  17. Child Selector .media { overflow: hidden; _overflow: visible; zoom: 1;

    } .media-img { float: left; margin-right: 10px; } .media-img > img { display: block; margin: 10px; } .media-body { overflow: hidden; } .is-hidden { display: none; }
  18. ςΩετ <div class="media skin-shadow is-hidden"> <div class="media-img"> <img src="http://lorempixel.com/100/100/" />

    </div> <div class="media-body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> .media-img > img
  19. Element Element ςΩετ <div class="media media_shadow"> <div class="media__img"> <img src="http://lorempixel.com/100/100/"

    /> </div> <div class="media__body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> Block Modifier
  20. .media { overflow: hidden; _overflow: visible; zoom: 1; } .media__img

    { float: left; margin-right: 10px; } .media__img > img { display: block; margin: 10px; } .media__body { overflow: hidden; } .media_shadow { box-shadow: 0 1px 5px rgba(0,0,0,0.75); }
  21. Element Element ςΩετ <div class="media media--shadow"> <div class="media__img"> <img src="http://lorempixel.com/100/100/"

    /> </div> <div class="media__body"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> </div> </div> Block Modifier
  22. CSS Preprocessor for OOCSS ✤ “&” Combinator (LESS & SASS3.3~)

    ✤ Placeholder Selector (SASS) ✤ Combine multiples files
  23. 3.3~ // btn component .btn { border: 1px solid #000;

    padding: 10px 20px; color: #000; border-radius: 8px; &--red { background-color: red; } &--blue { background-color: blue; } }
  24. .btn { border: 1px solid #000; padding: 10px 20px; color:

    #000; border-radius: 8px; } .btn--red { background-color: red; } .btn--blue { background-color: blue; }
  25. %btn { border: 1px solid #000; padding: 10px 20px; color:

    #000; border-radius: 8px; } %btn--red { background-color: red; } %btn--blue { background-color: blue; } .fire { @extend %btn; @extend %btn--red; } .stop { @extend %btn; @extend %btn--blue; }
  26. .fire, .stop { border: 1px solid #000; padding: 10px 20px;

    color: #000; border-radius: 8px; } .fire { background-color: red; } .stop { background-color: blue; }
  27. Placeholder Selector ✤ Clean markup - single class ✤ Semantic

    class naming ✤ Avoid using too much @extend
  28. “ ... not all semantics need to be content- derived

    ” ʮશͯͷηϚϯςΟΫε͕ίϯςϯπ༝དྷͰ͋ Δඞཁ͸ͳ͍ʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/
  29. “ Content-layer semantics are already served by HTML elements and

    other attributes. ” ʮίϯςϯπϨΠϠʔͷηϚϯςΟΫε͸ɺ HTMLཁૉͱͦͷଞଐੑʹΑͬͯɺ͢Ͱʹ༩͑ ΒΕ͍ͯΔʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/
  30. “ Class names impart little or no useful semantic information

    to machines or human visitors... ” ʮΫϥε໊͸ɺαΠτӾཡऀ΍Ϛγʔϯʹͱͬ ͯຆͲӨڹΛ༩͑Δ͜ͱ͸ͳ͘ɺ༗ӹͳ৘ใΛ ΋ͨΒ͢͜ͱ͸ͳ͍ʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/
  31. “ The primary purpose of a class name is to

    be a hook for CSS and JavaScript. ” ʮΫϥε໊ͷୈҰͷ໨త͸ɺCSS΍JavaScriptͷ ϑοΫʹͳΔ͜ͱɻʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/
  32. “ Class names should communicate useful information to developers. ”

    http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ ʮΫϥε໊͸։ൃऀ΁༗ӹͳ৘ใΛ఻ୡ͢΂͖ɻʯ
  33. “ A flexible and reusable component is one which neither

    relies on existing within a certain part of the DOM tree, nor requires the use of specific element types. ” http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ ʮϑϨΩγϒϧͰ࠷ར༻Մೳͳίϯϙʔωϯτ ͸ɺDOMπϦʔͷ͋Δ෦෼ʹґଘ͢Δ͜ͱ΋ɺ ಛఆͷཁૉλΠϓΛ࢖༻ͨ͠Γ΋͠ͳ͍ɻʯ
  34. The Goals of Good CSS Architecture ❖ Predictable ❖ Reusable

    ❖ Maintainable ❖ Scalable http://philipwalton.com/articles/css-architecture/
  35. Do you need a CSS Architecture & Methodology like OOCSS

    ? ❖ How many developers ? ❖ Reuse ? ❖ Maintainance ? ❖ Scale ? http://philipwalton.com/articles/css-architecture/