sass-site/source/documentation/style-rules/parent-selector.liquid

182 lines
5.3 KiB
Plaintext
Raw Normal View History

2023-05-16 13:26:56 +02:00
---
title: Parent Selector
introduction: >
The parent selector, `&`, is a special selector invented by Sass thats used
2023-05-25 17:55:08 +02:00
in [nested selectors](/documentation/style-rules#nesting) to refer to the
outer selector. It makes it possible to re-use the outer selector in more
complex ways, like adding a
2023-05-16 13:26:56 +02:00
[pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes)
or adding a selector *before* the parent.
---
2023-05-26 14:23:18 +02:00
{% markdown %}
2023-05-31 18:16:59 +02:00
When a parent selector is used in an inner selector, it's replaced with the
corresponding outer selector. This happens instead of the normal nesting
behavior.
2023-05-26 14:23:18 +02:00
{% endmarkdown %}
2023-05-16 13:26:56 +02:00
{% codeExample 'parent-selector' %}
2023-05-31 18:16:59 +02:00
.alert {
// The parent selector can be used to add pseudo-classes to the outer
// selector.
&:hover {
font-weight: bold;
}
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
// It can also be used to style the outer selector in a certain context, such
// as a body set to use a right-to-left language.
[dir=rtl] & {
margin-left: 0;
margin-right: 10px;
}
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
// You can even use it as an argument to pseudo-class selectors.
:not(&) {
opacity: 0.8;
}
2023-05-16 13:26:56 +02:00
}
2023-05-31 18:16:59 +02:00
===
.alert
// The parent selector can be used to add pseudo-classes to the outer
// selector.
&:hover
font-weight: bold
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
// It can also be used to style the outer selector in a certain context, such
// as a body set to use a right-to-left language.
[dir=rtl] &
margin-left: 0
margin-right: 10px
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
// You can even use it as an argument to pseudo-class selectors.
:not(&)
opacity: 0.8
2023-05-16 13:26:56 +02:00
{% endcodeExample %}
{% headsUp %}
2023-05-31 18:16:59 +02:00
Because the parent selector could be replaced by a type selector like `h1`,
it's only allowed at the beginning of compound selectors where a type selector
would also be allowed. For example, `span&` is not allowed.
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
We're looking into loosening this restriction, though. If you'd like to help
make that happen, check out [this GitHub issue][].
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[this GitHub issue]: https://github.com/sass/sass/issues/1425
2023-05-16 13:26:56 +02:00
{% endheadsUp %}
{% markdown %}
2023-05-31 18:16:59 +02:00
## Adding Suffixes
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
You can also use the parent selector to add extra suffixes to the outer
selector. This is particularly useful when using a methodology like [BEM][]
that uses highly structured class names. As long as the outer selector ends
with an alphanumeric name (like class, ID, and element selectors), you can use
the parent selector to append additional text.
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[BEM]: http://getbem.com/
2023-05-16 13:26:56 +02:00
{% endmarkdown %}
{% codeExample 'parent-selector-suffixes' %}
2023-05-31 18:16:59 +02:00
.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;
}
2023-05-16 13:26:56 +02:00
}
}
2023-05-31 18:16:59 +02:00
===
.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
2023-05-16 13:26:56 +02:00
{% endcodeExample %}
{% markdown %}
2023-05-31 18:16:59 +02:00
## In SassScript
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
The parent selector can also be used within SassScript. It's a special
expression that returns the current parent selector in the same format used by
[selector functions][]: a comma-separated list (the selector list) that
contains space-separated lists (the complex selectors) that contain unquoted
strings (the compound selectors).
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[selector functions]: /documentation/modules/selector#selector-values
2023-05-16 13:26:56 +02:00
{% endmarkdown %}
{% codeExample 'parent-selector-sassscript' %}
2023-05-31 18:16:59 +02:00
.main aside:hover,
.sidebar p {
parent-selector: &;
// => ((unquote(".main") unquote("aside:hover")),
// (unquote(".sidebar") unquote("p")))
}
===
.main aside:hover,
.sidebar p
parent-selector: &
// => ((unquote(".main") unquote("aside:hover")),
// (unquote(".sidebar") unquote("p")))
2023-05-16 13:26:56 +02:00
{% endcodeExample %}
{% markdown %}
2023-05-31 18:16:59 +02:00
If the `&` expression is used outside any style rules, it returns `null`.
Since `null` is [falsey][], this means you can easily use it to determine
whether a mixin is being called in a style rule or not.
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[falsey]: /documentation/at-rules/control/if#truthiness-and-falsiness
2023-05-16 13:26:56 +02:00
{% endmarkdown %}
2023-06-03 00:18:43 +02:00
{% render 'code_snippets/example-if-parent-selector' %}
2023-05-16 13:26:56 +02:00
{% markdown %}
2023-05-31 18:16:59 +02:00
### Advanced Nesting
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
You can use `&` as a normal SassScript expression, which means you can pass it
to functions or include it in interpolation—even in other selectors! Using it
in combination with [selector functions][] and the [`@at-root` rule][] allows
you to nest selectors in very powerful ways.
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[selector functions]: /documentation/modules/selector#selector-values
[`@at-root` rule]: /documentation/at-rules/at-root
2023-05-16 13:26:56 +02:00
{% endmarkdown %}
2023-06-03 00:18:43 +02:00
{% render 'code_snippets/example-advanced-nesting' %}
2023-05-16 13:26:56 +02:00
{% headsUp %}
2023-05-31 18:16:59 +02:00
When Sass is nesting selectors, it doesn't know what interpolation was used to
generate them. This means it will automatically add the outer selector to the
inner selector *even if* you used `&` as a SassScript expression. That's why
you need to explicitly use the [`@at-root` rule][] to tell Sass not to include
the outer selector.
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[`@at-root` rule]: /documentation/at-rules/at-root
2023-05-16 13:26:56 +02:00
{% endheadsUp %}