Tips for eliminating repetitiveness in situations such as BEM modifiers

As I delved into using my own adaptation of the BEM methodology, I encountered a roadblock with modifiers for nested elements.

The challenge at hand is changing the link color to red when product-desc-name has the mark class.
The snippet below illustrates the issue.

How can I achieve the desired style without redundancy in class names?

.product-desc {
  &-name {
    &.mark {
      /* this section is ugly */
      .product-desc-link {
        color: red;
      }
    }
  }
}
<ul class="product-desc">
  <li class="product-desc-name">
    <a class="product-desc-link">Param1</a>
  </li>
  <li class="product-desc-name mark"> <!--add class .mark-->
    <a class="product-desc-link">Param1</a>
  </li>
</ul>

Answer №1

One common drawback of BEM is the lack of a clear solution for certain issues, such as the one discussed here.

To address this issue, I propose renaming the classes to better reflect their purpose. For instance, the UL element could be renamed 'product-desc-list' and the LI element could be called 'product-desc', emphasizing that it represents a product description.

It's also important to consider the condition of the product when selecting elements, allowing for greater flexibility in using multiple blocks within one component.

The first step is defining the component, followed by specifying different states such as selected or in progress.

Below is an example to illustrate these concepts:

// Default styling for the product description.
.product-desc {
  &--link {
    text-decoration: underline;
  }
}

// Styling for the product description in a marked state.
.mark {
    .product-description {
        &.--link{
            font-weight: bold;
        }
    }
}
<ul class="product-desc-list">
  <li class="product-desc">
    <a class="product-desc--link">Param1</a>
  </li>
  <li class="product-desc mark"> <!--add class .mark-->
    <a class="product-desc--link">Param1</a>
  </li>
</ul>

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

Incorporate an animated loading GIF image within the ajaxStart function, dynamically appending it within a div tag

Is there a way to dynamically display a loading GIF image on all HTML web pages? var loading = $(' <div id="loading"> <img src="../img/loading%20(1).gif" id="loading-image" alt="Loading..." /> </div>'); $(document).ajaxStart( ...

Table does not have appropriate rowspan and colspan attributes

I've created a table using bootstrap, but it's not displaying as intended. I used row-span and col-span to format the table, and while most rows are correct, some are not appearing on the page. To better illustrate the issue, I have included an ...

Adjust the width of the navigation bar

Currently, I am attempting to center the menu bar and ensure that it perfectly fits the text. For reference, here is the website that I'm currently working on: I have already discovered a way to center the menu items as shown below: .menu { tex ...

Taking out the z-index from the transition code

Is there a way to restructure the code without needing to use z-index for the transition? Without z-index: https://jsfiddle.net/Legcb42d/ .container1 { position: relative; width: 100%; height: 100%; } .container1.slide { height: auto; min-heigh ...

Unforeseen outcomes arise when toggling expansion in JavaScript

I have a pop-out div at the top of my page that expands and closes on toggle. Also, when I toggle the pop-out div, it changes the top position of another div (a side pop-out menu). The issue is that the side pop-out menu should only appear when clicking ...

Adjustable Text Size with HTML5 and CSS3

Looking to enhance the accessibility of a website by implementing text size adjustment buttons ' A A A ' using just HTML5 and CSS3. Avoiding the use of jQuery or Javascript for this task. Inspiration drawn from this link. Appreciate any assistan ...

Enhance your CSS tables by including margin columns and adding a bottom border to each row

After working on an HTML table element, I decided to include some space between the columns. To achieve this, I used the following css properties: table { border-collapse: separate; border-spacing: 36px 0px; } However, I encountered an issue when attem ...

Chrome has a tendency to cut off page contents in print preview, unlike other browsers that do not have this issue

My current version of Chrome browser (54.0.2840.59) is exhibiting a strange behavior when attempting to print preview an HTML page I have developed. The print preview truncates the page content, displaying less than what should be shown. The content is not ...

Innovative ways to design a responsive carousel with Bootstrap

My goal was to create a slider with divisions inside a bootsrap carousel, and here is what I did. However, I am looking for guidance on how to adjust it so that on screens smaller than sm (of bootstrap), there are only two divisions in one carousel, and a ...

Exploring the process of integrating absolute paths within a global SCSS file in a VueJS project

The webpack configuration in my vue.config.js file looks like this: const path = require("path"); module.exports = { pluginOptions: { 'style-resources-loader': { preProcessor: 'scss', patterns: [ "./src/style ...

Embedding images within written content

Is there a way to insert an image into text from a specific position without using tables? I'm looking for a method other than aligning the image "left or right". ...

CSS: A guide to correctly setting an image URL for a checked checkbox

In my current situation, I have a list of checkboxes with corresponding images. When a checkbox is checked, I would like to add a black circle behind the image. Here is an example of the current output: This is what I expect the output to look like: Her ...

Unusual phenomenon of overflow and floating point behavior

HTML and CSS Output Example: <div id="float_left"> DIV1 </div> <div id="without_overflow"> DIV2 </div> CSS Styling: #float_left{ float: left; width:200px; background-color: red; } #wit ...

Creating a Circular Design with a Centered Line and Text Above and Below using Qualtrics (HTML/CSS/Java)

My knowledge of creating shapes and using these programming languages is limited. I am currently working on a survey in Qualtrics and I need to insert text into circular shapes. Creating one circle was straightforward, thanks to some helpful discussions ...

Unable to achieve full width for a div within a scrollable container

Currently, I am in the process of constructing a new website using Gatsby.js. However, I have encountered a minor issue with some CSS related to code blocks. Since CSS is not my strong suit, I have been unable to resolve it thus far. Below is a runnable sn ...

Button for toggling the mobile navbar in Bootstrap

Is there a way to adjust the position of the navbar-toggle button http://prntscr.com/5sldgb within the navbar after changing its height? Here is the HTML code: <div class="navbar navbar-default"> <div class="container"> ...

Using HTML and JavaScript, we can set two different color values - one for the background and one for the h1 title

I am trying to save two values, one for the h1 tag and one for the body background. I want the user to select color 1 and color 2. When I press the third button, all changes should be applied and the colors should change. I have attempted this but with no ...

Injecting CSS styles into dynamically inserted DOM elements

Utilizing Javascript, I am injecting several DOM elements into the page. Currently, I can successfully inject a single DOM element and apply CSS styling to it: var $e = $('<div id="header"></div>'); $('body').append($e); $ ...

Methods for verifying if a file is included in another file, while disregarding any whitespace adjustments

Let's say I have multiple CSS files (a.css, b.css, ..., e.css) and after concatenating and compressing them, I get a new file called compressed.css. Now, I need to verify if each of the original CSS files is included in the compressed.css file. Are th ...

Enhancing an element with a modifier in CSS using BEM conventions without the need for @extend

Trying to grasp the best method for writing BEM modifier rules within parent classes. I've utilized SASS's @extend but question if this is the right approach. For example: If there is a class called .form structured like this: <div className= ...