SlideShare uma empresa Scribd logo
1 de 54
Baixar para ler offline
VietnamThailandSingaporePhilippinesMalaysiaIndonesia Russia
Changing styles quickly, without fear and pain
CSS
IS
AWESOME
Specificity
1
2
3
4
style="color: red"
#content
.link, link:active, link[href]
body, body::after
Specificity weights
inline IDs
classes
pseudo-classes
attributes
elements
pseudo-elements
Most
specificity value
Least
specificity value
, , ,
In other words
1
2
3
4
1,0,0,0 — If the element has inline styling, that automatically wins
0,1,0,0 — For each ID value
0,0,1,0 — For each class, pseudo-class, attribute selector
0,0,0,1 — For each element reference
Sample calculations
0
inline
1
IDs
2
classes
3
elements
#sidebar ul li a.myclass:hover
, , ,
Combining
.item {
color: blue;
}
0
inline
0
IDs
1
classes
0
elements
, , ,
ul li li li li li li
li li li li li li li {
color: red;
}
0
inline
0
IDs
0
classes
14
elements
, , ,
>
Important Notes
1
2
3
The universal selector (*) has no specificity value (0,0,0,0)
Pseudo-elements (e.g. :first-line) get 0,0,0,1 unlike their
psuedo-class brethren which get 0,0,1,0
The pseudo-class :not() adds no specificity by itself, only
what's inside it's parentheses.
Same specificity
<p class="message error”>
Broken!
</p>
HTML CSS
.error {
color: red;
}
.message {
color: green;
}
Example
#content table {}
/**
* Uh oh! My styles get overwritten by `#content table {}`.
*/
.my-new-table {}
Example
#content table {}
#content .my-new-table {}
Enter header
!important
!important
0
inline
1
IDs
3
classes
1
elements
, , ,
#sidebar .weatherMod .hourly .tuesday h3 {
color: blue !important;
}
0 0 0 0
, , ,
!important
normal
!important
Consequences
1
2
3
one more level of complexity
specificity grows even faster
finding out what a rule will be applied still is not easy
4 eventually, working with such code comes to nearly impossible
!important
Specificity can, among other things:
1
2
3
limit your ability to extend and manipulate a codebase
interrupt and undo CSS cascading, inheriting nature
cause avoidable verbosity in your project
4
prevent things from working as expected when moved into
different environments
5 lead to serious developer frustration
Nature of CSS
http://www.thefreedictionary.com
“methodology – the system of methods and
principles used in a particular discipline”
Methodology
BEM
BEM
Block
Element
Modifier
BEM block
Block is a logically and functionally independent page
component, the equivalent of a component in Web
Components.
BLOCK
BEM blocks
Nested structure
Arbitrary placement
Re-use
BEM element
A constituent part of a block that can’t be used and does
not make sense outside the block.
ELEMENT
BEM element
BEM modifier
A BEM entity that defines the appearance and behavior of
a block or an element.
MODIFIER
BEM modifier
BEM naming conventions
1
2
3
Names of BEM entities are written using numbers
and lowercase Latin characters
Individual words within names are separated by a hyphen (-)
Information about the names of blocks, elements,
and modifiers is stored using CSS classes
BEM block name
HTML
CSS
EXAMPLE menu, lang-switcher
<div class="menu">...</div>
.menu { color: red; }
BEM element name
HTML
CSS
EXAMPLE menu__item, lang-switcher__lang-icon
<div class=“menu">
<span class=“menu__item"></span>
</div>
SCHEME block-name__elem-name
.menu__item { color: red; }
BEM modifier name
KEY-VALUE
BOOLEAN .owner-name_mod-name
.owner-name_mod-name_mod-val
BEM block modifier
KEY-VALUE
BOOLEAN .menu_hidden
.menu_theme_morning-forest
BEM element modifier
KEY-VALUE
BOOLEAN .menu__item_visible
.menu__item_type_radio
Alternative naming schemes
block-name__elem-name-mod-name
MyBlock__SomeElem_modName_modVal
blockName-elemName-modName-modVal
File system organization
1
2
3
A block implementation is divided into separate parts
Optional elements and modifiers are stored in separate files
Files are grouped by meaning and not by type
4 A project is divided into redefinition levels
Block on the filesystem
blocks/
input/ # input block directory
button/ # button block directory
Block implementation
blocks/
input/
input.css # `input` block implementation in CSS
input.js # `input` block implementation in JavaScript
button/
button.css
button.js
button.png
How actually does BEM address the problems?
How BEM helps
The naming convention helps to:
1
2
3
avoid name conflicts
avoid the cascade and keep the specificity for all declarations
on the same low level
write self-documenting code
Enter header
BEM file organization has its benefits:
1
2
3
One block - one folder. Easy to combine in the production
build only those blocks which are really used on the page.
All-in-one. Easy to remove blocks or move themto another project.
Plain folders list helps to avoid naming conflicts. You just
cannot add a new folder if name is already taken.
A block or an element: when should I use which?
Sophisticated file organization
Mixes
<a href="/" class="link">
Some link
</a>
HTML CSS
.link {
color: blue;
}
Mixes
<nav class="menu">
<a class=“menu__item
menu__item_link”>
…
</a>
<a class=“menu__item
menu__item_link”>
…
</a>
</nav>
HTML CSS
.menu { … }
.menu__item { … }
.menu__item_link {
color: blue;
}
Mixes
<nav class="menu">
<a class=“menu__item
link”>
…
</a>
<a class=“menu__item
link”>
…
</a>
</nav>
HTML CSS
.menu { … }
.menu__item { … }
.link {
color: blue;
}
Container-content pattern
<nav class="menu">
<span class="menu__item">
<a class=“link”>…</a>
</span>
<span class="menu__item">
<a class=“link”>…</a>
</span>
</nav>
HTML CSS
.menu { … }
.menu__item { … }
.link { … }
Preprocessors mixins
.clearfix { … }
.menu__item {
.clearfix;
…
}
Element in element
<nav class="menu">
<span class="menu__item">
<a class=“menu__item__link">
…
</a>
</span>
<span class="menu__item">
<a class=“menu__item__link">
…
</a>
</span>
</nav>
WRONG RIGHT
<nav class="menu">
<span class="menu__item">
<a class=“menu__item-link”>
…
</a>
</span>
<span class="menu__item">
<a class=“menu__item-link”>
…
</a>
</span>
</nav>
Why global classes are bad?
.animate { … }
Why name prefixes are bad?
.l-grid { … }
When I can use a few selectors in one declaration?
.theme-dark-sky .button { … }
Bottom line
1
2
3
Keep specificity low at all times
Do not mix a few BEM entities on one node
Do not use global classes and prefixes
4 Keep files related to the same block in the same folder
Links
https://en.bem.info
Thank you! Questions?

Mais conteúdo relacionado

Mais procurados

Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)Muhammed Thanveer M
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)club23
 
srt311 Project2
srt311 Project2srt311 Project2
srt311 Project2trayyoo
 

Mais procurados (7)

Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)
 
Web design - Working with forms in HTML
Web design - Working with forms in HTMLWeb design - Working with forms in HTML
Web design - Working with forms in HTML
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)Html 5-tables-forms-frames (1)
Html 5-tables-forms-frames (1)
 
srt311 Project2
srt311 Project2srt311 Project2
srt311 Project2
 
The Cascade is Dead
The Cascade is DeadThe Cascade is Dead
The Cascade is Dead
 
Chromatique
ChromatiqueChromatique
Chromatique
 

Destaque

FACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIA
FACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIAFACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIA
FACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIACátedra Banco Santander
 
Entrepreneur Community Profile Jaringan Rumah Usaha
Entrepreneur Community Profile   Jaringan Rumah UsahaEntrepreneur Community Profile   Jaringan Rumah Usaha
Entrepreneur Community Profile Jaringan Rumah UsahaDIJIMEDIA
 
64 interview questions
64 interview questions64 interview questions
64 interview questionsNexus
 
19643chapter 6 aspects of indian economy
19643chapter 6 aspects of indian economy19643chapter 6 aspects of indian economy
19643chapter 6 aspects of indian economySalman Kharal
 
Nebosh iDiploma Cert A
Nebosh iDiploma Cert ANebosh iDiploma Cert A
Nebosh iDiploma Cert AJahswill Efe
 
Pelatihan Sentuhan Spiritual Quantum Spiritual Qalbu
Pelatihan Sentuhan Spiritual Quantum Spiritual QalbuPelatihan Sentuhan Spiritual Quantum Spiritual Qalbu
Pelatihan Sentuhan Spiritual Quantum Spiritual QalbuHendry Risjawan
 
Antibioticos en el embarazo
Antibioticos en el embarazoAntibioticos en el embarazo
Antibioticos en el embarazoJRUIZ RUIZ
 
Creando Productos SaaS
Creando Productos SaaSCreando Productos SaaS
Creando Productos SaaSAsier Marqués
 

Destaque (11)

FACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIA
FACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIAFACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIA
FACEBOOK: EXPERIENCIA COMO HERRAMIENTA DE APOYO PARA LA DOCENCIA
 
Entrepreneur Community Profile Jaringan Rumah Usaha
Entrepreneur Community Profile   Jaringan Rumah UsahaEntrepreneur Community Profile   Jaringan Rumah Usaha
Entrepreneur Community Profile Jaringan Rumah Usaha
 
LCM Planner
LCM PlannerLCM Planner
LCM Planner
 
64 interview questions
64 interview questions64 interview questions
64 interview questions
 
19643chapter 6 aspects of indian economy
19643chapter 6 aspects of indian economy19643chapter 6 aspects of indian economy
19643chapter 6 aspects of indian economy
 
Nebosh iDiploma Cert A
Nebosh iDiploma Cert ANebosh iDiploma Cert A
Nebosh iDiploma Cert A
 
Pelatihan Sentuhan Spiritual Quantum Spiritual Qalbu
Pelatihan Sentuhan Spiritual Quantum Spiritual QalbuPelatihan Sentuhan Spiritual Quantum Spiritual Qalbu
Pelatihan Sentuhan Spiritual Quantum Spiritual Qalbu
 
Antibioticos en el embarazo
Antibioticos en el embarazoAntibioticos en el embarazo
Antibioticos en el embarazo
 
Creando Productos SaaS
Creando Productos SaaSCreando Productos SaaS
Creando Productos SaaS
 
To się w ram ie nie zmieści
To się w ram ie nie zmieściTo się w ram ie nie zmieści
To się w ram ie nie zmieści
 
Bach lute suite4
Bach lute suite4Bach lute suite4
Bach lute suite4
 

Semelhante a BEM

BEM It! for Brandwatch
BEM It! for BrandwatchBEM It! for Brandwatch
BEM It! for BrandwatchMax Shirshin
 
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
 
OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?Michael Posso
 
BEM it! Introduction to BEM
BEM it! Introduction to BEMBEM it! Introduction to BEM
BEM it! Introduction to BEMVarya Stepanova
 
HTML and CSS Coding Standards
HTML and CSS Coding StandardsHTML and CSS Coding Standards
HTML and CSS Coding StandardsSaajan Maharjan
 
Architecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolArchitecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolAdriaan Venter
 
Magento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentMagento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentIvan Chepurnyi
 
Intro to CSS Selectors in Drupal
Intro to CSS Selectors in DrupalIntro to CSS Selectors in Drupal
Intro to CSS Selectors in Drupalcgmonroe
 
BEM it! Introduction to BEM methodology
BEM it! Introduction to BEM methodologyBEM it! Introduction to BEM methodology
BEM it! Introduction to BEM methodologyVarya Stepanova
 
CSS Architecture: Writing Maintainable CSS
CSS Architecture: Writing Maintainable CSSCSS Architecture: Writing Maintainable CSS
CSS Architecture: Writing Maintainable CSSAlexei Skachykhin
 
Front end workflow Presentation at Coffee@DBG by Praveen Vijayan
Front end workflow Presentation at Coffee@DBG by Praveen VijayanFront end workflow Presentation at Coffee@DBG by Praveen Vijayan
Front end workflow Presentation at Coffee@DBG by Praveen VijayanDeepu S Nath
 
Design Systems, Pattern Libraries & WordPress
Design Systems, Pattern Libraries & WordPressDesign Systems, Pattern Libraries & WordPress
Design Systems, Pattern Libraries & WordPressJesse James Arnold
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS PresentationShawn Calvert
 
Rock Solid CSS Architecture
Rock Solid CSS ArchitectureRock Solid CSS Architecture
Rock Solid CSS ArchitectureJohn Need
 

Semelhante a BEM (20)

BEM - CSS, Seriously
BEM - CSS, SeriouslyBEM - CSS, Seriously
BEM - CSS, Seriously
 
BEM It! for Brandwatch
BEM It! for BrandwatchBEM It! for Brandwatch
BEM It! for Brandwatch
 
BEM methodology overview
BEM methodology overviewBEM methodology overview
BEM methodology overview
 
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...
 
OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
 
BEM it! Introduction to BEM
BEM it! Introduction to BEMBEM it! Introduction to BEM
BEM it! Introduction to BEM
 
CSC PPT 4.pptx
CSC PPT 4.pptxCSC PPT 4.pptx
CSC PPT 4.pptx
 
HTML and CSS Coding Standards
HTML and CSS Coding StandardsHTML and CSS Coding Standards
HTML and CSS Coding Standards
 
Architecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling ToolArchitecture Specification - Visual Modeling Tool
Architecture Specification - Visual Modeling Tool
 
Magento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module developmentMagento 2.0: Prepare yourself for a new way of module development
Magento 2.0: Prepare yourself for a new way of module development
 
Intro to CSS Selectors in Drupal
Intro to CSS Selectors in DrupalIntro to CSS Selectors in Drupal
Intro to CSS Selectors in Drupal
 
BEM it! Introduction to BEM methodology
BEM it! Introduction to BEM methodologyBEM it! Introduction to BEM methodology
BEM it! Introduction to BEM methodology
 
CSS Architecture: Writing Maintainable CSS
CSS Architecture: Writing Maintainable CSSCSS Architecture: Writing Maintainable CSS
CSS Architecture: Writing Maintainable CSS
 
BEM it!
BEM it!BEM it!
BEM it!
 
Front end workflow Presentation at Coffee@DBG by Praveen Vijayan
Front end workflow Presentation at Coffee@DBG by Praveen VijayanFront end workflow Presentation at Coffee@DBG by Praveen Vijayan
Front end workflow Presentation at Coffee@DBG by Praveen Vijayan
 
Design Systems, Pattern Libraries & WordPress
Design Systems, Pattern Libraries & WordPressDesign Systems, Pattern Libraries & WordPress
Design Systems, Pattern Libraries & WordPress
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
Rock Solid CSS Architecture
Rock Solid CSS ArchitectureRock Solid CSS Architecture
Rock Solid CSS Architecture
 
Fewd week2 slides
Fewd week2 slidesFewd week2 slides
Fewd week2 slides
 

BEM