- Sass Tutorial
- Sass - Home
- Sass - Overview
- Sass - Installation
- Sass - Syntax
- Using Sass
- Sass - CSS Extensions
- Sass - Comments
- Sass - Script
- Sass - @-Rules and Directives
- Control Directives & Expressions
- Sass - Mixin Directives
- Sass - Function Directives
- Sass - Output Style
- Extending Sass
- Sass Useful Resources
- Sass - Interview Questions
- Sass - Quick Guide
- Sass - Useful Resources
- Sass - Discussion
Sass - Referencing Parent Selectors
Description
You can select the parent selector by using the & character. It tells where the parent selector should be inserted.
Example
The following example describes the use of parent selectors in the SCSS file −
<html> <head> <title>Referencing Parent Selectors</title> <link rel = "stylesheet" type = "text/css" href = "style.css" /> <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </head> <body> <div class = "container"> <h1>Example using Parent Selector</h1> <a href = "http://www.tutorialspoint.com/"> www.tutorialspoint.com </a> </div> </body> </html>
Next, create the file style.scss. Note the use of & character, which specifies where the parent selector should be inserted.
style.scss
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 −
style.css
a { font-size: 20px; } a:hover { background-color: yellow; }
Output
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.