Sass - Referencing Parent Selectors



You can select the parent selector by using the & character. It tells where the parent selector should be inserted.


The following example describes the use of parent selectors in the SCSS file −

      <title>Referencing Parent Selectors</title>
      <link rel = "stylesheet" type = "text/css" href = "style.css" />
      <link rel = "stylesheet" href = "">
      <script src = ""></script>
      <script src = ""></script>

      <div class = "container">
         <h1>Example using Parent Selector</h1>
         <a href = ""> </a>

Next, create the file style.scss. Note the use of & character, which specifies where the parent selector should be inserted.


a {
   font-size: 20px;
   &:hover { background-color: yellow; }

You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command −

sass --watch C:\ruby\lib\sass\style.scss:style.css

Next, execute the above command; it will create the style.css file automatically with the following code −


a {
   font-size: 20px;
a:hover {
   background-color: yellow;


Let us carry out the following steps to see how the above given code works −

  • Save the above given html code in parent_selectors.html file.

  • Open this HTML file in a browser, an output is displayed as shown below..

  • Here & will be replaced with the parent selector a. When you hover on the link, it will display background color as yellow.

Sass CSS Extensions

E-Books Store