Table of contents
1.
Introduction
2.
Placeholder Selectors Explanation
2.1.
Example
2.2.
Use of Placeholder Selector
3.
Referencing Parent Selector Explanation
3.1.
Example
3.2.
Use of Referencing Parent Selector
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Sass-Referencing Parent Selectors & Placeholder Selector

Author Naman Kukreja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When you want to style the particular element from the HTML part, you select that element with the help of selectors in CSS. So are there any special selectors in Sass that are not present in CSS?

The answer to the above question is yes, and we are here to discuss some of them. While moving further in the article, we will discuss referencing parent selector and placeholder selector.

Placeholder Selectors Explanation

In regular CSS, we use class and id selectors specified by '.' and '#' by these symbols, respectively. But Sass gives us an edge here as there are many more selectors, one of them being a placeholder.

We have to use the @extend directive with the percentage symbol '%' to work with placeholder selectors. To display the result in the CSS, we have to use @extend.

The general syntax of placeholder selector is :

@extend %(name_of_selector);

Example

Here we will see an example where we will change according to HTML file in scss file and see what the code reflected in the CSS file is.

Let’s look at the HTML code first:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Placeholder Selector</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Learning the working of Placeholder</h1>
    <p class="first" >Sass is very useful</p>
    <h2>Example of Sass</h2>
    <p class="second">Here is the example of using Sass</p>
    <h3>This is heading 3</h3>
    <p class="third">This is the HTML code</p>
</body>
</html>

 

Here we have three headings corresponding to the three p tags with classes first, second, and third, respectively.

Now we have written some code in scss file to style the HTML content now just look at that:

.first{
    color: rgb(55, 212, 212);
}
.second{
    @extend .first;
    font-size: large;
    font-family: 'Courier New', Courier, monospace;
}
.third{
    @extend .first;
    background-color: rgb(10, 73, 75);
    font-style: italic;
}

 

We have used the first-class style in the second and third classes using the @extend feature as it allows the selector to inherit the techniques of another selector.

Now we will see the CSS code compiled by browser:

.first, .second, .third{
    color: rgb(55, 212, 212);
}
.second{
   
    font-size: large;
    font-family: 'Courier New', Courier, monospace;
}
.third{
 
    background-color: rgb(10, 73, 75);
    font-style: italic;
}

Output

To know how the following code works follow the steps given below:

  • Save the above HTML code.
  • Now open the saved HTML file in the browser. It should look like this:

Use of Placeholder Selector

This selector is not included in the compilation of the sass file, and because of this, it is used to create the Sass library. As shown in the above example, we can predefine some templates and use them afterward by using the @extend method.

Referencing Parent Selector Explanation

It is another and crucial selector in Sass. As the name suggests, it is related to parents, which we will discuss here.

With the help of this selector, we can efficiently reuse the parent selector. We can write again in the parent selector if we forget to write something.

It can select the parent selector by using the & character. The position of the & symbol tells where the parent selector should be inserted.

Example

In this example, we will see how to use referencing parent selector in Sass. We will first see the HTML file and then the scss file corresponding to the styling done in the HTML file and, after that, the CSS file that will be rendered by the browser.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Refrencing Parent Selector</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
    <div class = "container">
        <h1>Learning the working of Parent Selector</h1>
        <a href = "https://www.codingninjas.com/"> www.codingninjas.com </a>
     </div>
</body>
</html>

 

In the above HTML file, we have a div tag and h1 type heading and an anchor tag redirecting to codingninjas.com.

Now we will see the corresponding styling code in the scss file:

a{
    font-size: 25px;
    color: blue;
&:hover{

 
    background-color: darkslategray;
}
}

 

Suppose you have styled the anchor tag but forget to style when hovering, so there is no need to write this again. We can reuse the parent by using & tag as we have done above.

Now we will see the CSS code that is compiled by the browser:

a{
    font-size: 25px;
    color: blue;
}
a:hover{
    background-color: darkslategray;
 
}

 

This is the CSS code here. It will make the anchor tag and hover separately.

Output

To know how the following code works, follow the steps below:

  • Save the above HTML code.
  • Now open the saved HTML file in the browser. It should look like this.

Use of Referencing Parent Selector

  1. Users can create new classes with suffixes or prefixes as the parent selector.
  2. Users can also create different pseudo-classes.

FAQs

  1. Write some key features about Sass.
    Some key features are stated below: 
    Language extensions such as variables, nesting, and mixins.
    Customizable and well-formatted output.
     
  2. Explain the use of the @extend directive in Sass?
    It is used by placeholder selectors as it allows a more straightforward way for the selector to inherit the styles.
     
  3. When can a user use the % placeholder in Sass?
    The user can use this when working with the extended styles.
     
  4. Can we use the parent again in the parent selector?
    Yes, we can reuse the parent using the & symbol.

Key Takeaways

In this article, we have learned about two of many selectors from Sass, i.e., The placeholder selector and the referencing parent selector, their uses with the examples.

If you are new to Sass and want to overview Sass, refer to this blog. You will get a complete overview of the topic with its types, working, and examples.

Live masterclass