|
| 1 | +--- |
| 2 | +theme: seriph |
| 3 | +routerMode: hash |
| 4 | +title: CSS-Variablen & Theming |
| 5 | +info: | |
| 6 | + ## CSS-Variablen & Theming |
| 7 | +background: https://github.com/HTL-Wolfsberg-Betriebsinformatik/skript/blob/main/slides/content/slides/background-cover-16-9.webp?raw=true |
| 8 | +class: text-center |
| 9 | +drawings: |
| 10 | + persist: false |
| 11 | +transition: slide-left |
| 12 | +mdc: true |
| 13 | +layout: cover |
| 14 | +hideInToc: true |
| 15 | +download: true |
| 16 | +export: |
| 17 | + format: pdf |
| 18 | + dark: false |
| 19 | + withClicks: true |
| 20 | + withToc: true |
| 21 | +selectable: true |
| 22 | +--- |
| 23 | + |
| 24 | +# CSS-Variablen & Theming |
| 25 | +## Moderne, flexible und wartbare Gestaltung |
| 26 | + |
| 27 | +--- |
| 28 | +hideInToc: true |
| 29 | +--- |
| 30 | + |
| 31 | +# Inhalt |
| 32 | + |
| 33 | +<Toc minDepth="1" maxDepth="1" columns="1" /> |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +# Was sind CSS-Variablen? |
| 38 | + |
| 39 | +- Benutzerdefinierte Werte, die überall im CSS genutzt werden können |
| 40 | +- Werden mit `--` und Variablenname definiert (e.g. `--my-favorite-color`) |
| 41 | +- Werden mit `var(--my-favorite-color)` verwendet |
| 42 | +- Können vererbt werden (inheritance) |
| 43 | + |
| 44 | +**Beispiel:** |
| 45 | + |
| 46 | +```css |
| 47 | +:root { |
| 48 | + --primary-color: #4e8cff; |
| 49 | + --padding: 1rem; |
| 50 | +} |
| 51 | + |
| 52 | +button { |
| 53 | + background: var(--primary-color); |
| 54 | + padding: var(--padding); |
| 55 | +} |
| 56 | +``` |
| 57 | +--- |
| 58 | + |
| 59 | +# Warum CSS-Variablen nutzen? |
| 60 | + |
| 61 | +<br> |
| 62 | + |
| 63 | +✔ Wiederverwendbare Werte |
| 64 | + |
| 65 | +✔ Einfaches globales Styling |
| 66 | + |
| 67 | +✔ Dunkel/Hell-Themes |
| 68 | + |
| 69 | +✔ Weniger Wiederholungen → sauberer Code |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +# Syntax |
| 74 | +*** |
| 75 | + |
| 76 | +**Variablen definieren:** |
| 77 | + |
| 78 | +```css |
| 79 | +:root { |
| 80 | + --bg: white; |
| 81 | + --fg: black; |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +- `:root` = höchste Ebene (`html`-Element) → ideal für **globale** Variablen |
| 86 | +- Variablen beginnen **immer mit zwei Bindestrichen** |
| 87 | + |
| 88 | +<br> |
| 89 | + |
| 90 | +**Variablen verwenden:** |
| 91 | + |
| 92 | +```css |
| 93 | +body { |
| 94 | + background-color: var(--bg); |
| 95 | + color: var(--fg, red); /* mit Fallback */ |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | +layout: two-cols |
| 101 | +layoutClass: gap-16 |
| 102 | +--- |
| 103 | + |
| 104 | +# Theming: Dark vs. Light Mode |
| 105 | + |
| 106 | +```css |
| 107 | +:root { |
| 108 | + --bg: white; |
| 109 | + --text: black; |
| 110 | +} |
| 111 | + |
| 112 | +.dark { |
| 113 | + --bg: #1e1e1e; |
| 114 | + --text: #fafafa; |
| 115 | +} |
| 116 | + |
| 117 | +body { |
| 118 | + background: var(--bg); |
| 119 | + color: var(--text); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +Aufruf: |
| 124 | +```html |
| 125 | +<body class="dark"> |
| 126 | + <p>This is a text in dark mode</p> |
| 127 | +</body> |
| 128 | +``` |
| 129 | + |
| 130 | +::right:: |
| 131 | + |
| 132 | +<style> |
| 133 | +:root { |
| 134 | + --bg: white; |
| 135 | + --text: black; |
| 136 | +} |
| 137 | + |
| 138 | +.dark { |
| 139 | + --bg: #1e1e1e; |
| 140 | + --text: #fafafa; |
| 141 | +} |
| 142 | + |
| 143 | +div { |
| 144 | + background: var(--bg) !important; |
| 145 | + color: var(--text) !important; |
| 146 | + height: 100%; |
| 147 | +} |
| 148 | +</style> |
| 149 | +<div class="dark"> |
| 150 | + <p>This is a text in dark mode</p> |
| 151 | +</div> |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +# Automatischer Dark Mode |
| 156 | + |
| 157 | +Wechselt je nach Betriebssystem-Theme. |
| 158 | + |
| 159 | +```css |
| 160 | +:root { |
| 161 | + --bg: white; |
| 162 | + --text: black; |
| 163 | +} |
| 164 | + |
| 165 | +@media (prefers-color-scheme: dark) { |
| 166 | + :root { |
| 167 | + --bg: #121212; |
| 168 | + --text: #eeeeee; |
| 169 | + } |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +# Typische Einsatzgebiete |
| 176 | + |
| 177 | +<br> |
| 178 | + |
| 179 | +🎨 Farben (`--primary`, `--bg`, `--text`) |
| 180 | + |
| 181 | +📏 Abstände (`--padding`, `--gap`, `--space-xl`) |
| 182 | + |
| 183 | +🔠 Typografie (`--font-size`, `--line-height`, `--font-main`) |
| 184 | + |
| 185 | +🧱 Layout (`--width`, `--max-width`, `--radius`) |
| 186 | + |
| 187 | +🌈 Themes (Dark/Light/Branding) |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +# Gruppieren von Variablen nach Kategorie |
| 192 | + |
| 193 | +Einer der häufigsten Methoden. |
| 194 | + |
| 195 | +```css |
| 196 | +:root { |
| 197 | + /* 🎨 Farben */ |
| 198 | + --color-primary: #4e8cff; |
| 199 | + --color-secondary: #ffb84e; |
| 200 | + --color-bg: #ffffff; |
| 201 | + --color-text: #1a1a1a; |
| 202 | + |
| 203 | + /* 🔠 Typografie */ |
| 204 | + --font-size-base: 16px; |
| 205 | + --font-size-lg: 20px; |
| 206 | + --font-weight-bold: 700; |
| 207 | + |
| 208 | + /* 📏 Abstände */ |
| 209 | + --space-xs: 4px; |
| 210 | + --space-sm: 8px; |
| 211 | + --space-md: 16px; |
| 212 | + --space-lg: 24px; |
| 213 | + |
| 214 | + /* 🧱 Layout */ |
| 215 | + --radius: 12px; |
| 216 | + --max-width: 800px; |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +--- |
| 221 | + |
| 222 | +# Gruppieren von Variablen nach Komponenten |
| 223 | + |
| 224 | +Sinnvoll, wenn du große Projekte hast. |
| 225 | + |
| 226 | +```css |
| 227 | +:root { |
| 228 | + /* Button Component */ |
| 229 | + --button-bg: #4e8cff; |
| 230 | + --button-color: white; |
| 231 | + --button-radius: 8px; |
| 232 | + --button-padding: 0.5rem 1rem; |
| 233 | + |
| 234 | + /* Card Component */ |
| 235 | + --card-bg: white; |
| 236 | + --card-shadow: 0 2px 6px rgba(0,0,0,0.15); |
| 237 | + --card-radius: 12px; |
| 238 | +} |
| 239 | + |
| 240 | +``` |
| 241 | + |
| 242 | +<br> |
| 243 | + |
| 244 | +**Vorteil:** Komponenten werden schnell anpassbar. |
| 245 | + |
| 246 | +--- |
| 247 | + |
| 248 | + |
| 249 | +# Best Practice |
| 250 | + |
| 251 | +- Verwende :root für globale Variablen |
| 252 | +- Gruppiere Variablen logisch |
| 253 | +- Benutze sprechende Namen statt Farben: |
| 254 | + - ❌ `--blue` |
| 255 | + - ✔ `--primary-color` |
| 256 | +- Nutze Variablen auch für Abstände |
| 257 | +- Vermeide *hardcoded* Werte in CSS Klassen |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | + |
0 commit comments