Skip to main content

Sass: Referencing Parent Selectors

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

Example

Example of parent selectors in a 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.example.com/"> www.example.com </a>
</div>
</body>
</html>

and the style.scss file:

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

Then you tell Sass to watch the file and update the CSS when the file changes:

sass --watch my\path\style.scss:style.css

The generated style.css files is as shown below:

a {
font-size: 20px;
}

a:hover {
background-color: yellow;
}

Table of Contents