Combining various BEM modifiers in Sass

Currently, I am implementing BEM and have a specific element with multiple modifiers:

<div class="block__element block__element--m1 block__element--m2"></div>

In my project, SCSS is utilized to write nested rules that are in line with BEM conventions. If I aim to define a rule where an element (similar to the one above) possesses both the m1 and m2 modifiers, is there a method to achieve this while maintaining compatibility with my current approach? Below is the desired syntax which leads to a syntax error:

.block {
    display: block;

    &__element {
        display: inline;

        &--m1 {
            background-color: red;
        }

        &--m2 {
            background-color: green;
        }

        // Syntax error
        &--m1&--m2 {
            background-color: yellow;
        }
    }
}

Although attribute selectors can be used as a workaround, is there a more elegant solution?

Just to clarify, the compiled attribute selector should appear as follows:

.block__element--m1.block__element--m2

Answer №1

After reading @3rdthemagical's response, I was struck with inspiration for a more efficient solution. Turns out, Sass is not a fan of the & symbol following the beginning of a selector. However, it has no issues when enclosed in #{}:

.block {
    display: block;

    &__element {
        display: inline;

        &--m1 {
            background-color: red;
        }

        &--m2 {
            background-color: green;
        }

        // Works like a charm!
        &--m1#{&}--m2 {
            background-color: yellow;
        }
    }
}

Answer №2

Utilize variables to store selector names for better organization. Check out the Sassmeister demo.

.block {
  $module: ".block";

  &__element {
    $this: "#{$module}__element";

    &--m1#{$this}--m2 {
        background-color: yellow;
    }
  }
}

Here is the resulting css:

.block__element--m1.block__element--m2 {
  background-color: yellow;
}

Answer №3

Although this question may be dated, I have a fresh approach to solve it:


.square {
    display: block;

    &__shape {
        display: inline;

        &--style1 {
            background-color: blue;
        }

        &--style2 {
            background-color: orange;
        }

        &&--style1:is(&&--style2) {
            background-color: purple;
        }
    }
}

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

Steps to create a responsive iframe for Slideshare

Recently, I included a slideshare iframe presentation on my webpage. Here is the code snippet I used: <iframe src="//www.slideshare.net/slideshow/embed_code/key/HQoiz6GR1oLe1n" width="860" height="600" frameborder="600" marginwidth="0" marginheight="0 ...

Tips for customizing the appearance of the "Read More" button on an Elementor card

I'm new to the world of wordpress and I'm struggling to customize the CSS for a button on a card. My goal is to center the "Read more" button on the post card and give it a border, but I'm not having any luck in Elementor. I tried tweaking t ...

Is there a way to continuously switch a CSS animation class without needing a second click?

I am seeking a solution to animate the color and size of a div box, then return it to its original state when a button is clicked. Here is an example of my code: document.getElementById("andAction").addEventListener("click", function() { document.getE ...

"Utilizing lightbox JS to produce a centralized overlay across the entire webpage

Currently in the process of redesigning a gallery for a client at www.stagecraft.co.uk/gallery.html I am encountering some challenges with my overlay. I want it to be centered and on top of any divs that are currently in view, as new images from the clie ...

Using Clojure Hiccup for crafting stylish information in a unique manner

I want to create CSS style using hiccup by specifying the "top" and "left" variables to position an element. Currently, my code looks like this: (html [:div {:style (str "top" top ";left" left)} "some text"]) The code above is not very clean. It ...

Endless Background Image HTML/CSS

I'm currently working on a website where I'd like to have a background image that is black with some characters. The height of the page can vary, being either 'M' pixels or 'N' pixels tall. My goal is to show only one instanc ...

The Box element surpasses the height of the Grid component

Homepage Component <Box component="fieldset" sx={{borderRadius:2, height:"100%"}}> Application Structure Component <Grid container> <Grid item xs={4} > <Root/> </Grid> ...

Toggling CSS in Vue with a button and a checkbox

I'm currently working with VueJS and here is the CSS code I have: .button-css { align-items: center; background-color: var(--azure-radiance); border-radius: 30px; display: flex; height: 50px; min-width: 200px; padding: 0 60px; } .opensans-bold ...

What is the best way to align inline-block elements in the center?

I'm looking for a way to center inline-block elements, but I haven't had much luck so far. When I tried using margin-top: x em;, it just moved the image down instead of centering it. The inline-block is intentionally nested inside an image block. ...

Enhance your SVG with a stylish border by adding a decorative

Is there a way to add a blue or black border that follows the curve of an SVG used as a divider? <svg width="100%" height="96px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" prese ...

Tips on setting a background image to a div element

I'm facing an issue with a div setup like this: https://i.sstatic.net/4nuyO.png My goal is to remove a specific section of the circle shape displayed below: https://i.sstatic.net/09IWX.png The desired outcome is to achieve a final shape similar to ...

adaptable web design flexible picture

I am facing an issue while using MVC razor code to create a website logo and adjusting the image scaling for resizing the website. Even after trying image = max-width:100% and height:auto, the resizing is not working as expected! Below is the code snippet ...

Utilizing Bootstrap's Multi-Grid System

I am currently working on implementing a Bootstrap grid design that resembles pic1.jpg. However, I am facing challenges in achieving the desired layout, as pic2.jpg shows the current scenario. Below, I have shared the code I am using. pic1.jpg :- ! pic2. ...

Relentless underlining that feels like a trip to the

As a novice in web development, I am currently following a tutorial on creating a restaurant website. While I have encountered numerous obstacles along the way, most of them have been resolved successfully. However, I am facing an issue with my h1 element ...

Ways to choose the correct css styling

I'm currently working on creating navigation using Bootstrap 4, but I've hit a roadblock in adjusting the li items to suit my navigation needs. I'm struggling to figure out how to select these li tags since they are nested in multiple classe ...

I'm using Selenium XPATH or CSS to identify and select a checkbox within an HTML table based on the specified text. As a result, I have successfully

I am trying to select a checkbox from an HTML table with multiple columns. Specifically, I want to choose the checkbox that corresponds to the "CLEAN_AUDIT" option for the Name column. However, my current XPATH is selecting 4 checkboxes labeled CLEAN_AUDIT ...

Incorporate numerical increments onto the Bootstrap progress bar

Is there a way to incorporate steps (circles with numbers inside) on top of a Bootstrap 4 progress bar that are equally spaced? I have been considering the following approach: // css .wrapper-progress-step { /* what should go here? this would ensure t ...

Ways to structure a div to resemble a table

I'm currently working on a form that has multiple input fields, and I'm looking to structure them in a specific way. The layout I want to achieve can be seen in this example: https://jsfiddle.net/dmilos89/58ea82gj/2/ The example above utilizes ...

Can you modify a WordPress/WooCommerce title written in uppercase to display in proper capitalization?

My current project is centered around WordPress and utilizes WooCommerce as its e-commerce platform. I recently migrated products from an old site where the product titles were written in uppercase. At the moment, my HTML code for the product titles looks ...

Enhancing Vue with stylish components

My Vue component is quite basic, with the following structure: <script> export default { props: { source: { type: String, required: true, } }, } </script> <template> <div class="imageItem"> <img class="image" :data-srcse ...