css architecture with oocss, smacss, bem

Post on 11-Aug-2014

4.773 Views

Category:

Design

23 Downloads

Preview:

Click to see full reader

DESCRIPTION

Slide about Modern CSS Architecture. with OOCSS, SMACSS, BEM and Preprocessors. If you want to download PDF but don't want to login SlideShare, go to Speacker Deck on which is the same downloadable PDF : https://speakerdeck.com/katsunoritanaka/css-architecture-with-oocss-smacss-bem

TRANSCRIPT

CSSArchitecture

jsCafe 2014-03-09Katsunori Tanaka

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 PracticeMyths

❖ 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 IS TOO FRAGILE.”

http://www.stubbornella.org/content/2009/02/12/css-doesn’t-suck-you’re-just-doing-it-wrong/

-Nicole Sullivan

「コード(注:ここではCSS)もろすぎ」

CSS Architecturehttp://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 OrientedCSS

OOCSS Nicole Sullivanhttps://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 ClassSub Class(modifier)

Three Best PracticeMyths

❖ 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 PracticesOOCSS

Scalable &ModularArchitecture forCSS

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 ClassSub 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

BlockElementModifier

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://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 Preprocessorfor OOCSS

CSS Preprocessorfor 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;}

SingleClassOOSCSS

Multiple ClassesMultiple Classes

テキスト<a class="btn btn--red" href="#" >爆破</a>

<a class="btn btn--blue" href="#" >やめる</a>

Single ClassesSingle 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 architecturehttp://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

Bootstraphttp://getbootstrap.com/

Topcoathttp://topcoat.io/

Thank youso much

jsCafe 2014-03-09Katsunori Tanaka

ご静聴ありがとうございました

top related