BEM (Block Element Modifier) stands as a widely embraced CSS class naming convention designed to simplify the maintenance of cascading style sheets. Recognized for its effectiveness in organizing code and promoting reusability, BEM plays a crucial role in enhancing the clarity and structure of CSS. In the following discussion, we assume a foundational understanding of the BEM naming convention, directing our focus towards refining its syntax and addressing potential challenges. As we delve into the nuances of BEM, we introduce an alternative approach—ABEM (Atomic Block Element Modifier)—which seeks to overcome certain limitations while aligning with the principles of Atomic Design.
The standard BEM syntax is: “block-name__element-name–modifier-name”. Breaking styles into small components facilitates easier maintenance compared to dealing with high specificity scattered across the stylesheet. However, the syntax poses challenges in production and may cause confusion for developers. This article introduces a refined version of the syntax named ABEM (Atomic Block Element Modifier):
The prefix “a/m/o” belongs to Atomic Design methodology and should not be confused with Atomic CSS, as they are distinct concepts. Atomic Design is a structured approach aimed at efficiently organizing components to enhance code reusability.
It splits the components into three folders: atoms, molecules, and organisms. Atoms represent basic components, often comprising only one element such as a button. Molecules are comprised of small clusters of elements or components, like a form field with a label and input. Organisms, on the other hand, are intricate components composed of multiple molecules and atoms, for example, a comprehensive registration form.
Utilizing Atomic Design alongside classic BEM presents a challenge due to the absence of clear indicators indicating the type of component a block represents. This lack of clarity can lead to difficulty in locating the code for a particular component, often requiring searches across multiple folders. By prefixing with “atomic,” it becomes instantly evident which folder houses the component, streamlining the navigation process.
In Classic BEM, each word within a section is separated by a single dash. It’s worth noting that in the example provided, the atomic prefix is also set apart from the remainder of the class name by a dash. Consider the implications when adding an atomic prefix to Classic BEM versus camelCase:
At first glance, the component name appears as “o subscribe form” in the Classic BEM method, where the significance of the “o” is obscured. However, in the camelCase version, adding “o-” distinctly separates this prefix, indicating its intentional distinction from the component name. Alternatively, in Classic BEM, one could apply the atomic prefix by capitalizing the “o” as follows:
While capitalizing the “o” in Classic BEM would address the problem of it blending into the class name, it doesn’t resolve the fundamental issue inherent in the classic BEM syntax. When words are separated by dashes, the dash character loses its utility as a means of grouping. Employing camelCase allows us to reserve the dash character for supplementary grouping, such as appending a number to the end of a class name. Additionally, camelCase enhances the clarity of class name grouping, simplifying processing. In camelCase, each break in the class name signifies a distinct grouping, whereas in classic BEM, such breaks can indicate either a grouping or simply a space between words within the same group.
Consider this silhouette of a classic BEM class (with an atomic prefix) and attempt to discern the delineation between the prefix, block, element, and modifier sections:
Now, take a look at this version. It’s identical to the one above, but this time it employs camelCase to separate each word instead of dashes:
These silhouettes represent how your mind visually parses through your code. The surplus of dashes in the class name diminishes the clarity of groupings. While reading the code, your brain must discern whether encountered gaps signify new groupings or merely separate words within the same group. This ambiguity increases cognitive load and complicates the task at hand.