[{"data":1,"prerenderedAt":660},["ShallowReactive",2],{"blog-an-introduction-to-bem":3},{"id":4,"title":5,"body":6,"date":650,"description":651,"extension":652,"meta":653,"navigation":655,"path":656,"seo":657,"stem":658,"__hash__":659},"blog\u002Fblog\u002Fan-introduction-to-bem.md","An introduction to BEM",{"type":7,"value":8,"toc":641},"minimark",[9,13,26,42,46,49,78,81,84,99,222,235,357,360,373,482,496,530,534,548,552,579,583,586,593,596,627,630,634,637],[10,11,12],"p",{},"Do any of these apply to you when writing CSS?",[14,15,16,20,23],"ol",{},[17,18,19],"li",{},"Find it difficult to name elements in CSS.",[17,21,22],{},"Find it difficult to manage sprawling CSS files.",[17,24,25],{},"Find yourself writing conflicting CSS rules.",[10,27,28,29,33,34,37,38,41],{},"If so, BEM might be a solution for you. BEM which stands for ",[30,31,32],"strong",{},"Block",", ",[30,35,36],{},"Element"," and ",[30,39,40],{},"Modifier"," is a methodology for writing CSS in a scoped way. For the rest of the article I'm going to be using Sass, but this can all be equally applied to plain-old css too.",[43,44,32],"h2",{"id":45},"block",[10,47,48],{},"The way the scoping works in BEM is first by defining a block (you can also think of this as a component on a page) to start writing your CSS for, for this example let's start with a navigation:",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-css shiki shiki-themes github-dark",".navigation {\n}\n","css","",[57,58,59,72],"code",{"__ignoreMap":55},[60,61,64,68],"span",{"class":62,"line":63},"line",1,[60,65,67],{"class":66},"svObZ",".navigation",[60,69,71],{"class":70},"s95oV"," {\n",[60,73,75],{"class":62,"line":74},2,[60,76,77],{"class":70},"}\n",[10,79,80],{},"The key takeaway at this stage being that you should break your page down into blocks of encapsulated CSS that you can apply to your HTML.",[43,82,36],{"id":83},"element",[10,85,86,87,90,91,94,95,98],{},"If we were to think logically about the child elements in our navigation we would assume that there would be a list ",[57,88,89],{},"\u003Cul>",", a list item ",[57,92,93],{},"\u003Cli>"," and a link ",[57,96,97],{},"\u003Ca>",". In BEM, these child  elements are defined using a double underscore between the block and element name and would be written like so in CSS:",[50,100,102],{"className":52,"code":101,"language":54,"meta":55,"style":55},".navigation {\n    &__list {\n        display: flex;\n        align-items: space-between;\n    }\n    &__item {\n        flex: 1;\n    }\n    &__link {\n        display: block;\n        color: red;\n    }\n}\n",[57,103,104,110,119,135,148,154,162,175,180,188,199,212,217],{"__ignoreMap":55},[60,105,106,108],{"class":62,"line":63},[60,107,67],{"class":66},[60,109,71],{"class":70},[60,111,112,116],{"class":62,"line":74},[60,113,115],{"class":114},"s4JwU","    &",[60,117,118],{"class":70},"__list {\n",[60,120,122,126,129,132],{"class":62,"line":121},3,[60,123,125],{"class":124},"sDLfK","        display",[60,127,128],{"class":70},": ",[60,130,131],{"class":124},"flex",[60,133,134],{"class":70},";\n",[60,136,138,141,143,146],{"class":62,"line":137},4,[60,139,140],{"class":124},"        align-items",[60,142,128],{"class":70},[60,144,145],{"class":124},"space-between",[60,147,134],{"class":70},[60,149,151],{"class":62,"line":150},5,[60,152,153],{"class":70},"    }\n",[60,155,157,159],{"class":62,"line":156},6,[60,158,115],{"class":114},[60,160,161],{"class":70},"__item {\n",[60,163,165,168,170,173],{"class":62,"line":164},7,[60,166,167],{"class":124},"        flex",[60,169,128],{"class":70},[60,171,172],{"class":124},"1",[60,174,134],{"class":70},[60,176,178],{"class":62,"line":177},8,[60,179,153],{"class":70},[60,181,183,185],{"class":62,"line":182},9,[60,184,115],{"class":114},[60,186,187],{"class":70},"__link {\n",[60,189,191,193,195,197],{"class":62,"line":190},10,[60,192,125],{"class":124},[60,194,128],{"class":70},[60,196,45],{"class":124},[60,198,134],{"class":70},[60,200,202,205,207,210],{"class":62,"line":201},11,[60,203,204],{"class":124},"        color",[60,206,128],{"class":70},[60,208,209],{"class":124},"red",[60,211,134],{"class":70},[60,213,215],{"class":62,"line":214},12,[60,216,153],{"class":70},[60,218,220],{"class":62,"line":219},13,[60,221,77],{"class":70},[10,223,224,225,33,228,37,231,234],{},"If we were to compile this to CSS we would end up with the following CSS classes: ",[57,226,227],{},".navigation__list",[57,229,230],{},".navigation__item",[57,232,233],{},".navigation__link",". You can now see that we have clearly defined child elements that are scoped to the navigation block that we can apply to our HTML:",[50,236,240],{"className":237,"code":238,"language":239,"meta":55,"style":55},"language-html shiki shiki-themes github-dark","\u003Cnav class=\"navigation\">\n    \u003Cul class=\"navigation__list\">\n        \u003Cli class=\"navigation__item\">\n            \u003Ca class=\"navigation__link\" href=\"\">\n            \u003C\u002Fa>\n        \u003C\u002Fli>\n    \u003C\u002Ful>\n\u003C\u002Fnav>\n\n","html",[57,241,242,263,280,296,321,330,339,348],{"__ignoreMap":55},[60,243,244,247,250,253,256,260],{"class":62,"line":63},[60,245,246],{"class":70},"\u003C",[60,248,249],{"class":114},"nav",[60,251,252],{"class":66}," class",[60,254,255],{"class":70},"=",[60,257,259],{"class":258},"sU2Wk","\"navigation\"",[60,261,262],{"class":70},">\n",[60,264,265,268,271,273,275,278],{"class":62,"line":74},[60,266,267],{"class":70},"    \u003C",[60,269,270],{"class":114},"ul",[60,272,252],{"class":66},[60,274,255],{"class":70},[60,276,277],{"class":258},"\"navigation__list\"",[60,279,262],{"class":70},[60,281,282,285,287,289,291,294],{"class":62,"line":121},[60,283,284],{"class":70},"        \u003C",[60,286,17],{"class":114},[60,288,252],{"class":66},[60,290,255],{"class":70},[60,292,293],{"class":258},"\"navigation__item\"",[60,295,262],{"class":70},[60,297,298,301,304,306,308,311,314,316,319],{"class":62,"line":137},[60,299,300],{"class":70},"            \u003C",[60,302,303],{"class":114},"a",[60,305,252],{"class":66},[60,307,255],{"class":70},[60,309,310],{"class":258},"\"navigation__link\"",[60,312,313],{"class":66}," href",[60,315,255],{"class":70},[60,317,318],{"class":258},"\"\"",[60,320,262],{"class":70},[60,322,323,326,328],{"class":62,"line":150},[60,324,325],{"class":70},"            \u003C\u002F",[60,327,303],{"class":114},[60,329,262],{"class":70},[60,331,332,335,337],{"class":62,"line":156},[60,333,334],{"class":70},"        \u003C\u002F",[60,336,17],{"class":114},[60,338,262],{"class":70},[60,340,341,344,346],{"class":62,"line":164},[60,342,343],{"class":70},"    \u003C\u002F",[60,345,270],{"class":114},[60,347,262],{"class":70},[60,349,350,353,355],{"class":62,"line":177},[60,351,352],{"class":70},"\u003C\u002F",[60,354,249],{"class":114},[60,356,262],{"class":70},[43,358,40],{"id":359},"modifier",[10,361,362,363,366,367,369,370,372],{},"There may be situations where you don't need a new element, but need to modify it somehow. In the case of a navigation you may need to indicate a active state - step in ",[30,364,365],{},"modifiers",". A modifier is defined using a double dash between either the ",[30,368,45],{}," or the ",[30,371,83],{},":",[50,374,376],{"className":52,"code":375,"language":54,"meta":55,"style":55},".navigation {\n   &--pad {\n       padding: 20px;\n   }\n   &__link {\n       display: block;\n       color: red;\n       &--is-active {\n           border-bottom: 2px solid red;\n       }\n   }\n}\n",[57,377,378,384,392,408,413,419,430,441,449,469,474,478],{"__ignoreMap":55},[60,379,380,382],{"class":62,"line":63},[60,381,67],{"class":66},[60,383,71],{"class":70},[60,385,386,389],{"class":62,"line":74},[60,387,388],{"class":114},"   &",[60,390,391],{"class":70},"--pad {\n",[60,393,394,397,399,402,406],{"class":62,"line":121},[60,395,396],{"class":124},"       padding",[60,398,128],{"class":70},[60,400,401],{"class":124},"20",[60,403,405],{"class":404},"snl16","px",[60,407,134],{"class":70},[60,409,410],{"class":62,"line":137},[60,411,412],{"class":70},"   }\n",[60,414,415,417],{"class":62,"line":150},[60,416,388],{"class":114},[60,418,187],{"class":70},[60,420,421,424,426,428],{"class":62,"line":156},[60,422,423],{"class":124},"       display",[60,425,128],{"class":70},[60,427,45],{"class":124},[60,429,134],{"class":70},[60,431,432,435,437,439],{"class":62,"line":164},[60,433,434],{"class":124},"       color",[60,436,128],{"class":70},[60,438,209],{"class":124},[60,440,134],{"class":70},[60,442,443,446],{"class":62,"line":177},[60,444,445],{"class":114},"       &",[60,447,448],{"class":70},"--is-active {\n",[60,450,451,454,456,459,461,464,467],{"class":62,"line":182},[60,452,453],{"class":124},"           border-bottom",[60,455,128],{"class":70},[60,457,458],{"class":124},"2",[60,460,405],{"class":404},[60,462,463],{"class":124}," solid",[60,465,466],{"class":124}," red",[60,468,134],{"class":70},[60,470,471],{"class":62,"line":190},[60,472,473],{"class":70},"       }\n",[60,475,476],{"class":62,"line":201},[60,477,412],{"class":70},[60,479,480],{"class":62,"line":214},[60,481,77],{"class":70},[10,483,484,485,488,489,492,493,495],{},"The link class modifier will output as ",[57,486,487],{},".navigation__link--is-active"," in your css and you  ",[30,490,491],{},"add"," it onto the ",[57,494,233],{}," class when you want to indicate a active link e.g.",[50,497,499],{"className":237,"code":498,"language":239,"meta":55,"style":55},"\u003Ca class=\"navigation__link navigation__link--is-active\" href=\"\">\n\u003C\u002Fa>\n",[57,500,501,522],{"__ignoreMap":55},[60,502,503,505,507,509,511,514,516,518,520],{"class":62,"line":63},[60,504,246],{"class":70},[60,506,303],{"class":114},[60,508,252],{"class":66},[60,510,255],{"class":70},[60,512,513],{"class":258},"\"navigation__link navigation__link--is-active\"",[60,515,313],{"class":66},[60,517,255],{"class":70},[60,519,318],{"class":258},[60,521,262],{"class":70},[60,523,524,526,528],{"class":62,"line":74},[60,525,352],{"class":70},[60,527,303],{"class":114},[60,529,262],{"class":70},[43,531,533],{"id":532},"what-are-the-benefits","What are the benefits?",[14,535,536,539,542,545],{},[17,537,538],{},"Shared language within the team - this means that it's easier for a fellow team member to pick up the component and understand what it's trying to achieve from the get-go.",[17,540,541],{},"No hugely sprawling SCSS files - preventing infinite amounts of nesting which can be hard to read and discern the purpose of each selector.",[17,543,544],{},"Use modifiers to create alternative versions of components - this encourages the re-use of the same component.",[17,546,547],{},"By following this methodology it scopes your CSS - this in turn reduces conflicts with other team member's CSS as well as your own.",[43,549,551],{"id":550},"what-to-watch-out-for","What to watch out for...",[14,553,554,561,573,576],{},[17,555,556,557,560],{},"Think about the ",[30,558,559],{},"purpose"," of the component and name it based on this e.g. 'Grid List' rather than 'Event List'. This means this component makes sense in more than one context (even if it's only used in one to begin with).",[17,562,563,564,568,569,572],{},"Try not to reach for modifiers straight away - think about how ",[565,566,567],"em",{},"flexbox"," or ",[565,570,571],{},"grid"," can help achieve your layout aims.",[17,574,575],{},"The component should work in most contexts therefore leave layout and alignment to higher level components - in other words, the component should work in isolation.",[17,577,578],{},"Don't try and do too much with one component, if you are finding it increasingly difficult to  name the elements within the component it's likely a sign that you need to break it down into multiple components.",[43,580,582],{"id":581},"in-practice","In practice...",[10,584,585],{},"You may come across a design like the below (hopefully it will look slightly better!):",[10,587,588],{},[589,590],"img",{"alt":591,"src":592},"BEM example","\u002Fimages\u002Fbem.jpg",[10,594,595],{},"In the above example I would separate the components like so:",[270,597,598,605,615,618],{},[17,599,600,601,604],{},"The hero would be the highest level component, with it's own child element ",[57,602,603],{},".hero__caption"," (this could contain any content)",[17,606,607,608,610,611,614],{},"The ",[57,609,603],{}," element would control the alignment of any child elements within it using flexbox, I would also add a few modifiers to align it to the left, center or right e.g. ",[57,612,613],{},".hero__caption--align-right",".",[17,616,617],{},"The search box would be a new component, it's alignment in this context would be controlled by the parent element.",[17,619,620,621,37,624,614],{},"The tabs would be another new component, comprised of a ",[57,622,623],{},".tab__item",[57,625,626],{},".tab__link",[10,628,629],{},"As you can see with the above example I have split the design into three different components, not only this I have decoupled the components so that the search and tabs could be easily moved from this component and used elsewhere, equally another component could sit inside the hero caption.",[43,631,633],{"id":632},"conclusion","Conclusion",[10,635,636],{},"BEM is great for making it easier to name your CSS and creating a shared language amongst team members, but above all it encourages you to break down your layouts into logical and purposeful components.",[638,639,640],"style",{},"html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .s4JwU, html code.shiki .s4JwU{--shiki-default:#85E89D}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}",{"title":55,"searchDepth":74,"depth":74,"links":642},[643,644,645,646,647,648,649],{"id":45,"depth":74,"text":32},{"id":83,"depth":74,"text":36},{"id":359,"depth":74,"text":40},{"id":532,"depth":74,"text":533},{"id":550,"depth":74,"text":551},{"id":581,"depth":74,"text":582},{"id":632,"depth":74,"text":633},"2019-08-26","Do any of these apply to you when writing CSS? Find it difficult to name elements in CSS. Find it difficult to manage sprawling CSS files…","md",{"url":654},"an-introduction-to-bem",true,"\u002Fblog\u002Fan-introduction-to-bem",{"title":5,"description":651},"blog\u002Fan-introduction-to-bem","pieimLqdpUqOhZ_9otDlYfF4O3magI1fbY_ZTUArRGM",1794763528558]