SCSS Extend & Inheritance - DRY mit @extend
@extend ermöglicht Style-Inheritance zwischen Selektoren.
@extend Basics
.button {
padding: 10px 20px;
border: none;
border-radius: 4px;
}
.button-primary {
@extend .button;
background: blue;
color: white;
}
// Kompiliert zu:
// .button, .button-primary {
// padding: 10px 20px;
// border: none;
// border-radius: 4px;
// }
// .button-primary {
// background: blue;
// color: white;
// }
Placeholder Selectors
Placeholders mit % werden nicht kompiliert:
%button-base {
padding: 10px 20px;
border: none;
}
.btn-primary {
@extend %button-base;
background: blue;
}
.btn-secondary {
@extend %button-base;
background: gray;
}
// Kompiliert zu:
// .btn-primary, .btn-secondary {
// padding: 10px 20px;
// border: none;
// }
@extend vs @mixin
@extend (Shared Styles)
%card {
padding: 20px;
border: 1px solid gray;
}
.user-card {
@extend %card;
}
.product-card {
@extend %card;
}
// Output: Selektoren werden kombiniert
// .user-card, .product-card { padding: 20px; ... }
@mixin (Duplicated Styles)
@mixin card {
padding: 20px;
border: 1px solid gray;
}
.user-card {
@include card;
}
.product-card {
@include card;
}
// Output: Styles werden dupliziert
// .user-card { padding: 20px; ... }
// .product-card { padding: 20px; ... }
Wann was?
@extend:
- ✅ Statische gemeinsame Styles
- ✅ Keine Parameter nötig
- ✅ Kleinere CSS-Datei
@mixin:
- ✅ Mit Parametern
- ✅ Dynamische Styles
- ✅ Mit @content Blocks
- ✅ Besser für Theme-Wechsel
📝 Quiz
Wann sollte man @extend verwenden?
🎯
Zusammenfassung
Du hast gelernt:
- ✅ @extend für Style-Inheritance
- ✅ % Placeholders
- ✅ @extend vs @mixin
- ✅ Best Practices
Key Takeaways:
- @extend für statische Styles
- Mixins für dynamische Styles
- Placeholders mit %
- Vorsicht mit Specificity
Viel Erfolg! 🚀
54% abgeschlossen
Lektion abgeschlossen!
Gut gemacht! 🎉
Du hast "SCSS Extend & Inheritance - DRY mit @extend" abgeschlossen
Artikel bewerten
0.0 (0 Bewertungen)
Bitte einloggen um zu bewerten
Das könnte dich auch interessieren
SCSS Best Practices - Professioneller Workflow
Lerne die wichtigsten Best Practices für wartbaren, performanten SCSS Code. Do's and Don'ts für Production.
Fortgeschrittenscss
SCSS Control Directives - Logik im Stylesheet
Lerne @if, @for, @each und @while für dynamische Styles und Design Systems.
Fortgeschrittenscss
SCSS Final Project - Professional Landing Page
Baue eine vollständige Landing Page mit SCSS. Variables, Mixins, Responsive Design und moderne Patterns.
Fortgeschrittenscss