Introduction to BEM Methodology



INTRODUCTION TO BEM METHODOLOGY:

                From the earlier post, we get to know what CSS Architecture is and why we need that. If you have not taken a look, I would have recommended you to peer at that once.

What is methodology? In simple terms, naming the CSS selectors in a particular way.


This very day, the developers are using various methodologies with an ultimate aim of reducing the CSS code and making our CSS more maintainable. As mentioned in the earlier post, as the size of the team and project grows, basic type of styling would mess up everything.


BEM methodology is mainly used to speed up the development process and ease teamwork by arranging the CSS classes into independent modules.


BEM => Block Element Modifier.


This is not any kind of coding language or dependencies or logic or libraries that need to be perfect and  be downloaded. This is just to declare a CSS class.


*Note: This methodology is applicable only for classes since ID should not repeat*


B’ - Block:


DECLARATION: .block-name


    EXPLANATION: 

A Block is an object in our website. Think of it as the parent in the DOM tree.  For example, in HTML, we have the most common blocks like header, main, footer, content, sidebar. 

For instance, take a card. Now the card is an object, that is, it is the block    now. Therefore, it should have the class name like “. card”.


'E' - Element:


    DECLARATION: .block-name__element-name


    EXPLANATION:

         Now this element performs a particular function in a block. Proceed with our previous example. In that case, ‘.card’ is the block. Now the card may have a  title. This can be declared as ‘.card__title’.



'M' - Modifier:


    DECLARATION: .block-name__element-name--modifier-name


    EXPLANATION:

         Now this modifier works for both Block and element. There is only one very critical rule in BEM methodology - “A modifier cannot be used outside of the context of its own”.  For instance, again card may have two buttons, but these buttons have the same material properties having slight changes in their font color and background.  This can be defined in a class like “.card__btn--lg  and .card__btn--sm”. This should be used along with “.card__btn”.

 

There are two ways that we can follow:


1.Harris Roberts Style:

.block-name__element-name--modifier-name 

2.Pascal Case:

".BlockName__ElementName--ModifierName

   There are a few others too. But my most favorite is Harris Robert Style of naming the class which has the following rules

  • Names are lowercase

  • Words within the name separated by ‘-’

  • Element starts with ‘__’ (Two underscores)

  • Modifier starts with ‘--’ (Two hyphens)

 

This approach is most commonly used in SASS files. We'll see about SASS in further posts. To continue with our jottings, I would recommend you to get along with our posts to boost up your computer science knowledge.














Comments

Popular posts from this blog

MULTIMEDIA

Exploring The Hidden Internet

SKINPUT TECHNOLOGY