How can you apply CSS styles based on a user's system preferences using media queries?

Instruction: Illustrate the method to style components based on the user's system preferences like dark mode.

Context: This question probes into the candidate's ability to use CSS for adapting the UI to match user system preferences, enhancing user experience.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

Use preference media features to adapt a usable default. prefers-color-scheme reflects the user-agent's reported preference, which commonly follows system settings but can also reflect browser choices. It does not tell the application why the user chose a theme.

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS example</title> <style> :root { color-scheme: light; --surface: #fff; --ink: #111; } body { background: var(--surface); color: var(--ink); font: 1rem/1.5 system-ui; } a {...

Related Questions