Top method for expanding a SASS class using an inner selector (sub-class)

When it comes to styling HTML to display messages, here's what I have:

  <div className="message">
    <div className="message_label">
      A message
    </div>
  </div>

To style these messages, I am using the SCSS class below:

  .message {
    margin: 10px;
    border-radius: 10px;
    border: 3px solid lightblue;
    &_label {
      color: #444;
      padding: 5px;
    }
  }

Following BEM methodology, I want to create a modified version for error messages:

  <div className="message--error">
    <div className="message_label">
      This is an error!
    </div>
  </div>

In this error version, I simply need to change the colors to red, so I need to extend the existing SCSS class:

  .message {
    margin: 10px;
    border-radius: 10px;
    border: 3px solid lightblue;
    &_label {
      color: #444;
      padding: 5px;
    }

    &--error {
      @extend .message;
      border: 3px solid red;
      &_label {
        color: red; // However, this is not working
      }
    }
  }

The issue lies with the selector message_label because it is an inner selector and the @extend doesn't affect it as per the SCSS Docs. What would be the best approach to extending a class that includes an inner selector?

You can view the DEMO here.

Answer №1

The issue at hand is that @extend simply shares certain css properties among different classes. In this scenario, it should generate a selector like:

.message, .message--error {
    margin: 10px;
    border-radius: 10px;
    border: 3px solid lightblue;
}

.message_label {
    color: #444;
    padding: 5px;
}

.message--error {
    border: 3px solid red;
}

.message--error_label {
    color: red;
}

Notice how your color: red could potentially end up in the stylesheet.

Furthermore, I strongly advise against formatting your scss in this manner. It would be incredibly challenging to maintain and comprehend.

Solution

My recommendation to address your query is to simply use .message and .message--error on the same element, much like how Bootstrap handles things

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

Exploring the orderBy feature in the react-firebase-hooks library with NextJS

Recently, I've been utilizing the React Firebase Hooks package from this GitHub repository. The code snippet below has been functioning smoothly for me. const [posts, loading, error] = useCollection( firebase .firestore() .collection(& ...

Tips for enabling the wrap property with autogeneratecolumn set to true

In my grid view, I want to ensure that any text in a column that exceeds the width of the column will either wrap within the column or display on a new line. Below is the code for the grid view: <asp:GridView ID="GridView1" AllowSorting="True" runa ...

The combination of flex box and the ox component in MUI seems to have trouble working alongside the Toolbar component

Whenever I attempt to align items in a toolbar using flexbox, it seems to be ineffective. <AppBar sx={{ width: { sm: `calc(100% - ${drawerWidth}px)` }, ml: { sm: `${drawerWidth}px` }, }} position="fixed&q ...

creating nested columns within a parent column using flexbox techniques

Struggling with column height issues in Bootstrap 3? Consider using flexboxes for a simpler solution. Check out the design image I created using Bootstrap 3: https://i.sstatic.net/PVCKm.png Can this design be replicated with flexboxes? I have successfull ...

Row in Internet Explorer 7

I have a function that reveals hidden rows in a table, which works perfectly in all tested browsers except for IE7. The function is written using Prototype.js. function showInactives(){ var row_list = $$('tr.inactive'); var ck =$('inactive_ ...

What is the best way to make my text scroll horizontally if the container is not wide enough?

Instead of overwhelming you with code, I'll just share a link to the types of animations I've come across so far. Although these options are close to what I'm looking for, none of them are exactly right. The one that comes closest to my vis ...

Within a container, there are two divs separated by a white bar

Hello everyone, I am in need of creating a unique slideshow for my website and I want to start by splitting a div into two sections using a slightly skewed diagonal line that is either semi-transparent black or solid white. I have searched extensively onli ...

Creating a state in react using JSON data and axios

I find myself a bit puzzled on how to proceed. There is this JSON file named posts.json that I am working with. [ { "id": 1, "title": "Kylie Jenner", "content": "Social Media Public Figure", ...

Ways to focus on a specific div using JavaScript

I am attempting to create a 'snow' effect on the background of a particular div with a red border. Here is the code, but you can also view it here: <!-- language: lang-js --> var width = getWidth(); var height = getH ...

Oops! We encountered an issue with your request, resulting in a status code of 400. This error occurred while utilizing Axios with

Having trouble posting to a mongoDB database while creating a user authentication site using MERN stack. Despite trying multiple solutions, the issue persists with various error messages. Backend is configured using Node.js, Express.js, and MongoDB. Assist ...

Contrasting Firefox Experience on Mac and Windows

I'm experiencing some issues with a website that is displaying differently in Firefox on Windows compared to Mac. I did some research online to see if there are any known differences in CSS rendering between the two versions of Firefox, but it doesn&a ...

There appears to be an issue with the Material Ui modal not functioning properly within the full screen component

Currently, I am facing an issue with the react-full-screen node package as modals, popovers, and drawers are not functioning properly within my full screen component. Can anyone assist me in implementing a functional modal that works seamlessly with my fu ...

Incorporating Chakra UI, what is the best way to display a modal box upon page load?

Is there a way to display a modal box when the page loads in Chakra UI? I have been searching through Chakra's documentation but couldn't find any information on this. import { useDisclosure, Modal, ModalOverlay, ModalContent, ...

What is causing the issue with the "Inline-block" property in this CSS code?

Please review the CSS code provided below for styling instructions. /*rex is the container of ex,ex2,ex3*/ div.rex{ height:200px; border:0px; margin:60px auto; padding: 0; vertical-align:top; } div.ex{ width:34%; height:200px; background-color:#4f443a; ...

Issue encountered when attempting to invoke a reducer function for state modification

It seems like I'm overlooking something when trying to call a reducer function from redux using mapDispatchToProps, but the missing step eludes me. This is the code for the reducer: case "CHANGE_COMPLETED": return (state = { ...sta ...

The dynamic routing feature in React fails to function properly after the application is built or deployed

I'm facing an issue with getting a React route to function properly in the build version of my app or when deployed on Render. Here are the routes I have: <Route path="/" element={userID ? <Home /> : <Login />} /> <Route ...

Developing several sliders and ensuring they operate independently of each other

I am currently in the process of developing multiple sliders for a website that I am building. As I reach the halfway point, I have encountered a problem that has stumped me. With several sliders involved, I have successfully obtained the length or count ...

What is the best way to re-render a component immediately following an update in React?

As I attempt to change the color of a bar to green and then back to black, it seems like the latter color update is taking precedence in my code. const [color, setColor] = useState("black") const bubbleSort = async () => { const sleep = ms => ...

JavaScript form with radio buttons

I'm completely new to JavaScript and I'm attempting to create a basic script that will show one form when a radio button is checked, and another form when the second radio button is clicked (with no form displayed when neither is selected). I kno ...

How can I reduce the size of a Bootstrap 5 carousel that spans the entire screen?

As someone new to web development, I have found using bootstrap to be extremely helpful. However, I am currently struggling with creating a carousel for my website. My goal is to display some pictures in one corner of the page, but the carousel keeps str ...