$30 off During Our Annual Pro Sale. View Details »

BEM

 BEM

An introduction to BEM and its core principles. MercadoLibre Front-end Meetup. August, 2015.

Luciano Battagliero

August 14, 2015
Tweet

More Decks by Luciano Battagliero

Other Decks in Programming

Transcript

  1. B__E_M

    View Slide

  2. Hi, there!
    I’m Luciano Battagliero
    @battaglr

    View Slide

  3. Block,
    Element,
    Modifier

    View Slide

  4. Yandex

    View Slide

  5. ~2009

    View Slide

  6. A little bit of context
    OOCSS ~2010
    SMACSS ~2011

    View Slide

  7. What is it?

    View Slide

  8. It’s a methodology

    View Slide

  9. Heavily complemented by a set
    of tools, libraries and a complete
    technology stack

    View Slide

  10. At its core BEM is an unified
    semantic for different
    implementations

    View Slide

  11. That being said, this talk will be
    focused mainly on CSS

    View Slide

  12. What does
    BEM solve?

    View Slide

  13. Chaos
    /ˈkāˌäs/
    noun
    1. Complete disorder and confusion

    View Slide

  14. There are two types of problems
    we face in CSS

    View Slide

  15. Layout or cosmetic problems

    View Slide

  16. Architectural problems

    View Slide

  17. BEM attempts to help solving
    architecture related problems

    View Slide

  18. Entity

    View Slide

  19. A generic term to refer to blocks,
    elements or modifiers

    View Slide

  20. Block

    View Slide

  21. An independent and self-sufficient
    component of an interface

    View Slide

  22. Provides structure, behaviour and
    appearance encapsulation

    View Slide


  23. HTML

    View Slide


  24. HTML

    View Slide

  25. .modal { … }
    CSS

    View Slide

  26. Blocks must be context independent,
    thus they should not have direct
    influence over other blocks

    View Slide




  27. HTML

    View Slide




  28. HTML

    View Slide

  29. /* Don’t do this */
    .modal .btn { … }
    CSS

    View Slide

  30. /* Do this */
    .modal__btn { … }
    CSS

    View Slide

  31. Blocks can contain other blocks

    View Slide




  32. HTML

    View Slide




  33. HTML

    View Slide

  34. Multiple instances of a block could
    be used across the interface

    View Slide








  35. HTML

    View Slide








  36. HTML

    View Slide








  37. HTML

    View Slide

  38. Element

    View Slide

  39. An internal part of a block that
    can not be used outside of it

    View Slide

  40. Elements can contain other
    elements or blocks

    View Slide






  41. HTML

    View Slide






  42. HTML

    View Slide






  43. HTML

    View Slide

  44. Elements should not attempt to
    be a representation of the
    DOM structure

    View Slide







  45. HTML

    View Slide







  46. HTML

    View Slide







  47. HTML

    View Slide

  48. A block may not contain any element

    View Slide

  49. Modifier

    View Slide

  50. A variation on the appearance or
    behavior of a block or an element

    View Slide


  51. HTML

    View Slide


  52. HTML

    View Slide


  53. HTML

    View Slide


  54. HTML

    View Slide


  55. HTML

    View Slide


  56. HTML

    View Slide

  57. Multiple modifiers can be used
    simultaneously on the same
    block or element

    View Slide




  58. HTML

    View Slide




  59. HTML

    View Slide




  60. HTML

    View Slide

  61. Naming
    conventions

    View Slide

  62. The main purpose of a class
    name is to be used as a hook for
    adding style or behaviour

    View Slide

  63. A class name should communicate
    information which helps to
    understand its purpose

    View Slide

  64. Remember that a class name
    can not be “unsemantic”[*]

    View Slide

  65. The “official” syntax

    View Slide

  66. /* Basic syntax */
    .block__element_modifier
    CSS

    View Slide

  67. /* Basic syntax */
    .block__element_modifier
    CSS

    View Slide

  68. /* Basic syntax */
    .block__element_modifier
    CSS

    View Slide

  69. /* Basic syntax */
    .block__element_modifier
    CSS

    View Slide

  70. /* Key-value modifier */
    .block-or-element_modifier_value
    CSS

    View Slide

  71. /* Key-value modifier */
    .block-or-element_modifier_value
    CSS

    View Slide

  72. /* Entities with compound names */
    .block-name__element-name_modifier-name
    CSS

    View Slide

  73. /* Entities with compound names */
    .block-name__element-name_modifier-name
    CSS

    View Slide

  74. /**
    * All possible combinations
    */
    .block
    .block_modifier
    .block__element
    .block__element_modifier
    CSS

    View Slide

  75. /**
    * Don’t do any of these
    */
    .element
    .block_modifier__element
    .block__element__element
    CSS

    View Slide

  76. The “popular” syntax

    View Slide

  77. /* Basic syntax */
    .block__element--modifier
    CSS

    View Slide

  78. /* Basic syntax */
    .block__element--modifier
    CSS

    View Slide

  79. /* Basic syntax */
    .block__element--modifier
    CSS

    View Slide

  80. /* Basic syntax */
    .block__element--modifier
    CSS

    View Slide

  81. /* Entities with compound names */
    .block-name__element-name--modifier-name
    CSS

    View Slide

  82. /* Entities with compound names */
    .block-name__element-name--modifier-name
    CSS

    View Slide

  83. /**
    * All possible combinations
    */
    .block
    .block--modifier
    .block__element
    .block__element--modifier
    CSS

    View Slide

  84. /**
    * Don’t do any of these
    */
    .element
    .block--modifier__element
    .block__element__element
    CSS

    View Slide

  85. The “CamelCase”
    syntax

    View Slide

  86. /* Basic syntax */
    .Block-element--modifier
    CSS

    View Slide

  87. /* Basic syntax */
    .Block-element--modifier
    CSS

    View Slide

  88. /* Basic syntax */
    .Block-element--modifier
    CSS

    View Slide

  89. /* Basic syntax */
    .Block-element--modifier
    CSS

    View Slide

  90. /* Entities with compound names */
    .BlockName-elementName--modifierName
    CSS

    View Slide

  91. /* Entities with compound names */
    .BlockName-elementName--modifierName
    CSS

    View Slide

  92. /**
    * All possible combinations
    */
    .Block
    .Block--modifier
    .Block-element
    .Block-element--modifier
    CSS

    View Slide

  93. /**
    * Don’t do any of these
    */
    .element
    .Block--modifier-element
    .Block-element-element
    CSS

    View Slide

  94. Syntax comparison

    View Slide

  95. /* “Official” */
    block-name__element-name_modifier-name
    /* “Popular” */
    block-name__element-name--modifier-name
    /* “CamelCase” */
    BlockName-elementName--modifierName
    CSS

    View Slide

  96. /* “Official” */
    block-name__element-name_modifier-name
    /* “Popular” */
    block-name__element-name--modifier-name
    /* “CamelCase” */
    BlockName-elementName--modifierName
    CSS

    View Slide

  97. /* “Official” */
    block-name__element-name_modifier-name
    /* “Popular” */
    block-name__element-name--modifier-name
    /* “CamelCase” */
    BlockName-elementName--modifierName
    CSS

    View Slide

  98. /* “Official” */
    block-name__element-name_modifier-name
    /* “Popular” */
    block-name__element-name--modifier-name
    /* “CamelCase” */
    BlockName-elementName--modifierName
    CSS

    View Slide

  99. It has been scientifically proven
    that BEM class names are ugly

    View Slide

  100. Mix

    View Slide

  101. A combination of different entities
    on a single DOM node

    View Slide


  102. HTML

    View Slide


  103. HTML

    View Slide

  104. Implementation
    guidelines

    View Slide

  105. Only define entities using class
    selectors

    View Slide

  106. .icon { … }
    CSS

    View Slide

  107. Use descendant selectors only
    when needed

    View Slide

  108. /* Don’t do this */
    .menu .menu__item { … }
    CSS

    View Slide

  109. /* Do this */
    .menu--horizontal .menu__item { … }
    CSS

    View Slide

  110. Do not use type or id selectors

    View Slide

  111. /* Don’t do this */
    button { … }
    CSS

    View Slide

  112. /* Do this */
    .button { … }
    CSS

    View Slide

  113. /* Don’t do this */
    #breadcrumb { … }
    CSS

    View Slide

  114. /* Do this */
    .breadcrumb { … }
    CSS

    View Slide

  115. Do not declare styles outside
    of blocks

    View Slide

  116. /* Don’t do this */
    ul {
    list-style: none;
    }
    CSS

    View Slide

  117. /* Do this */
    .menu {
    list-style: none;
    }
    CSS

    View Slide

  118. This usually means no global styles
    and no global “resets”

    View Slide

  119. That’s just a
    part of it!

    View Slide

  120. There’s a lot more about
    BEM. Go to bem.info
    and find out!

    View Slide

  121. Questions?

    View Slide

  122. Thanks!

    View Slide