sass-site/source/documentation/style-rules/placeholder-selectors.liquid

67 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-05-16 13:26:56 +02:00
---
title: Placeholder Selectors
introduction: >
Sass has a special kind of selector known as a “placeholder”. It looks and
acts a lot like a class selector, but it starts with a `%` and it's not
included in the CSS output. In fact, any complex selector (the ones between
the commas) that even *contains* a placeholder selector isn't included in the
CSS, nor is any style rule whose selectors all contain placeholders.
---
2023-05-25 17:55:08 +02:00
2023-06-03 00:18:43 +02:00
{% render 'code_snippets/example-placeholder' %}
2023-05-16 13:26:56 +02:00
{% markdown %}
2023-05-31 18:16:59 +02:00
What's the use of a selector that isn't emitted? It can still be [extended][]!
Unlike class selectors, placeholders don't clutter up the CSS if they aren't
extended and they don't mandate that users of a library use specific class
names for their HTML.
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
[extended]: /documentation/at-rules/extend
2023-05-16 13:26:56 +02:00
{% endmarkdown %}
{% codeExample 'extended-selector' %}
2023-05-31 18:16:59 +02:00
%toolbelt {
box-sizing: border-box;
border-top: 1px rgba(#000, .12) solid;
padding: 16px 0;
width: 100%;
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
&:hover { border: 2px rgba(#000, .5) solid; }
}
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
.action-buttons {
@extend %toolbelt;
color: #4285f4;
}
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
.reset-buttons {
@extend %toolbelt;
color: #cddc39;
}
===
%toolbelt
box-sizing: border-box
border-top: 1px rgba(#000, .12) solid
padding: 16px 0
width: 100%
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
&:hover
border: 2px rgba(#000, .5) solid
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
.action-buttons
@extend %toolbelt
color: #4285f4
2023-05-16 13:26:56 +02:00
2023-05-31 18:16:59 +02:00
.reset-buttons
@extend %toolbelt
color: #cddc39
2023-05-16 13:26:56 +02:00
{% endcodeExample %}
{% markdown %}
2023-05-31 18:16:59 +02:00
Placeholder selectors are useful when writing a Sass library where each style
rule may or may not be used. As a rule of thumb, if you're writing a
stylesheet just for your own app, it's often better to just extend a class
selector if one is available.
2023-05-16 13:26:56 +02:00
{% endmarkdown %}