SlideShare ist ein Scribd-Unternehmen logo
1 von 83
Downloaden Sie, um offline zu lesen
CSS
Architecture jsCafe
2014-03-09
KatsunoriTanaka
with OOCSS,SMACSS,BEM
OUTLINE
1. CSS Architecture
2. CSS Methodology
3. CSS Preprocessor
4. Semantics
5. Summary
1. CSS Architecture
What?
CSS Architecture
“コンピュータ(特にハードウェア)
における基本設計や設計思想などの
基本設計概念を意味する。”
https://ja.wikipedia.org/wiki/コンピュータ・アーキテクチャ
-wikipedia
Why?
CSS Architecture
“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/
“CODE ISTOO FRAGILE.”
http://www.stubbornella.org/content/2009/02/12/css-doesn’t-suck-you’re-just-doing-it-wrong/
-Nicole Sullivan
「コード(注:ここではCSS)もろすぎ」
CSS Architecture
http://philipwalton.com/articles/css-architecture/
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/
2. CSS Methodology
CSS Methodology
✤ OOCSS
✤ SMACSS
✤ BEM
Object
Oriented
CSS
OOCSS Nicole Sullivan
https://github.com/stubbornella/oocss/wiki
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
EXMAPLE
MEDIA OBJECT
テキスト
<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/
❖ Add classes
❖ Don’t use descendent selectors
Two Practices
OOCSS
Scalable &
Modular
Architecture for
CSS
SMACSS
SMACSS Jonathan Snook
http://smacss.com
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
Base Rules
http://yuilibrary.com/yui/docs/cssreset/
http://necolas.github.io/normalize.css/
❖ ベースとなるルールセットを定義。
❖ タイプセレクタを用いる。
❖ クラスセレクタ、IDセレクタは用いない。
❖ CSS Reset
❖ Normalize CSS
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)
Theme Rules
❖ ページ全体のテーマ変更用のルール
❖ クラスセレクタのみで定義
❖ クラスの命名規則は、theme-
❖ theme-a-background
❖ theme-a-border
❖ theme-b-background
❖ theme-b-border
http://smacss.com/book/type-theme
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
Block
Element
Modifier
BEM
BEM Yandex
http://bem.info/
BEM stands for
✤ Block
✤ Element
✤ Modifier
http://bem.info/method/definitions/
Block
❖ コンテンツから独立した存在
❖ ページやアプリケーションを構成している
「レゴブロック」のような役割
❖ クラスセレクタのみで定義。
❖ SMACSSにおけるModule Class
http://bem.info/method/definitions/
Element
❖ ブロックを構成する一部分
❖ エレメントが属するブロックの中でのみ意味
をなす、ブロックに依存した存在
❖ クラスセレクタのみで定義。
❖ サブクラスはアンダースコア二つでつなげる
❖ .block__element
❖ SMACSSにおけるSub Class (descendent)
http://bem.info/method/definitions/
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);
}
MindBEMDing
http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/
MindBEMDing
http://nicolasgallagher.com/about-html-semantics-front-end-architecture/
❖ BEM命名規則の改良版(by Nicolas Gallagher)
❖ ダブルセパレータ
❖ .block__element
❖ .block−−modifier
❖ .block__element−−modifier
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
3. CSS Preprocessor
for OOCSS
CSS Preprocessor
for OOCSS
✤ “&” Combinator (LESS & SASS3.3~)
✤ Placeholder Selector (SASS)
✤ Combine multiples files
テキスト
<a class="btn btn--red" href="#" >爆破</a>
<a class="btn btn--blue" href="#" >やめる</a>
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;
}
Single
Class
OOSCSS
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
4. Semantics
About HTML semantics and
front-end architecture
http://nicolasgallagher.com/about-html-semantics-front-end-architecture/
“ ... 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. ”
「クラス名の第一の目的は、CSSやJavaScriptの
フックになること。」
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ツリーのある部分に依存することも、
特定の要素タイプを使用したりもしない。」
CSS Class
Naming Convention
✤ Semantic ?
✤ presentational / behavioural ?
5. Summary
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/
Bootstrap
http://getbootstrap.com/
Topcoat
http://topcoat.io/
Thank you
so much jsCafe
2014-03-09
KatsunoriTanaka
ご静聴ありがとうございました

Weitere ähnliche Inhalte

Andere mochten auch

Design Pattern Craftsmanship
Design Pattern CraftsmanshipDesign Pattern Craftsmanship
Design Pattern CraftsmanshipJason Beaird
 
Writing Scalable and Maintainable CSS
Writing Scalable and Maintainable CSSWriting Scalable and Maintainable CSS
Writing Scalable and Maintainable CSSDmitry Sheiko
 
Smacss e-css-faz-bem
Smacss e-css-faz-bemSmacss e-css-faz-bem
Smacss e-css-faz-bemJust Digital
 
CSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreCSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreRuss Weakley
 
OOCSS and SMACSS Case Study
OOCSS and SMACSS Case StudyOOCSS and SMACSS Case Study
OOCSS and SMACSS Case StudyKota Fujishiro
 
Style Guides Are The New Photoshop (Fronteers 2012)
Style Guides Are The New Photoshop (Fronteers 2012)Style Guides Are The New Photoshop (Fronteers 2012)
Style Guides Are The New Photoshop (Fronteers 2012)Stephen Hay
 
OOCSS, SMACSS or BEM, what is the question...
OOCSS, SMACSS or BEM, what is the question...OOCSS, SMACSS or BEM, what is the question...
OOCSS, SMACSS or BEM, what is the question...Michael Posso
 

Andere mochten auch (9)

Design Pattern Craftsmanship
Design Pattern CraftsmanshipDesign Pattern Craftsmanship
Design Pattern Craftsmanship
 
Writing Scalable and Maintainable CSS
Writing Scalable and Maintainable CSSWriting Scalable and Maintainable CSS
Writing Scalable and Maintainable CSS
 
Smacss e-css-faz-bem
Smacss e-css-faz-bemSmacss e-css-faz-bem
Smacss e-css-faz-bem
 
HTML CSS & Javascript
HTML CSS & JavascriptHTML CSS & Javascript
HTML CSS & Javascript
 
CSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreCSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and more
 
OOCSS and SMACSS Case Study
OOCSS and SMACSS Case StudyOOCSS and SMACSS Case Study
OOCSS and SMACSS Case Study
 
Style Guides Are The New Photoshop (Fronteers 2012)
Style Guides Are The New Photoshop (Fronteers 2012)Style Guides Are The New Photoshop (Fronteers 2012)
Style Guides Are The New Photoshop (Fronteers 2012)
 
OOCSS, SMACSS or BEM, what is the question...
OOCSS, SMACSS or BEM, what is the question...OOCSS, SMACSS or BEM, what is the question...
OOCSS, SMACSS or BEM, what is the question...
 
Atomic design
Atomic designAtomic design
Atomic design
 

CSS Architecture with OOCSS, SMACSS, BEM