SlideShare a Scribd company logo
BEMについて
⼀緒に考えてみませんか
# Presented by 齋藤克弘
2019.4.14 SUN
# TECH::EXPERT # チーム開発 # 勉強会 # CSS設計
はじめに
l僕もみなさんもやりたいことは同じ。
- 最終課題をより良いものにしたい!
l 『建設的議論』のススメ | ×ケンカ
- お互いの知識を深め合うことができる。
- プロジェクトの完成度を⾼めることができる。
l僕はまだまだ未熟者
- ⾃分が絶対に正しいとは思ってません。
- だから、皆さんからのご意⾒・ダメ出し⼤歓迎!
1
I respect your code.
I respect your opinion.
I respect you.
今⽇、お伝えしたいこと
lBEMによる命名規則をできるだけ守る
- クラス名の衝突を避けられる。
- スタイルの影響範囲がわかりやすくなる。
lscssファイルは1Blockにつき、1ファイル作成
- その際、scssファイル名は“_Block名.scss”にする。
×block名: footer-about ファイル名: _footer-bottom.scss
○block名: footer-about ファイル名: _footer-about.scss
3
CSS設計って?
「CSSを記述するときのルール」
• このルールがしっかりしていないと、プロジェクトは破綻
• 複数⼈で作業するときは特に要注意
CSS設計はプロジェクトの『⼟台』
• BEMはCSS設計の1つ。
4
BEMって?
l 要素を「Block」「Element」「Modifier」の3つ分ける。
l クラス名の『厳格な命名規則』が特徴。
l 原則として、クラスセレクタを⽤いる。
Block: menu
Element: menu__item
Modifier: menu__item--current
例) mindBEMding
5
本家メルカリのCSS設計
OOCSS (Object-Oriented CSS)に近い設計
• 構造と⾒た⽬を切り離す。
• デザインに関するCSSを書いたクラス名をHTMLに与えて⾒た⽬を作る。
// 商品名
.font-2 {
font-size: 16px;
font-weight: 400;
color: #333;
}
// ログインボタン
.btn-red {
background: #ea352d;
border: 1px solid #ea352d;
color: #fff;
}
// 値段
.font-5 {
font-size: 20px;
font-weight: 600;
color: #333;
}
6
CSS設計 - 料理の⼿法みたいなもの
本家メルカリ
イタリアン料理
(OOCSSっぽいやつ)
• 本家メルカリと僕らのメルカリは違う料理⼿法(CSS設計)。
• 複数の料理⼿法を適当に混ぜると、ヘンテコな料理に。
• それぞれの⽅法論を融合させるなら、とても慎重に。
僕らのメルカリ
⽇本料理
(BEM)
7
今の問題点って?
l クラス名が被ってしまうかもしない。
.btn-sns-facebook {
margin: 16px 0 0;
background: #385185;
color: #fff;
}
.btn-sns-facebook {
margin: 14px 0 0;
display: inline-block;
…..
}
Aさん Bさん
衝突
l ⼀々、クラス名がすでに使われていないか確かめないといけない。
8
BEMの命名規則を守れば被らない
lCSSがBlock毎に独⽴しているから、変更時も影響範囲がわかりやすい。
.login__btn--facebook {
margin: 16px 0 0;
background: #385185;
color: #fff;
}
.footer__btn--facebook {
margin: 14px 0 0;
display: inline-block;
…..
}
Aさん Bさん
9
それでも残る問題点
l Block名が被ってしまうかもしない。
ここのBlock名は
”app-banner”にしよう!
ここのBlock名は
”app-banner”にしよう!
.app-banner {
width: 100%;
…..
}
.app-banner {
width: 600px:
…..
}
10
scssファイルは1Blockにつき1つ / ”_Block名.scss”
lこの2つのルールを守れば、Block名は被らない。
同名のファイルは複数保存できないから。
11
おまけ - より良いscssを書くために①
l何度も同じCSSを書かなきゃいけないときは、mixinを使う。
// _mixin.scss
@mixin banner-background($background-image) {
background: $background-image;
background-size: auto 100%;
background-position: center;
}
// _vegitable.scss
.vegitable {
&__tomate {
@include banner-background(“tomate.jpg”);
}
&__onion {
@include banner-background(“onion.jpg”);
}
&__potato {
@include banner-background(“potato.jpg”);
}
}
12
おまけ - より良いscssを書くために②
l SCSSでは、アンパサンド(&)を活⽤する。
- クラス名を変更する際、変更箇所が少なくて済む。
. vegitable {
&__tomate {
@include banner-background(“tomate.jpg”);
}
&__onion {
@include banner-background(“onion.jpg”);
}
&__potato {
@include banner-background(“potato.jpg”);
}
}
.vegitable {
.vegitable__tomate {
@include banner-background(“tomate.jpg”);
}
.vegitable__onion {
@include banner-background(“onion.jpg”);
}
.vegitable__potato {
@include banner-background(“potato.jpg”);
}
}
13
まとめ
lBEMによる命名規則をできるだけ守る
- クラス名の衝突を避けられる。
- スタイルの影響範囲がわかりやすくなる。
lscssファイルは1Blockにつき、1ファイル作成
- その際、scssファイル名は“_Block名.scss”にする。
×block名: footer-about ファイル名: _footer-bottom.scss
○block名: footer-about ファイル名: _footer-about.scss
14
参考サイト
lBEM 公式ドキュメント
- https://en.bem.info/methodology/
15
lQiita ⼀番詳しいCSS設計規則BEMのマニュアル
- https://qiita.com/Takuan_Oishii/items/0f0d2c5dc33a9b2d9cb1
lmaesblog BEMをSassで快適に書く⽅法
- https://mae.chab.in/archives/2553
lUK MILK より良いCSSを書くための様々なCSS設計まとめ
- https://uxmilk.jp/43386

More Related Content

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

BEMについて一緒に考えてみませんか