Open your site on a phone at night and it can feel like a flashlight: white background, dark text, full brightness, while every other app on the screen has already gone dark. Visitors do not set that preference inside your pages. They set it once, in their system settings, and expect websites to respect it. A single media query is how your CSS hears that preference and answers with a dark palette, with no settings page, no cookie and no JavaScript.
This guide covers the query itself, what it can and cannot do, and how it combines with the width and orientation blocks you generate in the Media Query Builder on Ovatly. One honesty note up front: the builder does not emit the dark mode condition. That line you write by hand. The builder earns its place by generating the size and orientation blocks that sit next to it.
Contents
- Quick answer
- What prefers-color-scheme actually reads
- The one line you write by hand
- Where the Media Query Builder fits
- How to target dark mode, step by step
- Worked example: dark styles for small screens only
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
To target dark mode, add one hand-written block to your stylesheet, @media (prefers-color-scheme: dark), and put your dark colors inside it. That is standard CSS, and it is worth stating plainly that no tool generates it for you: the Media Query Builder builds conditions for width, height, media type and orientation only, and color scheme is not among them. Where the builder does help is everything around that line. It generates and checks the breakpoint and orientation blocks, and those join the dark condition with the same and logic, so a rule can apply only to small screens in dark mode. Write the dark line yourself; let the builder handle the geometry.
What prefers-color-scheme actually reads
The query asks one question: has this visitor told their system they prefer a dark or a light scheme? The answer comes from the operating system setting, or from the browser's own theme setting where it has one. Dark, and the block matches; light, and it is skipped entirely.
Some systems switch on a schedule, dark after sunset and light in the morning, so the same visitor can get your dark design in the evening and your light design at noon. That is the query working as designed, not a bug.
What the query never does is offer a choice on your page. It reports a system preference; it stores nothing, and it cannot remember that one visitor wants your site dark while their system stays light. A per-site toggle is a different feature, built with JavaScript and storage, and out of scope for any media query.
The one line you write by hand
The block itself is short:
@media (prefers-color-scheme: dark) {
/* dark overrides go here */
}
Inside it, override the properties that carry color: backgrounds, text color, borders, link colors, and shadows that would disappear against a dark background. Leave layout alone: if a heading needs to be smaller on phones, that rule belongs in a width block, where it applies to both schemes equally.
There is a matching prefers-color-scheme: light value. Most sites never need it, because the base styles outside any block already are the light design.
Where the Media Query Builder fits
The builder assembles responsive breakpoint snippets. You start from a device preset or enter exact pixel widths and heights, add an orientation condition if you need one, choose a media type of screen or all, and it outputs a complete @media block with a placeholder comment inside, ready to paste into your stylesheet and fill with your CSS. Minimum and maximum width and height are in pixels; an empty field means no limit on that side. Conditions combine with and, so every condition must be true at once for the block to apply.
Two details are worth knowing. The preset widths are widely used conventions from common device sizes, not official standards, so treat them as a starting point rather than a rule. And the builder validates obvious mistakes, such as a minimum width larger than the maximum, before you copy anything. It runs fully in your browser; nothing is uploaded.
None of that includes a color-scheme condition, which is why the dark block above is hand-written. The connection is the and keyword: a builder block and the dark condition can join into one query, as the worked example shows.
How to target dark mode, step by step
- Decide your dark palette first: background, surface, text and link colors that keep the same contrast relationships as your light design.
- Write the
@media (prefers-color-scheme: dark)block by hand, after the base styles it overrides, and put the color overrides inside it. - Open the Media Query Builder for any size or orientation condition you also need. For a phones-only rule, set media type to screen and maximum width to 767px, and copy the block it produces.
- Combine the two: join the conditions on one line with
and, or nest one block inside the other. Every condition must be true at once for the styles to apply. - Test properly: switch your own operating system or browser to dark mode, reload, then switch back. Check both schemes at a narrow and a wide window before calling it done.
Worked example: dark styles for small screens only
Suppose most of your dark mode visitors arrive on phones, and you want the dark palette tightened there first. The builder gives you @media screen and (max-width: 767px) for the size half. Joining the hand-written dark condition gives:
@media screen and (max-width: 767px) and (prefers-color-scheme: dark) {
body {
background: #121212;
color: #e8e8e8;
}
}
Walk the conditions. A phone in dark mode matches all three, so the styles apply. A desktop in dark mode fails the width condition. A phone in light mode fails the scheme condition. That all-or-nothing behaviour is the and logic the builder uses for its own conditions, extended by one condition it cannot generate.
Limitations and honest tradeoffs
The builder cannot help with the dark condition itself; if you came here hoping a tool would write the whole dark mode feature, it will not. The query also gives visitors no choice on your site: it follows their system setting, including schedules.
Images do not adapt. Photographs stay exactly as bright, and a logo designed for a white background can vanish on a dark one unless you give it its own treatment. You also maintain two palettes from now on: every new component needs its colors checked in both schemes, or you will ship a half-dark page one release at a time.
Common mistakes
- Looking for a dark mode setting in the builder. There is none. Its conditions are width, height, media type and orientation; the scheme line is yours to write.
- Restyling layout inside the dark block. Change colors there. Layout changes belong in width blocks, or the two schemes drift into different pages.
- Treating the query as a toggle. It reads a system setting. A per-site choice needs a JavaScript switch, which this query is not.
- Designing dark first and bolting light on afterwards. Base styles are what light-scheme visitors get; keep them complete in their own right.
- Testing by resizing alone. A narrow window proves nothing about scheme. Switch the system or browser setting itself.
FAQs
Can the Media Query Builder generate a dark mode query? No. Its condition set is width, height, media type and orientation, with no color-scheme option. The dark mode block is one line you write by hand; the builder generates the size and orientation blocks that can sit alongside it.
What does prefers-color-scheme actually read? It reads the color scheme preference set in the visitor's operating system or browser. If that setting is dark, the block applies; if it is light, or set to follow a schedule that is currently in light hours, it does not.
Can I combine dark mode with a width condition? Yes. Conditions join with 'and', and every condition must be true at once. A block can require a dark scheme and a maximum width of 767px together, so it applies only to small screens in dark mode.
Does prefers-color-scheme give visitors a dark mode toggle on my site? No. It only reports the system setting. A toggle that lets a visitor choose per site, and remembers that choice, needs JavaScript and storage, which the media query cannot do.
Should the dark block replace my normal styles? No. Your base styles stay as the default design, and the dark block overrides the color-bearing properties inside it. Layout belongs in your width blocks, so both schemes share one structure.
Conclusion
Dark mode support is one honest line of CSS: @media (prefers-color-scheme: dark), written by hand, respecting a setting the visitor already made. The Media Query Builder still does the surrounding work, generating the width and orientation blocks that combine with that line through the same and logic. If a query of yours refuses to fire at all, work through our debugging checklist for a media query that is not working next. For more guides in this series, see our Website and Webmaster Tools guides.
References
- MDN Web Docs, on the prefers-color-scheme media feature: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme
- MDN Web Docs, on using media queries: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries