Is it necessary for an element in BEM to be contained within a block?

I have a question about BEM methodology. Is it acceptable to use a .block__element class on its own without a parent block? I am working on a WordPress site with various classes from the theme, page builder, and plugins, which can make things messy when trying to maintain the BEM structure. Can I just style the block__element directly using SCSS, or should I add a separate block to the structure for proper organization?

Sample HTML

<footer id="footer">
 <div class="theme-class page-builder-class">
  <div class="footer-block__text">Some footer element text</div>
 </div>
</footer>

CSS

#footer .footer-block__text {
 color: #ffffff;
}

Answer №1

In my humble opinion, I would have to lean towards the answer being a firm no, and here's why:

  • BEM was specifically crafted to uphold module separation for better clarity and to reduce the likelihood of styles bleeding over from one module or block to another. Mixing BEM blocks with non-BEM code goes against the fundamental principles of BEM.
  • The careful design of BEM blocks relies on inheriting styles from the block__element. Without the presence of a block ancestor, the structure of the library loses its intended functionality. For example, if the styling of block__element is contingent on receiving rules from block (which sets display:flex) but it lacks a block parent, the desired effect will not be achieved. In such scenarios, the essence of applying BEM gets compromised.

Answer №2

I believe that combining BEM classes with other methodologies is possible in many scenarios. BEM can be integrated with other approaches, legacy code, or even unique medium-specific classes.

For instance, I have personally combined different methodologies:

  • If you need to apply specific styling to a block that isn't based on logic, it may be more practical to use a utility class (e.g. .u-uppercase) rather than creating a non-logical BEM modifier (e.g. .title--uppercase).
  • If you have a layout class that is used across multiple blocks, using an OOCSS object class (e.g. .o-container) could simplify your BEM structure compared to repeating the same BEM element everywhere (e.g. .header__container).

You can explore a detailed presentation on extending BEM concepts here: Modular CSS at Rangle.

In my own experience, I experimented with mixing OOCSS (prefixed with l-) and BEM classes (prefixed with p-) while developing the website . Although it felt unfamiliar initially, this approach turned out to be highly beneficial and stable.

An illustration from the netalis website:

<!-- HEADER BLOCK -->
<div class="p-header p-header--hero">

    <!-- NESTED BLOCK -->
    <div class="p-menu"></div>

    <!-- OBJECT CLASS -->
    <div class="l-container">

        <!-- HEADER ELEMENT -->
        <div class="p-header__content"></div>
    </div>
</div>

This hybrid approach works because these external classes are:

  1. Segregated from BEM classes, ensuring no selectors mix both types (.p-header > .l-container)
  2. Immutable, meaning the utility/object classes remain consistent over time.

Hence, my query is: Are your page-builder-class isolated and immutable?

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

Header image misalignment

Seeking a solution to have the image fill up the remaining space in the .container class I attempted setting the width of the image in CSS to width: 100%, but encountered two different results: Image with attribute set: https://ibb.co/9yKJgvF If I remove ...

Innovative CSS trick for styling checkboxes alongside non-related content

The CSS checkbox hack demonstrated below assumes that the content is a sibling of the checkbox. By clicking on the label, the content will toggle. Check out the DEMO here <input id="checkbox" type="checkbox" /> <label for="checkbox"> Toggle ...

Analyzing neglected CSS in intricate websites

While there are tools available to identify unused CSS on static web pages, real-world scenarios often involve CSS being used dynamically after interactions such as opening modals or popups. How can we effectively manage our ever-growing render-blocking CS ...

Is there a way to adjust the opacity of a background image within a div?

I have a lineup of elements which I showcase in a grid format, here's how it appears: https://i.stack.imgur.com/JLCei.png Each element represents a part. The small pictures are essentially background-images that I dynamically set within my html. Up ...

Is there a way to create an HTML select element where each option has a unique background color, and will display properly

I want to create multiple HTML select elements with unique background colors for each option: <select runat="server" id="select"> <option value="A">White</option> <option value="B">Red</option> <option value="C">Yellow& ...

When I try to hover my mouse over the element for the first time, the style.cursor of 'hand' is not functioning as expected

Just delving into the world of programming, I recently attempted to change the cursor style to hand during the onmouseover event. Oddly enough, upon the initial page load, the border style changed as intended but the cursor style remained unaffected. It wa ...

The dominance of CSS specificity in favor of the flex property compared to flex-basis

Is there a specificity among CSS properties that affects how styles are applied? I've been experimenting with flexbox and encountered an interesting scenario: The second selector .flex-basis-5 is added dynamically via JavaScript based on certain con ...

Creating a clean and modern design with Bootstrap 4 by applying bottom borders solely to the

I need to create a sidebar with a bottom border after each li element, except the last one. However, I'm facing an issue with Bootstrap 4 where using border-0 removes all borders on list-group-items. I've tried editing it via CSS but can't s ...

Webkit browsers do not properly clip content with rounded corners when using position:relative

In webkit browsers like Chrome, rounded corners do not properly cut off content when using position:relative; Check out this demonstration. Here is the HTML: <div class="outer"> <div class="inner"> </div> <div> And here ...

Using CSS3 to apply transformations to multiple divs based on their individual attributes

I am currently developing JavaScript code that will enable the use of HTML like the example below: <div class="box" data-vert-speed="7">ContentDiv1</div> <div class="box" data-hori-speed="10">ContentDiv2</div> <di ...

Arranging HTML elements with jQuery - the existing script flips back and forth

I have created a code snippet on CodePen that showcases a feature of the website I am currently developing: http://codepen.io/barrychapman/pen/BzJGbg?editors=1010 Upon loading the page, you will notice 6 boxes. The first box is active, while the remaining ...

What are some strategies for disregarding global CSS styles?

I am facing an issue on my html page with a specific div structure. The <div id="container">/* lots of nested html with elements like div, ul, li etc */</div> is causing some trouble in my global css file called style.css. The style. ...

Upgrading Bootstrap from version 3.4 to 5.3 in a .NET project

I have a website project created using .Net Framework 4.7.2. Currently, I am utilizing Bootstrap version 3.4.1 from here. Since Bootstrap 3.4 is now in an end-of-life phase, I need to upgrade to the latest version, 5.3. Upon replacing the old version fil ...

The "list-group" class is not functioning properly in Bootstrap 4

I've been diligently working on a project with Bootstrap 4. In one particular section, I need to display images in rows within a list-group that pulls images from a folder. However, when using Bootstrap 4, the image sizes are too large, unlike when I ...

Updating form validation: relocating error messages

When using a jQuery form validation script, an "OK!" message is typically displayed below the input field when the input is correct. However, I would like to customize this behavior and have the "OK!" message appear on the right side of the field instead ( ...

iOS devices experiencing issues with iframe responsiveness

Can someone take a look at this for me? I can't seem to figure out what's missing. I have an iframe with a width that extends beyond the Bootstrap 12 col I placed it in, despite setting up CSS to make it responsive. The site works fine in desktop ...

Using Regular Expressions (or not) in Javascript/Jquery to append <span> around vowels

Seeking guidance as a beginner in the coding world. Currently, my code generates numbers from 1 to 99 and displays text next to the number based on certain conditions. For instance, numbers divisible by 3 are labeled 'Java', those divisible by 5 ...

Troubleshooting the issue of autoplay not functioning in HTML5 audio

I'm facing a strange issue with my code - the autoplay feature for audio is not working as expected. Typically, whenever I insert this particular piece of code into a website, the music starts playing automatically. However, it seems to be malfunctio ...

What is the best way to place an anchor so that it is perfectly aligned between a static header and footer?

I am looking to create a website where the content is placed below a fixed header and footer. Navigation is to be done through anchors, with only the active anchor being displayed while the rest remains hidden by the header and footer (similar to a window) ...

My website is being cut off

After creating this website with a viewport setup, I noticed that it is not fully visible on certain screens. When viewed on a CRT monitor at 800x600 desktop resolution or lower than 1280x800, such as on mobile devices, the content gets clipped. Is there a ...