Creating Sub Components with Sass and BEM Mixin

Utilizing Sass 3.3 ".scss syntax" for implementing BEM nesting with a custom mixin:

@mixin e($name) {
  @at-root #{&}__#{$name} {
    @content;
  }
}

Applying the mixin in this manner:

.header {
  background:red;

  @include e(logo) {
    background:green;

    @include e(inner) {
      background:blue;
    }
  }
}

The resulting output is as follows:

.header {
  background:red;
}

  .header__logo {
    background:green;
}

    .header__logo__inner {
      background:blue;
    }

The desired output, however, should be:

.header {
  background:red;
}

  .header__logo {
    background:green;
}

    .header__logo-inner {
      background:blue;
    }

It can be observed that the __ convention changes to a - for the inner element.

An additional mixin could resolve this issue, but the objective is for the e mixin to handle it automatically. The tentative outline for modifying the e mixin is as follows:

@mixin e($name) {
  @if 'sub-component' {

    @at-root #{&}__#{$name} {
      @content;
    }

  } @else if 'sub-sub-component' {

    @at-root #{&}-#{$name} {
      @content;
    }

  }
}

Answer №1

Credit to Hugo Giraudel for inspiring this new mixin structure:

@mixin custom($name, $isSubElement: false) {
  @if $isSubElement == false {

    @at-root #{&}__#{$name} {
      @content;
    }

  } @else if $isSubElement == true {

    @at-root #{&}-#{$name} {
      @content;
    }

  }
}

To use this mixin, follow this format:

.navigation {
  background: #333;

  @include custom(link) {
    color: #fff;

    @include custom(sublink, true) {
      font-weight: bold;
    }
  }
}

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

Guide to creating a dynamic illumination using CSS, HTML, and JS

Can you help me create a unique lighting effect for a specific section of my webpage? I'm curious about the techniques needed to achieve a similar effect as seen on another webpage. Is it feasible to create this effect using only CSS and HTML, or are ...

Resolving the Issue of CSS Image Overlapping Text on Mobile Devices

I'm facing a CSS issue regarding the positioning of an image on small screens. The image is not responsive and I've tried using position:relative; and adjusting margins manually, but it hasn't resolved the problem. Below is my HTML Code: & ...

Problem with React-Bootstrap CSS styling

After importing Bootstrap CSS, I noticed that extra CSS is being applied to the navbar, as seen in the image below. Although Bootstrap cards are being used in the code, the issue is specifically with the Navbar. Interestingly, the problem disappears when I ...

Is the image loading promptly? Do we need to adjust the resolution?

Is there a way to decrease the loading time of images? For instance, if I have 20 identical images on my homepage (each sized at 1920x1080) This is the image: I want to crop the image to a fixed size. Here's the CSS code I am using: .startpage_im ...

``There seems to be an issue with the alignment and vertical placement in both Firefox and IE

I'm currently working on a responsive landing page for a company and running into an issue with the CTA button's position outside of Chrome. It's functioning perfectly in Chrome, resizing as intended: Here is the CSS responsible for this: ...

Complete Form Validation Issue Unresolved by Jquery Validation Plugin

I'm facing an issue with the jQuery validation plugin while attempting to validate a form. Strangely, it only works for one input field. Can anyone suggest a solution? <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.valid ...

Evenly distributing a row of images with consistent horizontal spacing

I have multiple rows of images set up like this: <div class="row"> <img src="image.jpg" alt=""> <img src="image.jpg" alt=""> <img src="image.jpg" alt=""> <img src="image.jpg" alt=""> <img src="image.jpg" alt=""&g ...

Tips for activating hover effect on child components by hovering over the parent container?

At the moment, I am tackling an issue with CSS in a nextJS web application. The problem lies in a parent div element that contains multiple child elements. My goal is for hovering over the parent div to trigger changes on the child elements as well. Here& ...

Issue with sticky positioning not functioning properly with overlapping scrolling effect

When the user scrolls, I want to create an overlapping effect using the 'sticky' position and assign a new background color to each div (section). However, despite setting 'top' to 0 for the 'sticky' position, the divs still s ...

Having trouble sending the information to Parse.com using the website

I am a beginner with the Parse database and I am currently working on implementing a code that allows users to sign up, with their information stored in the Parse database. However, I am encountering an issue where the values are not uploading as expected. ...

Creating a Cross-Fade Effect in easySlider 1.7 using the Jquery Plugin

Want to achieve a cross-fade effect in easySlider 1.7 Jquery Plugin? Check out this tutorial Easy Slider 1.7 for guidance. Appreciate any help, thanks! ...

Is it advisable to utilize media queries and transform: translateY(-%) to position content above the mobile keyboard?

I've been struggling for nearly a whole day and could really use some assistance. I'm having trouble understanding how chat web apps manage to work around this particular issue. There have been numerous forum posts discussing the problem: I am tr ...

When the parent DIV is hovered over, the image and text properties will be altered

Greetings, I am just starting out in web development and would like to seek guidance from the Stack Overflow community. Here is a link to my code: https://codepen.io/noxwon/pen/RwrmxKX My code is very basic and filled with repetitive lines. I have multip ...

Is CSS Floating Causing Issues in Internet Explorer?

Encountering an issue with older versions of IE. I have three left-floated divs above three floated divs, each in separate wrappers. The lower divs are overlapping the upper ones specifically on older versions of IE; however, it works fine on Safari, mobil ...

Building a Sleek Hover Effect Dropdown Menu Using HTML and CSS

I've been following a tutorial on YouTube that showcases how to create a hoverable dropdown menu using HTML and CSS. The video link can be found below: Youtube Video Tutorial: https://www.youtube.com/watch?v=9Qrs8p7WgCc After replicating the code an ...

Tips for tailoring content based on screen size

I am looking for a way to display different content depending on whether the user is viewing my website on a large screen (PC/tablet) or a small screen (mobile). While my site is responsive and uses bootstrap, I have a lot of content that is only suitable ...

What could be causing media queries to not update values even after being changed through JavaScript?

I have a simple navigation bar on my website, featuring links to different subpages. To enhance the user experience on mobile devices, I added a hamburger menu icon that is displayed on smaller screens. The links in the navigation bar are hidden using CSS ...

Full options in Bootstrap Selectpicker are not being displayed

I am currently facing an issue with a searchable list containing nearly 2700 items in a bootstrap selectpicker. When I scroll down the options, only around 50 or 60 are displayed, leaving the rest empty. You can view this problem in detail by checking out ...

I'm having trouble with my tablet view taking priority over my desktop view - what could be causing this issue?

Styling for different screen sizes: @media (min-width: 991px) and (max-width:768px){} .container, .container1, .container2 .container3{ float: left; } .container1{ width: 50%; } .container2{ width: 50%; } .container3{ width: 100%; } /**** ...

Modify the color of the text to be white

How can I modify the code below to change the text color to white? The original code is inspired by this question. I am unsure about how the text color was originally set to blue. .footer-background { padding-top: 20px; padding-bottom: 20px; bac ...