Yogesh Chauhan's Blog

Ampersand (Parent Selector) in SCSS (Sass)

in SCSS on April 26, 2021

The parent selector is nothing but an ampersand symbol (&).

The parent selector (&) is used to refer to the outer selector when you nest selectors.

I love it! It makes it so much easier to reuse the outer selector and helps to create more complex styles by following a simple syntax.

For e.g. take a look at this SCSS:


.alert {
  &:hover {
    font-weight: bold;
  }
  &.second-class{
    font-size: 20px;
  }
}

Here’s the compiled CSS:


.alert:hover {
  font-weight: bold;
}
.alert.second-class {
  font-size: 20px;
}

Those are just two different elements/selectors style but can you imagine doing that for tons of nested elements?!

You can refer to the outer selector after as well. Like this:


.alert {
  :not(&) {
    opacity: 0.8;
  }
}

Here’s the compiled CSS:


:not(.alert) {
  opacity: 0.8;
}

If you don’t leave a space or add a bracket, you might experience weird behavior.

Here’s the GitHub issue page related to that.


Add Suffixes

Use the parent selector, you can add additional suffixes to the outer selector, as long as the outer selector ends with an alphanumeric name.

Like this:


.accordion {
  max-width: 600px;
  margin: 4rem auto;
  width: 90%;
  font-family: "Raleway", sans-serif;
  background: #f4f4f4;

  &__copy {
    display: none;
    padding: 1rem 1.5rem 2rem 1.5rem;
    color: gray;
    line-height: 1.6;
    font-size: 14px;
    font-weight: 500;

    &--open {
      display: block;
    }
  }
}

Compiled CSS will be this:


.accordion {
  max-width: 600px;
  margin: 4rem auto;
  width: 90%;
  font-family: "Raleway", sans-serif;
  background: #f4f4f4;
}
.accordion__copy {
  display: none;
  padding: 1rem 1.5rem 2rem 1.5rem;
  color: gray;
  line-height: 1.6;
  font-size: 14px;
  font-weight: 500;
}
.accordion__copy--open {
  display: block;
}


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to switch dark and light themes using pure CSS?CSSHow to remove the Gutenberg Block Library CSS from WordPress?WordPressHow to create a dynamic countdown using HTML and JavaScript?HTML5 Steps to Create a Line using Canvas Tag in HTML5HTMLHow to create a flip effect with CSS?CSSAggregate Functions Examples in Postgres for BeginnersPostgres