Share
Embed
CSS Architecture with OOCSS, SMACSS, BEM
Slide about Modern CSS Architecture.
with OOCSS, SMACSS, BEM and Preprocessors.
Katsunori Tanaka
March 09, 2014
Transcript
83 slides
-
OUTLINE 1. CSS Architecture 2. CSS Methodology 3. CSS Preprocessor
4. Semantics 5. Summary -
“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 -
“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 -
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/ -
The Goals of Good CSS Architecture ❖ Predictable ❖ Reusable
❖ Maintainable ❖ Scalable http://philipwalton.com/articles/css-architecture/ -
“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/ -
Two Main Principles ✤ Separate Structure and Skin ✤ Separete
Container and Content -
“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 -
“break the dependency between the container module and the content
objects it contains.” ʮ ίϯςφʔϞδϡʔϧͱͦͷதͰ͋Δίϯ ςϯπͱͷґଘؔΛஅͪΔʯ Separate Container and Content -
ςΩετ <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 { 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; } -
ςΩετ <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 -
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) -
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/ -
Three Main Principles ✤ Categorizing CSS Rules ✤ Naming Rules
✤ Minimizing the Depth of Applicability -
Five Types of Categories 1. Base 2. Layout 3. Module
4. State 5. Theme -
Layout Rules ( Major Components) ❖ɹϖʔδͷओཁͳϨΠΞτΛ࢘Δίϯϙʔωϯτ ❖ɹίϯςφʔͱͯ͠ޙड़͢ΔϞδϡʔϧΛؚΉ ❖ɹΫϥεηϨΫλɺIDηϨΫλͰఆٛ ❖ɹΫϥεͷ໋໊نଇɺl-ɹlayout- ❖ɹ.l-header
❖ɹ.l-sidebar ❖ɹ.l-content ❖ɹ.l-footer http://smacss.com/book/type-layout -
Module Rules ( Minor Components) ❖ɹઌड़ͷϝδϟʔίϯϙʔωϯτʹஔ ❖ɹݸʑͷϞδϡʔϧελϯυΞϩϯͳίϯϙʔωϯτ ͱͯ͠ଘࡏͰ͖ΔΑ͏ʹઃܭʢreusableʣ ❖ɹΫϥεηϨΫλͷΈͰఆٛɻ ❖ɹαϒΫϥεϋΠϑϯͰͭͳ͛Δʢ໋໊نଇʣ
❖ɹ.media ❖ɹ.media-image ❖ɹ.media-image-hoge http://smacss.com/book/type-module -
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) -
State Rules ❖ɹLayout, Moduleͷ྆ํʹద༻͞ΕΔ ❖ɹJavaScriptʹґଘ͢Δ ❖ɹaddClass, removeClass, toggleClass ❖ɹΫϥεηϨΫλͷΈͰఆٛɻ ❖ɹΫϥεͷ໋໊نଇɺis-
❖ɹis-active ❖ɹis-tab-active ❖ɹis-hidden ❖ɹis-media-hidden http://smacss.com/book/type-state -
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; } -
ςΩετ <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) -
Minimizing the Depth of Applicability ❖ɹదԠੑͷਂΛ࠷খݶʹͱͲΊΔ ❖ɹਂͷਂ͍ࢠଙηϨΫλ༻͠ͳ͍ ❖ɹίϯςϯπґଘΛࢭ͢Δҝ ❖ɹϞδϡʔϧͰࢠηϨΫλʹͱͲΊΔ ❖ɹཧࢠηϨΫλͷΘΓʹαϒΫϥεͰఆٛ
http://smacss.com/book/applicability -
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; } -
ςΩετ <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 -
Two core goals of SMACSS 1. HTMLͱίϯςϯπͷηϚϯςΟοΫͳՁΛ ্͢Δ͜ͱʢదͳ໋໊نଇʹج͍ͮͨΫϥε ໊ʹΑΔίϯςϯπͷৄࡉઆ໌ɻʣ 2.
ಛఆͷHTMLߏͷґଘΛݮ͢Δ͜ͱ http://smacss.com/book/html5 -
Modifier ❖ɹBlock, Elementͷݟӫ͑ৼ͍Λࢦఆ ❖ɹΫϥεηϨΫλͷΈͰఆٛɻ ❖ɹαϒΫϥεΞϯμʔείΞҰͭͰͭͳ͛Δ ❖ɹ.block_modifier ❖ɹ.block__element_modifier ❖ɹSMACSSʹ͓͚ΔSub Class (Modifier),
State Rule http://bem.info/method/definitions/ -
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 -
.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); } -
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 -
CSS Preprocessor for OOCSS ✤ “&” Combinator (LESS & SASS3.3~)
✤ Placeholder Selector (SASS) ✤ Combine multiples files -
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; } } -
.btn { border: 1px solid #000; padding: 10px 20px; color:
#000; border-radius: 8px; } .btn--red { background-color: red; } .btn--blue { background-color: blue; } -
Multiple Classes Multiple Classes ςΩετ <a class="btn btn--red" href="#" >രഁ</a>
<a class="btn btn--blue" href="#" >ΊΔ</a> -
Single Classes Single Class ςΩετ <a class="fire" href="#" >രഁ</a> <a
class="stop" href="#" >ΊΔ</a> -
%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; } -
.fire, .stop { border: 1px solid #000; padding: 10px 20px;
color: #000; border-radius: 8px; } .fire { background-color: red; } .stop { background-color: blue; } -
Placeholder Selector ✤ Clean markup - single class ✤ Semantic
class naming ✤ Avoid using too much @extend -
“ ... not all semantics need to be content- derived
” ʮશͯͷηϚϯςΟΫε͕ίϯςϯπ༝དྷͰ͋ Δඞཁͳ͍ʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ -
“ Content-layer semantics are already served by HTML elements and
other attributes. ” ʮίϯςϯπϨΠϠʔͷηϚϯςΟΫεɺ HTMLཁૉͱͦͷଞଐੑʹΑͬͯɺ͢Ͱʹ༩͑ ΒΕ͍ͯΔʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ -
“ Class names impart little or no useful semantic information
to machines or human visitors... ” ʮΫϥε໊ɺαΠτӾཡऀϚγʔϯʹͱͬ ͯຆͲӨڹΛ༩͑Δ͜ͱͳ͘ɺ༗ӹͳใΛ ͨΒ͢͜ͱͳ͍ʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ -
“ The primary purpose of a class name is to
be a hook for CSS and JavaScript. ” ʮΫϥε໊ͷୈҰͷతɺCSSJavaScriptͷ ϑοΫʹͳΔ͜ͱɻʯ http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ -
“ Class names should communicate useful information to developers. ”
http://nicolasgallagher.com/about-html-semantics-front-end-architecture/ ʮΫϥε໊։ൃऀ༗ӹͳใΛୡ͖͢ɻʯ -
“ 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πϦʔͷ͋Δ෦ʹґଘ͢Δ͜ͱɺ ಛఆͷཁૉλΠϓΛ༻ͨ͠Γ͠ͳ͍ɻʯ -
The Goals of Good CSS Architecture ❖ Predictable ❖ Reusable
❖ Maintainable ❖ Scalable http://philipwalton.com/articles/css-architecture/ -
Do you need a CSS Architecture & Methodology like OOCSS
? ❖ How many developers ? ❖ Reuse ? ❖ Maintainance ? ❖ Scale ? http://philipwalton.com/articles/css-architecture/