SCSS Partials & Imports - Modularer Code
Partials und das Module System organisieren SCSS Code professionell.
Partials
Partials sind SCSS-Dateien mit _ Prefix:
scss/
├── _variables.scss
├── _mixins.scss
├── _buttons.scss
└── main.scss
Warum _?
- Werden nicht direkt zu CSS kompiliert
- Nur über @use/@import verwendbar
@use vs @import
❌ @import (Deprecated)
@import "variables";
@import "mixins";
@import "buttons";
// Problem: Alles global!
✅ @use (Modern)
@use "variables";
@use "mixins";
.button {
background: variables.$primary-color;
@include mixins.flex-center;
}
Vorteile:
- Namespaces
- Keine Duplikate
- Bessere Performance
Namespaces
// _colors.scss
$primary: blue;
$secondary: gray;
// main.scss
@use "colors";
.button {
background: colors.$primary;
}
Custom Namespace
@use "colors" as c;
.button {
background: c.$primary;
}
Kein Namespace
@use "colors" as *;
.button {
background: $primary; // Direkt zugänglich
}
File Organization
scss/
├── abstracts/
│ ├── _variables.scss
│ ├── _mixins.scss
│ ├── _functions.scss
│ └── _index.scss
├── base/
│ ├── _reset.scss
│ ├── _typography.scss
│ └── _index.scss
├── components/
│ ├── _buttons.scss
│ ├── _cards.scss
│ ├── _forms.scss
│ └── _index.scss
├── layout/
│ ├── _header.scss
│ ├── _footer.scss
│ ├── _grid.scss
│ └── _index.scss
├── pages/
│ ├── _home.scss
│ ├── _about.scss
│ └── _index.scss
└── main.scss
Index Files
// abstracts/_index.scss
@forward "variables";
@forward "mixins";
@forward "functions";
// main.scss
@use "abstracts"; // Lädt alle abstracts
.button {
background: abstracts.$primary;
}
Complete Project Structure
// abstracts/_variables.scss
$primary: #007bff;
$secondary: #6c757d;
// abstracts/_mixins.scss
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
// abstracts/_index.scss
@forward "variables";
@forward "mixins";
// components/_buttons.scss
@use "../abstracts" as *;
.btn {
padding: 10px 20px;
&-primary {
background: $primary;
}
&-center {
@include flex-center;
}
}
// main.scss
@use "abstracts";
@use "components/buttons";@forward
Reexport von Partials:
// _index.scss
@forward "variables";
@forward "mixins";
@forward "functions";
// main.scss
@use "abstracts"; // Alle abstracts verfügbar
Private Members
// _variables.scss
$public-color: blue;
$_private-color: gray; // _ macht private
// main.scss
@use "variables";
.text {
color: variables.$public-color; // ✅ OK
color: variables.$_private-color; // ❌ Error
}
📝 Quiz
Was ist der Hauptvorteil von @use gegenüber @import?
🎯
Zusammenfassung
Du hast gelernt:
- ✅ Partials mit
_Prefix - ✅ @use statt @import
- ✅ Namespaces für Organisation
- ✅ @forward für Reexports
- ✅ File Structure Best Practices
- ✅ Private Members mit
_
Key Takeaways:
- @use > @import
- Partials für Modularity
- Index Files mit @forward
- 7-1 Pattern für Structure
Viel Erfolg! 🚀
46% abgeschlossen
Lektion abgeschlossen!
Gut gemacht! 🎉
Du hast "SCSS Partials & Imports - Modularer Code" 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 Extend & Inheritance - DRY mit @extend
Lerne @extend für Style-Inheritance. Placeholder Selectors, Best Practices und wann Mixins besser sind.
Fortgeschrittenscss