Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass: Selecting the parent element with multiple nested selectors

Here's what I'm ultimately trying to do:

.books, .dvds, .magazines {
  article &.books {
    /* Wanting the selector to only be ".books article" */
  }
  article {
    /* Can apply to any of the `article` tags under .books, .dvds and .magazines */
  }
}

I've got some nested selectors and instead of breaking out a new .books article selector, I'd like to keep them nested, but still only target article elements under .books.

I did try this, and it works, but the output is .books.books article, which is redundant and makes me cringe:

.books, .dvds, .magazines {
    &.books article {
        /* Outputs ".books.books article, .dvds.books article, .magazines.books article"...boooo, hisssss */
    }
}
like image 619
Shpigford Avatar asked Oct 01 '12 11:10

Shpigford


1 Answers

What about something more like:

article {

  .books &,
  .dvds &,
  .magazines & {
    /* book, dvd, magazine shared stuff */
  }

  .books & {
    /* book stuff */
  }

}

compiles to:

.books article, .dvds article, .magazines article {
  /* book, dvd, magazine shared stuff */
}
.books article {
  /* book stuff */
}
like image 187
Jackie Avatar answered Oct 11 '22 13:10

Jackie