What is the best way to style an element with two classes in SCSS?

I've always understood that when targeting a div with two classes in SCSS, you should structure it like this:

HTML:

<div class="item active">...</div>

SCSS:

.item {
  &.active {
   /* add your code here */
  }
}

But what about when I want to target an element's after pseudo-element? In CSS, we would normally do something like this:

.item.active:after {
/* add your code here */
}

Any help would be greatly appreciated!

Answer №1

Here are a couple of ways to achieve this:

a. Use this method if you want to apply styles to the .active class as well:

.item {
  background: red;
  height: 100px;
  width: 100px;
  &.active {
    &:after{
      content: "aaa";
    }
  }
}

or

b. Use this method if you only want to add styles to the :after pseudo-element when the item has the class active:

.item {
  background: red;
  height: 100px;
  width: 100px;
  &.active:after{

      content: "aaa";

  }
}

Check out the jsFiddle link for a live example.

Answer №2

Experiment with:

.element {
   &.enabled {
     &:before {
     /* add your code here */
     } 
   }
 } 

Answer №3

.element {
    &.selected {
        /* add your code here */
        &:before{
            /* customize your code here */
        }
    }
}

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

the php renderer fails to take into account the styles

Trying to convey this as clearly as possible. I have the variables below: $name = main channel and $childs = sub channels. When I remove the childs from the HTML, only the main channel shows up - which is expected. However, when attempting to style the su ...

Nav bar breaching the Bootstrap threshold

I am new to using bootstrap and currently in the learning process. I am facing an issue with the navigation bar I created. All the contents are aligning to the right, but the search bar and tabs are breaking onto a new line. I have attempted various soluti ...

Guide to vertically aligning text in an overlay using CSS within Angular 2

I currently have an overlay on my page that displays a spinner (an Angular material component) and accompanying text. This overlay covers the entire page and prevents clicking on elements below it. The spinner is positioned in the center of the page, and ...

What is the method to reduce the gap between the label and field in Vuetify input controls?

Consider a custom Vuetify text field as an example (Playground) <template> <v-app> <v-container> <v-text-field label="label goes here" variant="plain" density="compact" ...

Pure CSS text slideshow

I'm currently working on developing a text content slideshow using only CSS. Here is the HTML/CSS code I have: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>CSS text slideshow examp ...

Is it possible to add a border to both the tbody and td

I currently have a table that is organized with tbody elements to group rows together. In order to create a grid-like structure, I applied borders to each individual td element within the tbody. However, I also desire to show that the tbodies themselves ar ...

Connecting a string array to the navigation bar

Need Assistance with AngularJS: In my controller, I have a string array that looks like this: var app = angular.module('myApp', []); app.controller('mycontroller', function($scope) { $scope.menuitems =['Home','About&apos ...

Implementing a Slide animation in React

I am currently working on a carousel feature that includes two buttons, Next and Previous. When the user clicks on Next, I want the carousel to slide from left to right, and when they click on Previous, it should slide from right to left. One important not ...

Shifting the size of a React element with animation

Within my React-based application, I am attempting to execute a basic CSS3 width transition with the following code: .foo { transition: width 1s ease-in-out; } My objective is to apply a style to an element in the React component which is "width: xx% ...

Manually controlling line breaks in HTML text

When collaborating with designers, they often have strong opinions about word wrapping in the final HTML page. If I am working on a fixed layout (non-responsive) and the designer is not satisfied with how the text wraps, there are several options available ...

Attempting to create a zoom-in effect on the background during hover

I attempted to create a hover effect on the image background and added an overlay using the "before" pseudo-element to later insert text on it. However, the overlay seems to be covering the container instead of the parent element (the Cimg1 class). When ...

The concealment of the container is ineffective when attempting to hide it until all cached images are

My website has a background and a main container. I wanted to hide the container until the entire page had loaded, so I included #cover{opacity:0} at the beginning of the page and $(window).load(function() { $('#cover').css('opacity&apo ...

How to efficiently embed one HTML file within another HTML file

Currently, I am working with a file called a.html and its accompanying CSS file, a_style.css, which is linked in the <head>. Now, I have another HTML file named b.html, with its own styles included within the <style> tag. The content of b.html ...

Internet Explorer 7 does not display icons

When I look at my icon, I'm calling it like this: <td> <i id="sthAdd" class="icn_plus"></i> </td> and the class looks like this: .icn_plus { background: url(../images/icons/plus-14x14.png) no-repeat; width: 20px; heig ...

Leveraging Angular to dynamically adjust the height of ng-if child elements based on parent

I'm struggling with a few things in my current setup. I have a view that consists of 3 states - intro, loading, and completed. My goal is to create a sliding animation from left to right as the user moves through these states. Here is the basic struc ...

Tips for creating a website with an integrated, easy-to-use editing tool

Recently, I designed a website for a friend who isn't well-versed in HTML/CSS/JavaScript. He specifically requested that the site be custom made instead of using a web creator. Now, he needs to have the ability to make changes to the site without nee ...

Inconsistency in naming of jQuery CSS properties

Can you explain the reason behind the difference in property names: $(elem).css('padding-top') compared to $(elem).css('marginTop') ? Wouldn't it make more sense if we use: $(elem).css('margin-top') instead? ...

Tips for horizontally aligning a modal with smooth transition effects using Material UI and ensuring it is responsive

Struggling to center a modal with transition using Material UI and facing challenges with responsiveness on small screens, particularly mobile devices. The modal looks good and is centered on smaller screens if no transition is used. However, when a trans ...

Unusual Happenings with jQuery Draggable

Currently, I am experimenting with jQuery draggable to move individual letters of the word "Hello" around on the page. However, I have come across a frustrating issue. When I drag the letter H towards the right and it gets near the letters E, L, L, or O, ...

Transform preexisting Vue UI library measurements into pixels

I was curious about converting all existing units (em / rem) to pixels. How can I easily convert the entire output units to px? Are there any tricks or methods available? Currently, I am utilizing Vuesax UI to create a plugin for various websites and temp ...