Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass .scss: Nesting and multiple classes?

Tags:

sass

I'm using Sass (.scss) for my current project.

Following example:

HTML

<div class="container desc">     <div class="hello">         Hello World     </div> </div> 

SCSS

.container {     background:red;     color:white;      .hello {         padding-left:50px;     } } 

This works great.

Can I handle multiple classes while using nested styles.

In the sample above I'm talking about this:

CSS

.container.desc {     background:blue; } 

In this case all div.container would normally be red but div.container.desc would be blue.

How can I nest this inside container with Sass?

like image 751
matt Avatar asked Jun 18 '12 14:06

matt


People also ask

How do I use SCSS with nested classes?

The first way to nest in SCSS is very simple. Nest a number of HTML elements within a single HTML element. Then in your SCSS simple write the parent selector class name as you would in CSS, then write the nested tags within the parent. Bitchin, you are nesting in SCSS!

How do I select multiple classes in Sass?

SASS/SCSS code to select multiple select classes in the same item. In SCSS, parent selector & symbol is used. This & will be resolved side by side after compilation for CSS.

How does nesting work in Sass?

Nesting is a shortcut to creating CSS rules. Nesting in SASS works as selector of multiple CSS and combine them within one another instead of writing different CSS lines just to be precise about the style that we want to add to an element, we just nest it up.

What is Sass selector nesting?

Nesting permalinkNestingRather than repeating the same selectors over and over again, you can write one style rules inside another. Sass will automatically combine the outer rule's selector with the inner rule's.


2 Answers

You can use the parent selector reference &, it will be replaced by the parent selector after compilation:

For your example:

.container {     background:red;     &.desc{        background:blue;     } }  /* compiles to: */ .container {     background: red; } .container.desc {     background: blue; } 

The & will completely resolve, so if your parent selector is nested itself, the nesting will be resolved before replacing the &.

This notation is most often used to write pseudo-elements and -classes:

.element{     &:hover{ ... }     &:nth-child(1){ ... } } 

However, you can place the & at virtually any position you like*, so the following is possible too:

.container {     background:red;     #id &{        background:blue;     } }  /* compiles to: */ .container {     background: red; } #id .container {     background: blue; } 

However be aware, that this somehow breaks your nesting structure and thus may increase the effort of finding a specific rule in your stylesheet.

*: No other characters than whitespaces are allowed in front of the &. So you cannot do a direct concatenation of selector+& - #id& would throw an error.

like image 119
Christoph Avatar answered Oct 14 '22 13:10

Christoph


If that is the case, I think you need to use a better way of creating a class name or a class name convention. For example, like you said you want the .container class to have different color according to a specific usage or appearance. You can do this:

SCSS

.container {   background: red;    &--desc {     background: blue;   }    // or you can do a more specific name   &--blue {     background: blue;   }    &--red {     background: red;   } } 

CSS

.container {   background: red; }  .container--desc {   background: blue; }  .container--blue {   background: blue; }  .container--red {   background: red; } 

The code above is based on BEM Methodology in class naming conventions. You can check this link: BEM — Block Element Modifier Methodology

like image 28
Murdock Helscream Avatar answered Oct 14 '22 12:10

Murdock Helscream