light-dark()
Recap
h1 {
color: green;
}
h2 {
color: blue;
}
p {
color: red;
}h1 {
color: #bada55;
}
h2 {
color: #ff2291;
}
p {
color: #5000ca;
}???
???
???
h1 {
/* = Primary */
color: #bada55;
}
h2 {
/* = Secondary Color */
color: #ff2291;
}
p {
/* = Tertiary/Accent Color */
color: #5000ca;
}h1 {
/* = Primary/Brand Color */
color:
}
h2 {
/* = Secondary Color */
color:
}
p {
/* = Tertiary/Accent Color */
color:
}primarysecondarytertiaryvar(-- );h1 {
/* = Primary/Brand Color */
color:
}
h2 {
/* = Secondary Color */
color:
}
p {
/* = Tertiary/Accent Color */
color:
}primarysecondarytertiaryvar(-- );html {
--primary: #bada55;
}
h1 {
color:
}primaryvar(-- );html {
--primary: 20;
}
h1 {
color:
}???
primaryvar(-- );html {
--primary: 20;
}
h1 {
color:
}primaryvar(-- );h1 {
color: var(--primary);
}
h2 {
color: var(--secondary);
}
p {
color: var(--tertiary);
}h1 {
color: var(--primary);
margin-bottom: 1rem;
}
h2 {
color: var(--secondary);
margin-bottom: 1rem;
}
p {
color: var(--tertiary);
margin-block: 1rem;
}html {
--primary: #bada55;
--secondary: #ff2291;
--tertiary: #5000ca;
--space-sm: 1rem;
}html {
--primary: #bada55;
--secondary: #ff2291;
--tertiary: #5000ca;
--space-sm: 1rem;
}h1 {
color: var(--primary);
margin-bottom: var(--space-sm);
}
h2 {
color: var(--secondary);
margin-bottom: var(--space-sm);
}
p {
color: var(--tertiary);
margin-block: var(--space-sm);
}html {
--primary: #bada55;
--secondary: #ff2291;
--tertiary: #5000ca;
--space-sm: 1rem;
}"Egenskaber, jeg selv har fundet på"
html {
--primary-color: #bada55;
}h1 {
color: var(--primary-color);
}--dit-variabel-navn: [værdi];--dit-variabel-navnhtml {
--primary-color: #bada55;
}h1 {
color: var(--primary-color);
}--dit-variabel-navn: [værdi];[property]: var( );--dit-variabel-navnmed var()-funktion
html {
--primary: #5000ca;
--secondary: #463866;
--accent: #fb28a8;
--space-xxs: 0.25rem;
--space-xs: 0.5rem;
--space-sm: 1rem;
--space-md: 1.5rem;
--space-lg: 2rem;
--space-xl: 3rem;
--space-xxl: 6rem;
}html {
--my-color: green;
}
aside {
--my-color: red;
}
h2 {
color: var(--my-color);
}<html>
<body>
<h2>Jeg er green</h2>
<aside>
<h2>Jeg er red</h2>
</aside>
</body>
</html><html>
<body>
<h2>Jeg er green</h2>
<aside>
<h2>Jeg er red</h2>
</aside>
</body>
</html>html {
--my-color: green;
}
aside {
--my-color: red;
}
h2 {
color: var(--my-color);
}Differentiér mellem lyst og mørkt tema i CSS
To enable support for the light-dark() color function, the color-scheme must have a value of light dark, usually set on the :root pseudo-class.
:root
= html
html /* eller :root */ {
color-scheme: light dark;
}Aktivering af light-dark-funktionen
:root
med custom properties
html /* eller :root */ {
color-scheme: light dark;
--color-brand:
}Opsætning af custom properties
:root
Light mode
#5000ca;light-dark( , )med custom properties
html /* eller :root */ {
color-scheme: light dark;
--color-brand:
}Opsætning af custom properties
:root
light-dark( , )#5000ca;#e0d3ffLight mode
Dark mode
med custom properties
html /* eller :root */ {
color-scheme: light dark;
--color-brand: light-dark(#5000ca, #e0d3ff);
--background-primary: light-dark(#eee, #1b1b1b);
}:root
med custom properties
html {
background: var(--background-primary);
}
h1 {
color: var(--color-brand);
}:root
med JavaScript
html {
color-scheme: light;
}
html.dark {
color-scheme: dark;
}'invalid''submit'const html = document.querySelector("html");
const btn = document.querySelector("#theme-btn");
function toggleTheme() {
html.classList.toggle('dark');
}
btn.addEventListener('click', toggleTheme);Brug classList.toggle()
Hvis slukket -> tænd
Hvis tændt -> sluk
classList
classList
html.
element
const html = document.querySelector("html");classList
.add
("dark")
.remove
html.
klasse
NB! uden punktum!
classList
.add
("dark")
.remove
html.
Tilføj
klasse
classList
.add
("dark")
.remove
.contains
html.
Fjern klasse
classList
.add
("dark")
.remove
.contains
html.
Indeholder klasse
.toggle
classList
("dark")
.remove
.toggle
html.
Tænd/sluk klasse
.contains
const html = document.querySelector("html");
const btn = document.querySelector("#theme-btn");
function toggleTheme() {
html.classList.toggle('dark');
}
btn.addEventListener('click', toggleTheme);html {
color-scheme: light;
--color-brand: light-dark(#5000ca, #e0d3ff);
--background-primary: light-dark(#eee, #1b1b1b);
}
html.dark {
color-scheme: dark;
}øvelse
Brug light-dark() til at differentiere mellem farve-værdier, og sæt color-scheme for hhv. html og html.dark. Brug JavaScript til at tænde/slukke dark mode.
color-scheme
.classList.toggle()
Eksempel med radio-knapper
Eksempel med checkbox (forklædt som switch)