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="form--inverse">

.form
  background-color: $white
  color: $black
  ...
  &__label
  ...
  &__input
  ...

and you wish to include a modifier named .color-inversed with properties such as:

  &--color-inveresed 
    @extend .form // my dilemma arises here. 
    background-color: $black
    color: $white

nesting it under the .form class:

.form
  &__label
  &__input
  &--color-inversed
  ...

Is it correct to @extend .form inside --color-inversed? It seems off due to potential redundancy in the final CSS output, yet without extending, div class='form--color-inversed would not inherit any .form styles.

Answer №1

Avoid using an additional modifier when extending an element in BEM, as modifiers are designed to be used in conjunction with their respective classes:

<div class="card card--highlighted"></div>

Modifiers serve as extra class names applied to block/element nodes within the DOM. Apply modifiers only to the blocks/elements they are meant to modify, while retaining the original class.

Explore naming conventions at getbem.com

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

What are the permissible child elements for an <a> tag?

Lately, I've been focused on structuring HTML code and it got me thinking about which elements are actually allowed as children of an <a> element. Can you help clarify? ...

Guide to making Bootstrap collapsible panels with full-width content that do not affect neighboring columns

I'm currently working on a responsive grid that displays items with expandable details when clicked. My issue is that when one item expands, the elements next to it collapse down, which is not what I want. I want all items to stay at the top and have ...

How to perfectly align a button at the center and position it in the forefront?

I'm attempting to place a button in front of my video and center it on the video. I've tried using position-relative to bring it in front and flex to center it vertically and horizontally, but it's not working as expected. The z-index is cor ...

A transparent float is yielding unexpected outcomes

<!Doctype html> <html> <head> <meta charset="utf-8" /> <title>testing code</title> </head> <body> <div style="float: left; width: 200px; height: 150px; background-color: ...

Utilizing the `theme` property in makeStyles with Material-UI

I am currently working on passing down an muiTheme to a component through the use of ThemeProvider, and I also want to apply it to the children components. Furthermore, I aim to utilize the theme's properties in both components by creating a classes o ...

What is the best method for implementing page transitions between components in NextJS?

My goal is to create a form that smoothly slides to the right, similar to the one seen on DigitalOcean's website when you click "Sign up using email" here: . While the transition itself is relatively simple, I noticed that DigitalOcean uses 2 separat ...

Eliminate any hyperlink mentions from the Media Print Screen

I'm experiencing a small issue where I am unable to remove the link reference from the print view. Below is the HTML code snippet: <table style="width: 436px; height: 374px;" border="0" cellpadding="5"> <tbody> <tr style=" ...

Activate the Sass IntelliSense feature specifically for .vue files within the VS Code editor

Could IntelliSense for Sass be activated in .vue documents without connecting them to sass, as this may disrupt other extensions dependent on the .vue file association? ...

Make sure that your inline-block elements are aligned with both the left and right edges of their

Explaining what I'm attempting can be a bit tricky, but I'll do my best. I have X number of categories that I need to organize. Each category needs to be enclosed in a box with a grey border. I want the category boxes to be displayed "inline-bl ...

Include content within the navigation bar of Bootstrap 3.3

I am trying to insert text at the end of the header in a jumbotron template: <div id="navbar" class="navbar-collapse collapse"> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text ...

Enhancing speed on an extensive list without a set height/eliminating the need for virtualization

My webapp includes a feature that showcases exhibitors at an expo. The user can click on "Exhibitors" in the navigation bar to access a page displaying all the exhibitors. Each exhibitor may have different details, some of which may or may not contain data ...

Adding CSS code to my index.css file in React is not reflected in my application

I've been following a tutorial on YouTube about reactJS, and the YouTuber recommended importing a CSS file into index.css to properly style the website. However, when I tried copying the CSS code and importing it into both App.js and Index.js, nothing ...

How to customize the color of the clock symbol in a MUI TextField set to type 'time'

<TextField id="endTime" label="End Time" onChange={onEndTimeChange} type="time" defaultValue="16:00" className={classes.textField} /> By using the attribute 'type="time"', an ic ...

Is there a distinction between em and percentage measurements and do their defaults have specified definitions?

Both em and percentage are defined in relation to other elements or properties. For example, with text-indent, it is related to the width of the paragraph, while with font size, it is related to the browser's default size. The question arises - wh ...

Stretchable navigation cell

I am working on a navigation bar called "nav" that consists of a "ul" and "li". I have specified the display property for the "ul" as table and for the "li" as table-cell. Setting the "ul" to take up the full width of the wrapper ensures proper alignment. ...

Struggling with the elimination of bullet points in CSS navigation bars

I'm having trouble removing the bullet points from my navigation bar. I've tried using list-style-type: none and even adding !important, but it doesn't seem to work. Strangely enough, everything looks fine in Chrome, but I get an error when ...

Differences in HTML animations can be seen when comparing Google Chrome to Microsoft Edge. Looking for a workaround for autoplay to ensure

My intro video animation is facing recording difficulties due to the autoplay policy in Google Chrome. It seems nearly impossible to capture it accurately. If only autoplay could function for an offline HTML file, my issue would be resolved. Unfortunately ...

Include a floating element within a modal box

I am trying to display some cards within a Bootstrap modal form. Inside the modal, I want the cards to appear in two columns by using the "col-md-5" class. .mycard.col-md-5 card contents However, when I use the "col-md-5" class, it adds a property o ...

Tips for creating multiple functions within a single JavaScript function

Is there a way to combine these two similar functions into one in order to compress the JavaScript code? They both serve the same purpose but target different CSS classes. The goal is to highlight different images when hovering over specific list items - ...

Place the social media division at the center of the webpage

I've been struggling to center the social media icons on my website, but haven't had any luck. Here's the code I've been working with: https://jsfiddle.net/2ahgL130/1/ CSS: .table1{ margin:0; padding:0; min-width:100% } ...