Managing Variants in SCSS with BEM Approach: A Comprehensive Guide

Trying to solve a frustrating issue with BEM can be quite the challenge. I currently use the BEM methodology for developing UI elements in my application. For example:

<div class="card">
        <h2 class="card__title">Sample Title </h2>
        <h3 class="card__subtitle">Sub Title </h3>
        <button class="card__action">Action Button </button>
    </div>

The SCSS code for this would look like:

.card {
        &__title {color: red}
        &__subtitle {color: violet}
        &__subtitle {background: black}
    }

If I want to add a new variation of the card, it would be added as "card--modifier".

.card {
        &--variant {background-color: white;}
        &__title {color: red;}
        &__subtitle {color: violet;}
        &__subtitle {background: black;}
    }

Now, without breaking the nesting in SCSS, how can I modify the properties of a child under the variant?

Answer №1

When faced with situations like this, I don't see the need to make a big deal out of repeating a classname here and there.

For example, if you want the card__title to have a blue font color in the case of card--variant, you can simply add the necessary changes at the end of your rules (remember that order matters, prioritize low specificity and handle special cases last).

.card {
   &__title { color: red; }
   &__subtitle { color: violet; }
   &__action { background-color: black; }

   &--variant {
       .card__title { color: blue; }
   }
}

If for any reason you absolutely do not want to duplicate a name, you can store the block-level name in a variable and use string interpolation:

.card {
   $block: &;
   &__title { color: red; }
   &__subtitle { color: violet; }
   &__action { background-color: black; }

   &--variant {
       .#{$block}__title { color: blue; }
   }
}

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

When creating a PDF from HTML using iText, the CSS styling is not applied

Even though I am able to generate a PDF from an HTML string, the issue is that it doesn't apply the CSS styles. How can I create a PDF with CSS styling? Please assist! I have already attempted using CSSResolver. Below is my code snippet: {String res ...

What is the process for installing and utilizing the custom CSSLint rules that I have developed for the command line interface?

After investing a significant amount of time into following the instructions outlined here and here for creating custom CSS linting rules using CSSLint via the CLI, I have successfully generated and tested my own set of rules. However, I am now facing the ...

I encountered a sudden issue with Beautifulsoup 4's .find_all() function, as

An automated scientific literature collector using Google Scholar was successfully collecting data until a sudden issue arose. Despite the data entering the soup, the results were empty after the first .find_all() function. Interestingly, this problem did ...

Erase periods from the text box

Is there a way to remove the dots in the right bottom corner of a textarea without disabling resizing? https://i.sstatic.net/N2zfh.jpg I have seen a solution using the css property resize:none, but I would like to keep the resizing functionality... Are ...

I am looking to update a WordPress site every 10 seconds to keep it current

Currently, I am working on a plugin and my goal is to automatically refresh the widget every 10 seconds. Can someone guide me on how to achieve this? I need to figure out a way to call the widget function at regular intervals in order to refresh the conte ...

The clash of interests between jQuery and Bootstrap

I'm facing a problem with conflicting jQuery and Bootstrap files in my header.php code. Whenever I include the jQuery file before the bootstrap.js file, it causes issues with the tabs on my website where clicking on them does not navigate me to the co ...

How is it possible for the web browser to display fonts like Open Sans even if they are not installed on my device?

Explaining how a browser is able to correctly display text with a specific Font-Family, it can be described as follows: The font-family property allows designers to create a prioritized list of fonts for the browser to use when displaying content. If the ...

Can I inspect the HTML source of GWT?

How can I access the HTML source code generated by GWT? Currently, I only see the DIV with a specific ID when viewing the source. Is there a way to view the entire HTML output? Is it possible to design my table in HTML using divs and lists, then wrap it i ...

Is there a way to adjust the dimensions of the <audio> tag using CSS?

//I am trying to adjust the size of the audio player based on the 'height' and 'width' properties, but it doesn't seem to be working as expected. <audio autoplay controls height="100px" width="100px"> ...

Effortless design using Flex Box

Creating a consistent HTML structure for my website using bootstrap 4 is my goal. The key elements include: sidebar, h1, and content. The challenge lies in organizing them effectively based on the screen size: For mobile view, they should be arranged in ...

best practices for creating space between flexbox items

My task involves presenting a continuous scroll list of scheduled jobs using Angular Material. Each item in the list needs to be divided into three columns utilizing flexbox. Despite my efforts, I am struggling with adding space between the columns within ...

Font discrepancies in HTML field types "text" and "textarea" are causing inconsistent font display

Why do the text in my "text" and "textarea" fields show different fonts on my pages' HTML? I attempted to specify the font type in both the background CSS file and within the HTML, but it did not resolve the issue. Just to be transparent...I'm ...

Is it possible to utilize multiple .html files in a single view while incorporating Materialize?

Within my AngularJS application that utilizes Materialize, I have a module called "reports" containing a view called "reports.html" intended to house all of my reports. However, I desire to separate the code for each report into individual .html files, suc ...

Events are failing to appear in their correct positions on the screen

Software Version - "react-big-calendar": "^0.28.2" https://i.sstatic.net/nU4K6.png- Attached is a screenshot for reference. Please see below React code snippet: <Calendar defaultDate={moment().toDate()} defaultVie ...

Submit the chosen choice via the get method to navigate to a different page

I'm having an issue with a dropdown select option and a button. When the button is clicked, the selected option should be sent to another page called "update.php" so it can be accessed there. However, I am facing difficulties as the new page is not lo ...

Navigating the website with curtain.js and anchor tags

Currently, I am working on a website located at www.TheOneCraft.co.uk. I have incorporated the curtain.js jQuery plugin to create animated slide/pages as users scroll down. However, I have been unsuccessful in making the navigation bar follow this animati ...

Examining a specific element node to see if it contains a certain string

When using PHP Unit with Selenium Server, my goal is to verify if a specific element in an xpath contains a certain string value, like this: <table> <thead></thead> <tbody> <tr> <th>1</th> < ...

HTML <section> Order Placement

I am currently facing an issue with the placement of two divs on my html page. I have a navigation bar and another content div, but they are not appearing in the right order. This is how my html structure looks: <html> <body> <div id="navig ...

Converting from CAPS lock to using the capitalize property in CSS

Is there a way to transform a sentence that is all in CAPs lock without changing it? This sentence is part of a paragraph, and I am looking for a CSS solution (maybe with a little Jquery) that works reliably across most devices! I have come across similar ...

Is it possible to modify the CSS styling in React using the following demonstration?

I am attempting to create an interactive feature where a ball moves to the location where the mouse is clicked. Although the X and Y coordinates are being logged successfully, the ball itself is not moving. Can anyone help me identify what I might be overl ...