Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCSS: How to use two classes together with an ampersand?

I have some css rules to be applied for primary button, I did something like this:

.my-btn {
  &--primary {
     padding: 10px;
  }
}

Now I want same rules to be applied for a primary button with "sm" class, I can do something like this:

.my-btn {
  &--primary, &--primary.my-btn--sm  {
    padding: 10px;
  }
}

This is working for me. But I want to use "&" for "sm" as well, like this:

.my-btn {
  &--primary, &--primary.&--sm  {
    padding: 10px;
  }
}

Is it possible?

like image 291
durgesh.patle Avatar asked Sep 25 '18 06:09

durgesh.patle


People also ask

What does ampersand do in SCSS?

Explains the mostly common used ampersand symbol in SCSS In SCSS, the & is used to nest selectors. As one of the most extremely useful symbols, it can be a nice time-saver if you know how to use it.

Can you select two classes in CSS?

Multiple classes can be applied to a single element in HTML and they can be styled using CSS.

What is nesting in SCSS?

Nesting is combining of different logic structures. Using SASS, we can combine multiple CSS rules within one another. If you are using multiple selectors, then you can use one selector inside another to create compound selectors.


1 Answers

In this case, a solution could be use a variable:

.my-btn {
  $this: &;
  &--primary, &--primary#{$this}--sm  {
    padding: 10px;
  }
}

Or use interpolation syntax

.my-btn {
  &--primary, &--primary#{&}--sm  {
    padding: 10px;
  }
}
like image 185
ReSedano Avatar answered Oct 10 '22 07:10

ReSedano