What if you could fix all your button styles by changing just one line of code?
Why Placeholder selectors with % in SASS? - Purpose & Use Cases
Imagine you are styling many buttons on a website. You want them all to share the same basic look, like color and padding, so you copy and paste the same CSS rules into each button style.
When you want to change the shared style, you have to update every single button style manually. This is slow, easy to forget, and can cause inconsistent designs.
Placeholder selectors with % let you write shared styles once and reuse them by extending. This means you keep your code clean and easy to update.
.btn-primary { color: white; padding: 1rem; }
.btn-secondary { color: white; padding: 1rem; }%button-base { color: white; padding: 1rem; }
.btn-primary { @extend %button-base; }
.btn-secondary { @extend %button-base; }You can create reusable style blocks that keep your CSS DRY (Don't Repeat Yourself) and easy to maintain.
On a website with many types of buttons, you can change the base button style in one place, and all buttons update automatically.
Copy-pasting styles is slow and error-prone.
Placeholder selectors let you write shared styles once.
Extending placeholders keeps your CSS clean and easy to update.
Practice
%?Solution
Step 1: Understand what placeholder selectors do
Placeholder selectors start with%and are used to hold styles that are not output directly but can be reused.Step 2: Identify their usage
They are extended by other selectors using@extendto apply the stored styles without duplicating CSS.Final Answer:
To hold reusable styles that can be extended by other selectors -> Option AQuick Check:
Placeholder selectors = reusable styles [OK]
- Thinking % creates HTML elements
- Confusing % with variables
- Using % for comments
%button-style inside a Sass rule?Solution
Step 1: Recall the syntax for extending placeholders
In Sass, to apply styles from a placeholder, you use@extend %placeholder-name;inside a selector.Step 2: Check each option
button { @extend %button-style; } uses@extend %button-style;correctly. The other options use incorrect keywords or syntax.Final Answer:
button { @extend %button-style; } -> Option CQuick Check:
@extend %placeholder-name; = correct syntax [OK]
- Using @include instead of @extend
- Omitting the @ symbol
- Writing %button-style without @extend
%alert { color: red; font-weight: bold; }
.warning { @extend %alert; background: yellow; }What CSS will be generated?Solution
Step 1: Understand placeholder output
Placeholder selectors do not output CSS themselves. Their styles are included only when extended.Step 2: Analyze the @extend usage
The.warningclass extends%alert, so it gets the styles from%alertplus its own background style.Final Answer:
.warning { color: red; font-weight: bold; background: yellow; } -> Option DQuick Check:
Placeholder styles appear only in extended selectors [OK]
- Expecting %alert to appear in CSS
- Thinking styles duplicate without extend
- Confusing @extend with @include
%card-style {
border: 1px solid black;
}
.card {
extend %card-style;
padding: 1rem;
}Solution
Step 1: Check the syntax of @extend
The correct syntax requires the@symbol beforeextendto work properly.Step 2: Identify the error in the code
The code usesextend %card-style;without@, causing a syntax error.Final Answer:
Missing @ symbol before extend -> Option BQuick Check:
@extend must have @ symbol [OK]
- Omitting @ before extend
- Misnaming placeholder selectors
- Misplacing properties inside placeholders
.primary-btn and .secondary-btn, sharing common styles but with different colors. Which Sass code correctly uses placeholder selectors to avoid repeating common styles?Solution
Step 1: Identify the correct use of placeholder selectors
Placeholder selectors start with%and hold common styles to be extended by other selectors.Step 2: Check each option for correct reuse and color overrides
%btn-common { padding: 1rem; border-radius: 0.5rem; } .primary-btn { @extend %btn-common; background-color: blue; color: white; } .secondary-btn { @extend %btn-common; background-color: gray; color: black; }correctly defines%btn-commonwith shared styles and extends it in both buttons, adding different colors separately..btn-common { padding: 1rem; border-radius: 0.5rem; } .primary-btn { @extend .btn-common; background-color: blue; color: white; } .secondary-btn { @extend .btn-common; background-color: gray; color: black; }incorrectly uses a class instead of a placeholder.%btn-common { padding: 1rem; border-radius: 0.5rem; background-color: blue; } .primary-btn { @extend %btn-common; color: white; } .secondary-btn { @extend %btn-common; background-color: gray; color: black; }mixes shared and specific colors in the placeholder, causing unwanted style inheritance.%btn-common { padding: 1rem; border-radius: 0.5rem; } .primary-btn { background-color: blue; color: white; } .secondary-btn { background-color: gray; color: black; }does not use placeholders at all, repeating styles.Final Answer:
%btn-common placeholder with @extend usage in both buttons -> Option AQuick Check:
Use %placeholder for shared styles, extend and override colors [OK]
- Using classes instead of placeholders for shared styles
- Putting unique styles inside the placeholder
- Not using @extend to reuse styles
Ready to prove it?
Pass the test and this pattern moves unknown
