5. 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
8. 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.
14. !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
15. !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
29. 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
31. 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; }
36. 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
37. Block on the filesystem
blocks/
input/ # input block directory
button/ # button block directory
40. 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
41. 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.
42. A block or an element: when should I use which?
52. When I can use a few selectors in one declaration?
.theme-dark-sky .button { … }
53. 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