Project: Dark and Light Theme
Define colours as variables on :root, override them for a dark class or prefers-color-scheme and use the variables everywhere. A button toggles the class.
Example
<style>
:root { --bg: #fff; --fg: #0f172a } .dark { --bg: #0f172a; --fg: #f1f5f9 } .box { background: var(--bg); color: var(--fg); padding: 12px; border: 1px solid #64748b }
</style>
<div class="box" id="b">Click the button<br><button onclick="b.classList.toggle('dark')">Toggle theme</button></div>Exercise
Dark preference query.
@media (prefers-color-scheme: )
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.