Tips on properly styling HTML using the BEM naming convention

I'm trying to implement BEM naming in my project, but I'm having trouble with how to name elements within elements. BEM guidelines state that elements of elements should not exist. How should I approach naming them correctly?

<div class="container">
<div class="profile">
  <p class="profile__message></p>
  <div class="profile__item">
    <div class="profile__item__el profile__item__el-image">
      <a class="thumb"><img></a>
      <div class="profile__item__el-remove"></div>
    </div>
    <span class="profile__item__el profile__item__el-name"></span>
    <span class="profile__item__el profile__item__el-author"></span>
    <span class="profile__item__el profile__item__el-date"></span>
    <div class="profile__item__el-favorite"></div>
  </div>
</div>
</div>

After reviewing the code, I realize that having a separate class 'profile__item__el' for all elements might not be the best approach as not all elements are of the same type. They are all item elements, and I believe their class names should convey that information. However, it seems that according to BEM principles, this is not the correct way to name them.

Answer №1

Quick response.

Each block contains either an element or another block

Therefore, every element is assigned a class like block-name__elem-name - without any additional classes.

Every block has its own namespace.

To customize the appearance of a block or element in BEM, use a modifier.

Here's how it works: html + css - check it out below


BEM is also a set of technologies with its own template engine. You don't have to write plain html - it's automatically compiled.

Here's an example(bemjson):

{
  block : 'row',
    content : [
      {
          elem : 'col',
          mods : { sw : 4, mw : 2, lw : 2, xlw : 2 },
          content : [
              {
                  block : 'footer', // determines the parent element
                  elem : 'age',    // identifies the child element
                  content : [
                      {
                          block : 'icon',
                          mods : { type : 'some-icon' }
                      }
                  ]
              },
          ]
      },
  ]
}

You can view the resulting html in the bundle(html):

<div class="row"> // block namespace
  <div class="row__col row__col_sw_4 row__col_mw_2 row__col_lw_2 row__col_xlw_2"> //elem row__col with modifiers
    <div class="footer__age"> // child element age under parent footer block
      <i class="icon icon_type_some-icon">
        <svg></svg>
      </i>
    </div>
</div>

The css is structured like this(elements are typically in a 2-level hierarchy):

.row // namespace
{
    margin: 0 -7px;

    &__col // element
    {
        padding: 0 7px;
    }
    &__col_p_0 // element with modifier
    {
      padding: 0px;
    }
}

For more information, visit the official BEM website:

Answer №2

Perhaps leaning more towards this approach could be a slightly better option, although it still may not be ideal:

<div class="container">
    <div class="profile">
      <p class="profile__message></p>
      <div class="profile__item">
        <div class="profile__attribute profile__image">
          <a class="thumb"><img></a>
          <div class="action--remove"></div>
        </div>
        <span class="profile__attribute profile__name"></span>
        <span class="profile__attribute profile__author"></span>
        <span class="profile__attribute profile__date"></span>
        <div class="action--favorite"></div>
      </div>
    </div>
</div>

The use of the "profile__attribute" class may not be entirely necessary. It could be useful if you plan to apply consistent styles to all those attributes.

It seems like there might be some confusion about the intended use of the BEM 'modifier.' The correct format involves two hyphens, and modifiers are typically used for different variations of the same element. For instance, if you have a button with large and small versions, you could use button--large and button--small. In this case, the name, author, and date could be seen as separate elements rather than variations of one.

Ultimately, there isn't a definitive right or wrong approach to BEM. It often comes down to personal preference and adherence to coding style guides.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

text box with an immobile header

As the browser window size decreases, the layout changes. However, when scrolling down, the search text box moves up and is no longer visible due to its lack of fixation. How can I make the search text box stay fixed as I scroll down? I tried implementing ...

execute the function whenever the variable undergoes a change

<script> function updateVariable(value){ document.getElementById("demo").innerHTML=value; } </script> Script to update variable on click <?php $numbers=array(0,1,2,3,4,5); $count=sizeof($numbers); echo'<div class="navbox"> ...

I'm having issues getting my fancybox to display properly

HTML: <!--gallery--> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script> !window.jQuery && document.write('<script src="jquery-1.4.3. ...

Aligning Description Item components horizontally in antdLearn how to easily horizontally align Description

Currently, I am utilizing the `antd` Description components. In this scenario, when there is no `title` for the items, the value should be aligned to the left. You can see an example of this alignment in the image below: https://i.sstatic.net/Ah70f.png I ...

Animate with jQuery to swap the positions of two floating divs

Hello, I recently made some updates to the code by adding 2 divs of different sizes. I am trying to achieve a switching animation using CSS floats for these divs. If you'd like to take a look at the current code, please visit - http://jsfiddle.net/jz ...

What could be causing the lack of access to the prerender.io service in this particular setup?

I am the owner of a Angular application running on an Apache server. To enable prerendering, I added <meta name="fragment" content="!" /> in the <head> section and set $locationProvider.html5Mode(true); In my Apache server's .htaccess fi ...

Can the SVG def element be modified?

In my SVG file, there is a defs element structured as follows: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 250 330" onclick="tweenGloss()"> <defs> <linearGradient id="grad" x1="174.6 ...

Create an input field with a dynamic and exclusive identifier using the DataTables plugin

I am having trouble creating unique IDs for each input field based on the number of rows Here is the code snippet: $(document).ready(function() { var oTable = $('#jsontable').dataTable(); //Initialize the datatable $.ajax({ url ...

Learn how to utilize ng-class to assign an ID selector to HTML elements

In my code, I have an icon that is displayed conditionally using ng-class. I am looking to include ID's for both the HTML elements. <i ng-show="ctrl.data.length > 1" ng-class="{'glyphicon glyphicon-chevron-down': !ctrl.isOpen, ...

How can I view a comprehensive list of updates made to a webpage?

Is it possible to track and display all the modifications made to a web page? This includes new HTML elements added, old elements removed, and any changes in the location of existing elements. Currently, I am utilizing Selenium WebDriver with Java. Below ...

Iframe form submissions

I am interested in accomplishing the following task. My objective is to submit a form to my booking engine and show the results on either a new page or within a Div element. Ideally, when the user clicks the submit button, it would display an Iframe or re ...

The specified font-size in my CSS is being ignored by Chrome

I'm a beginner in the world of html/css and was tasked with creating a html webpage. I set a specific font-size for my webpage, only to be surprised when my browser (Google Chrome) adjusted it on its own. Even though I specified the font-size as 14px, ...

Minimize the screen dimensions and adjust the margins accordingly

Having a problem with responsive margins? Take a look at this site as an example: When I shrink the screen size, the margins decrease and smoothly transition from 4 to 2 columns. In my case, it does something similar but the issue is that the margin does ...

jquery unclean data, in need of the jquery ui popup dialog

I have been experimenting with this library to determine if a form is dirty. By default, it triggers the standard browser confirmation asking whether you are certain about navigating away from the page. The jquery dirtyforms website includes a section sugg ...

I'm struggling with my project called "Number TSP" and I can't seem to figure out why it's not working properly

Upon reaching the end of my code, I am encountering an issue where instead of seeing the expected output of "Done!", it displays undefined. This is the code in question: const container = document.querySelector(".container") const table = document.querySe ...

Guide on incorporating data from a text file into an HTML table

I'm attempting to showcase the records from upload.txt in an HTML table. The location of the upload.txt file is at this URL: http://localhost:3000/upload. When accessing this URL, it retrieves the upload.txt file. Currently, I am using node.js for r ...

Utilizing JQuery slideToggle() to Toggle Between Opening and Closing Multiple Divs

Utilizing Jquery's slideToggle() function to manage the opening and closing of my main menu has been a success. However, I am facing the challenge of making sure that when one div is opened, another one closes. Despite trying various methods, I have ...

Locating all elements on a webpage that are either above or below a specific element, capturing those that intersect visually

Is it possible to locate all the additional HTML elements that a particular element overlaps with, is positioned under, or intersects with? My goal is to ensure that this element is displayed above every other element. I am looking to identify all interse ...

Problem involving non-breaking spaces

After changing the font-family of my website from Gotham Pro to Gotham, I encountered an issue with the styling of numbers that were formatted using my currency formatter function. Strangely, when the &nbsp character is removed, everything appears to b ...

Converting JSON data retrieved from a URL into HTML format

I have a JSON output from a URL that I need to format into html. The structure of the JSON data is as follows: prtg-version "xxxxxxx" treesize 0 channels 0 name "Downtime" name_raw "Downtime" lastvalue "" lastvalue_raw "" 1 name ...