Guest User

Untitled

a guest
Sep 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. ## BEM
  2.  
  3. Know your element (`__`) from your modifier (`--`). Both blocks and elements can have modifiers. (`.b--m`, `.b__e--m`).
  4.  
  5. In react lingo, BEM's "B" (for "Block") is a component.
  6.  
  7. Elements can nest deep in html, but the selector doesn't represent this deep nesting. (no `.a__b__c`, instead just `.a__c` - which can be in `.a__b`).
  8.  
  9. **The idea**
  10. The idea with BEM is to keep a flat hierarchy (don't nest selectors) with simple selectors - only one class per selector, and no IDs or element tags. By not nesting we have a low and consistent specificity of all html elements, which makes it possible to have modifications of styles without using `!important`.
  11.  
  12. Don't ever use the html element as well as a class in the selector for this same reason (no `button.button`), as well as not to lock oneself into having to use a specific type of element.
  13.  
  14. Only time nesting of selectors is allowed is if elements need changing when the block has a modifier class applied to it (`.editor--dark .editor__text {color: white}` for example).
  15.  
  16. Use of html element selectors can be used at times, for example in the case of the flag above, where we need to wrap the `img` in an element for styling, but might still want to apply styles to the `img` without having another class (`.flag__actual-image` is a bit ugly). But try to keep it to a minimum, and don't have "magic" elements, where (for example) a span in a `.media` gets grey text and centered (give it a name instead, `.media__caption` maybe?).
  17.  
  18. Since we scope every element and modifier, we are free to use whatever names we want - no risk of clashes between the icon called `danger` and the button style called `danger`.
  19.  
  20. **Example: **
  21.  
  22. Notice the two buttons which are both elements of the editor, as well as blocks in their own right, in two different ways.
  23.  
  24. Also, note that there are two navs, but one would be stuck to the top (`--top`) and the other bottom, but they are both nav bars of the editor, and so share most styles (different variants/modifications of the same element).
  25.  
  26. Lastly, notice the use of `icon--remove` even though we don't have `icon`. Since we don't nest our selectors it's possible to "borrow" modifiers (and elements) from blocks without applying the full block styles. In this case `icon--remove` sets the content of the `:before` pseudo element to the right icon, but doesn't apply any base icon styles, something `.icon` does but a button doesn't need. If we combined classes when making selectors `.icon.icon--remove` this wouldn't be possible, only thanks to the flat hierarchy with simple selectors is this possible.
  27. ```html
  28. <div class="editor editor--dark-mode">
  29. <div class="editor__nav editor__nav--top">
  30. <input class="editor__filename" type="text" />
  31. <button class="editor__button btn btn--danger btn--icon icon--remove">Move to trash</button>
  32. </div>
  33. <div class="editor__body">
  34. ...
  35. </div>
  36. <div class="editor__nav editor__nav--bottom">
  37. <button class="editor__button btn btn--primary btn--icon icon--save">Save</button>
  38. </div>
  39. </div>
  40. ```
Add Comment
Please, Sign In to add comment