What Is the Sass Ampersand?
The ampersand (&) is a special selector in Sass that represents the parent selector in nested rules. Unlike standard nesting, which adds a descendant combinator (space) between selectors, the ampersand allows you to place the parent selector exactly where you need it—whether at the beginning, middle, or end of a compound selector.
Without the ampersand, nesting creates descendant selectors:
.parent {
.child {
color: blue;
}
}
// Compiles to: .parent .child { color: blue; }
With the ampersand, you control exactly how the parent is combined:
.parent {
&.child {
color: blue;
}
}
// Compiles to: .parent.child { color: blue; }
Why the Ampersand Matters
- Eliminates repetition - No need to retype parent selectors
- Enables BEM methodology - Clean modifier class syntax
- Controls specificity - Prevents overly specific selectors
- Improves organization - Keeps related styles together
- Supports complex patterns - Pseudo-classes, combinators, contexts
For teams building scalable front-end architectures, mastering the Sass ampersand is essential for maintaining clean, modular CSS. Combined with other CSS selector techniques, it forms the foundation of maintainable stylesheets.
Understanding these core advantages will transform how you write CSS
Cleaner Code
Eliminate repetitive parent selector typing and write more concise stylesheets
Better Organization
Keep related styles together while maintaining clear selector relationships
BEM Compatible
Implement Block Element Modifier patterns with natural, readable syntax
Full Control
Position the parent selector anywhere in your compound selectors
Basic Usage Patterns
Standard Nesting vs. Ampersand Nesting
Understanding the difference between implicit and explicit parent referencing is crucial.
// Standard nesting (implicit descendant)
.card {
.title {
font-size: 1.5rem;
}
}
// Output: .card .title { font-size: 1.5rem; }
// Ampersand nesting (explicit parent)
.card {
& .title {
font-size: 1.5rem;
}
}
// Output: .card .title { font-size: 1.5rem; }
Both produce the same output, but the ampersand makes your intent explicit.
When to Use the Ampersand
Use the ampersand when you need:
- Compound selectors - Element with multiple classes:
&.primary - Pseudo-classes -
:hover,:focus,:nth-child() - Pseudo-elements -
::before,::after,::first-line - Attribute selectors -
[disabled],[type="text"] - Combinators -
>,+,~ - Modifier patterns - BEM naming conventions
The ampersand works seamlessly with pseudo-classes to create interactive component states without verbose selector repetition.
1.button {2 // Compound selector3 &.primary { }4 5 // Pseudo-class6 &:hover { }7 &:focus { }8 9 // Pseudo-element10 &::before { }11 12 // Attribute selector13 &[disabled] { }14 15 // Child combinator16 & > .icon { }17 18 // Adjacent sibling19 & + .label { }20 21 // General sibling22 & ~ .hint { }23}Working with Pseudo-Classes and Pseudo-Elements
The ampersand shines when working with interactive states and decorative elements.
Interactive States
.button {
background: blue;
color: white;
&:hover {
background: darken(blue, 10%);
}
&:active {
background: darken(blue, 20%);
}
&:focus {
outline: 2px solid blue;
outline-offset: 2px;
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
}
Pseudo-Elements
.card {
position: relative;
&::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 4px;
background: linear-gradient(to right, blue, purple);
}
&::after {
content: "→";
margin-left: 0.5rem;
}
}
Complex State Combinations
.input {
border: 1px solid #ccc;
&:focus {
border-color: blue;
&::placeholder {
color: lighten(blue, 30%);
}
}
&:invalid:not(:focus) {
border-color: red;
}
}
BEM Methodology with the Ampersand
Block Element Modifier (BEM) is a popular naming convention that pairs perfectly with the Sass ampersand. The BEM methodology creates a clear, hierarchical structure for component-based styling, making your CSS more predictable and easier to maintain.
BEM Structure
- Block - The main component:
.card - Element - Part of the block:
.card__title,.card__image - Modifier - Variation of the block or element:
.card--featured,.card__title--highlighted
Ampersand Syntax for BEM
.card {
display: flex;
flex-direction: column;
&__title {
font-size: 1.5rem;
margin-bottom: 1rem;
&--highlighted {
color: blue;
font-weight: bold;
}
}
&__image {
width: 100%;
height: auto;
&--large {
height: 400px;
object-fit: cover;
}
}
&__content {
padding: 1rem;
&--compact {
padding: 0.5rem;
}
}
&--featured {
border: 2px solid blue;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
}
This produces clean, predictable class names:
.card__title.card__title--highlighted.card__image--large.card--featured
For a deeper dive into BEM naming conventions, explore our BEM 101 guide which covers additional patterns and real-world applications.
1.btn {2 padding: 0.75rem 1.5rem;3 border-radius: 4px;4 font-size: 1rem;5 cursor: pointer;6 transition: all 0.2s ease;7 8 &__icon {9 margin-right: 0.5rem;10 }11 12 &__label {13 font-weight: 500;14 }15 16 &--primary {17 background: blue;18 color: white;19 20 &:hover {21 background: darken(blue, 10%);22 }23 }24 25 &--secondary {26 background: transparent;27 color: blue;28 border: 1px solid blue;29 }30 31 &--small {32 padding: 0.5rem 1rem;33 font-size: 0.875rem;34 }35 36 &--large {37 padding: 1rem 2rem;38 font-size: 1.125rem;39 }40 41 &:disabled {42 opacity: 0.5;43 cursor: not-allowed;44 }45}Using Combinators with the Ampersand
The ampersand works with all CSS combinators for powerful selector control.
Child Combinator (>)
.list {
display: flex;
flex-direction: column;
gap: 0.5rem;
& > .item {
padding: 0.75rem;
background: #f5f5f5;
&:hover {
background: #e0e0e0;
}
}
& > &__header {
font-weight: bold;
text-transform: uppercase;
font-size: 0.75rem;
color: #666;
}
}
Adjacent Sibling Combinator (+)
.form-group {
margin-bottom: 1rem;
& + & {
margin-top: 1.5rem;
}
& + &--highlighted {
margin-top: 2rem;
padding-top: 1rem;
border-top: 2px solid blue;
}
}
General Sibling Combinator (~)
.checkbox {
&.checked ~ .label {
color: blue;
}
&:not(:checked) ~ .description {
opacity: 0.5;
}
}
Contextual Styling and Qualifying Selectors
The ampersand allows powerful contextual styling by positioning the parent selector anywhere.
Theme-Based Styling
.card {
background: white;
.dark-theme & {
background: #1a1a1a;
color: #f5f5f5;
}
.light-theme & {
background: white;
color: #333;
}
}
Responsive Modifiers
.component {
padding: 1rem;
@media (min-width: 768px) {
& {
padding: 2rem;
}
}
@media (max-width: 767px) {
& {
padding: 0.5rem;
}
}
}
Context Classes
.sidebar {
width: 250px;
.compact-view & {
width: 180px;
.widget {
padding: 0.5rem;
}
}
.extended-view & {
width: 320px;
.widget {
padding: 1.5rem;
}
}
}
Advanced Patterns
The @at-root Directive
The @at-root directive escapes the nesting hierarchy, placing rules at the root level.
.parent {
color: blue;
.child {
color: red;
@at-root .sibling {
color: green;
}
}
}
// Compiled CSS
.parent { color: blue; }
.parent .child { color: red; }
.sibling { color: green; }
Combining @at-root with &
.component {
&__element {
color: blue;
@at-root &--modifier {
color: red;
}
}
}
// Compiled CSS
.component__element { color: blue; }
.component__element--modifier { color: red; }
Specificity Manipulation
.button {
background: blue;
// Double the specificity without !important
&& {
background: darken(blue, 10%);
}
}
// Compiled CSS
.button { background: blue; }
.button.button { background: #0000cc; }
Using SassScript with &
@mixin when-inside($selector) {
@at-root #{$selector} & {
@content;
}
}
.card {
background: white;
@include when-inside(".dark-theme") {
background: #333;
}
@include when-inside(".sidebar") {
background: #f5f5f5;
}
}
Vanilla CSS Nesting vs. Sass Ampersand
Modern CSS supports native nesting, but the behavior differs from Sass.
Key Differences
| Feature | Sass & | Vanilla CSS & |
|---|---|---|
| String concatenation | Supports suffix appending | Selector only |
| Parent representation | Fully compiled selector | Outer selector |
| Mixin integration | Works in SassScript | Not available |
| Preprocessing | Compiles to CSS | Native browser support |
Sass SCSS - Works with Suffix
.card {
&__title { }
&--featured { }
}
// Output: .card__title { }
// Output: .card--featured { }
Vanilla CSS - Different Approach
/* Vanilla CSS */
.card {
&__title { }
}
.card--featured { }
/* Must be separate rule for modifiers */
Modern Workflow Consideration
For projects transitioning to vanilla CSS nesting:
// Keep Sass for suffix power
.card {
&__title {
font-size: 1.5rem;
}
&--featured {
border: 2px solid blue;
}
}
| Feature | Sass & | Vanilla CSS & |
|---|---|---|
| Suffix appending | Yes (&__element) | No |
| Parent selector | Fully compiled | Outer selector only |
| Mixin integration | Full support | Not available |
| Interpolation | With #{} | Not needed |
| Browser support | Requires compilation | Native support |
| @at-root | Available | Not applicable |
| SassScript | Works with & | Not applicable |
Best Practices
When to Use the Ampersand
- Pseudo-classes and pseudo-elements - Always use
&:hover,&::before - Modifier classes -
&-modifier,&--variant - Compound selectors -
&.active,&[disabled] - Combinators -
& > .child,& + .sibling - Context modifications -
.dark-theme &
When to Avoid the Ampersand
- Simple descendant selectors -
nav a { }is clearer - Deeply nested rules - Keep nesting to 2-3 levels maximum
- Unnecessary abstraction - Don't over-engineer simple selectors
Maintaining Readability
// Good - clear and organized
.card {
&__header { }
&__body {
padding: 1rem;
&--compact {
padding: 0.5rem;
}
}
&__footer {
display: flex;
& .btn {
margin-left: auto;
}
}
}
// Avoid - too deeply nested
.card {
& & & & &__title {
font-size: 1.5rem;
}
}
Frequently Asked Questions
Does the ampersand affect CSS performance?
No. The ampersand is resolved at compile time—it has no runtime performance impact. The compiled CSS is identical whether you use & or write it manually.
Can I use & with CSS custom properties?
Yes! The ampersand works seamlessly with CSS custom properties. Use it to create scoped theme variables within component blocks.
What's the difference between & and no & in nesting?
Without &, Sass adds a descendant combinator (space) automatically. With &, you control exactly how the parent is combined with child selectors.
Should I use BEM with Sass?
Absolutely! BEM and Sass are a perfect match. The ampersand makes BEM modifiers natural to write while keeping related styles organized.
How deep should I nest my selectors?
Aim for 2-3 levels maximum. Deeper nesting creates overly specific selectors that are harder to override and maintain.
Is native CSS nesting replacing Sass?
Native CSS nesting is great, but Sass still offers advantages like mixins, functions, and the string concatenation pattern with & that's not available in vanilla CSS.
Conclusion
The Sass ampersand is a powerful tool that, when used correctly, significantly improves the organization, maintainability, and readability of your stylesheets. By understanding its behavior—from basic pseudo-class handling to advanced patterns like @at-root and BEM methodology—you can write cleaner CSS that's easier to maintain and scale.
Key Takeaways
- Use
&explicitly to clarify your selector intent - Leverage the ampersand for BEM-style component architecture
- Combine with pseudo-classes, pseudo-elements, and combinators
- Use
@at-rootwhen you need to escape nesting - Keep nesting shallow for maintainable code
- Consider vanilla CSS nesting for future compatibility
Mastering the ampersand is essential for any developer working with Sass, enabling you to write more elegant and maintainable stylesheets while maintaining full control over your CSS output.
Ready to implement these techniques in your projects? Our web development team specializes in building scalable CSS architectures using modern preprocessor techniques and industry best practices.