Home CSS Media Queries prefers-contrast Definition
Definition and Usage The prefers-contrast media feature detects whether a user has requested more contrast, less contrast, or a custom color palette .
Use it to strengthen boundaries, increase text contrast, or avoid overly stark palettes. It complements good default contrast; it does not excuse low-contrast base styles.
Baseline · Newly available. This preference query is designed for progressive enhancement. Browsers that do not support it keep the default styles.
Tip Not the same as forced colorsprefers-contrast reports a preference. forced-colors reports that the browser is actively substituting colors. A custom forced palette can cause custom to match.
Syntax
CSS Syntax @media (prefers-contrast:no-preference | more | less | custom ) { … }
Wrap the feature in parentheses and put the conditional CSS inside the media block.
Values
Feature Values Value Description Try it no-preferenceNo contrast preference has been expressed Try itmoreThe user prefers stronger contrast Try itlessThe user prefers lower contrast Try itcustomThe user uses a custom palette that does not map to more or less Try it
Approach
How to Use prefers-contrast Use the preference to enhance a solid, accessible default.
Build a readable default that already meets contrast guidance. Use more to strengthen text, borders, and focus indicators. Use less only to soften non-essential contrast without hiding content. Test custom and forced-color environments without relying on color alone. Piece Required? Role @mediaYes Wraps the preference query prefers-contrastYes Names the media feature Accessible defaults Yes Work when no query matches Real-device testing Recommended Confirms operating-system behavior
Try it
Live Preview Your browser evaluates each prefers-contrast value below through matchMedia.
Code
CSS Examples Each example keeps the default usable, then responds to prefers-contrast. Open Try It Yourself for a live editor.
1. More contrast 2. Less contrast 3. Custom palette Example 1 — Strengthen a Card Increase border and text contrast when more is requested.
.card {
color: #475569;
border: 1px solid #cbd5e1;
}
@media (prefers-contrast: more) {
.card {
color: #0f172a;
border: 3px solid currentColor;
}
} Text and boundary become stronger when more matches.
How It Works The default card is already readable. The query detects a request for more contrast. Text becomes darker and the border thicker. Using currentColor keeps the border coordinated. Example 2 — Soften a Decorative Surface Reduce harsh decoration while keeping text readable.
.notice {
color: #111827;
background: #ffffff;
border: 2px solid #111827;
}
@media (prefers-contrast: less) {
.notice {
border-color: #94a3b8;
background: #f8fafc;
}
} Decoration softens, while the text remains clearly readable.
How It Works The normal notice uses a strong boundary. The query detects a lower-contrast preference. Only decorative surface colors soften. Important text remains dark and legible. Example 3 — Handle a Custom Preference Avoid decorative effects when a custom contrast palette is active.
.button {
box-shadow: 0 4px 12px rgb(15 23 42 / 25%);
}
@media (prefers-contrast: custom) {
.button {
box-shadow: none;
outline: 2px solid currentColor;
}
} A solid outline replaces a color-dependent shadow.
How It Works The default uses a decorative shadow. The custom query covers user-defined palettes. A current-color outline survives palette changes. The control remains identifiable without the shadow. Edge Cases & Pitfalls Contrast preferences vary by platform, and support arrived later than other preference queries.
baseline Good defaults still matter Every user needs readable text even when no preference query matches.
forced Check forced-colors too A forced palette changes actual colors; use the separate forced-colors feature when needed.
meaning Do not rely on color alone Use labels, icons, underlines, and clear boundaries for important states.
support Treat as enhancement Older browsers ignore the query and keep your accessible default styles.
Compatibility
Browser Support prefers-contrast is a browser media feature . Node.js, Deno, and Bun do not apply CSS media queries.
✓ Baseline · Newly available
prefers-contrast Available across current major browsers; older releases may ignore it.
Google Chrome 96+
Full support
Mozilla Firefox 101+
Full support
Apple Safari 14.1+
Full support
Microsoft Edge 96+
Full support
Opera 82+
Full support
Samsung Internet 17+
Full support
Internet Explorer No
No support @media (prefers-contrast) Progressive enhancement
Bottom line: Use as progressive enhancement over an accessible default. Test platform settings because available preferences differ.
Remember
Key Takeaways ✓ Role: respond to a user’s prefers-contrast setting.
✓ Values: no-preference, more, less, custom.
✓ Default: keep the page accessible when the query is unsupported.
✓ Test: use actual operating-system preference settings.
★ One line: use prefers-contrast to respect user preferences without weakening the default experience.
Frequently Asked Questions What does prefers-contrast detect? It detects whether the user prefers more contrast, less contrast, a custom palette, or has expressed no preference.
What values does prefers-contrast accept? The values are no-preference, more, less, and custom.
Is prefers-contrast the same as forced-colors? No. prefers-contrast reports a preference; forced-colors reports that the browser is actively overriding author colors.
What should more change? Common improvements include darker text, thicker borders, clearer underlines, and stronger focus indicators.
Should no-preference styles have low contrast? No. The default must already be readable and accessible. Preference queries enhance an adequate base.
How should unsupported browsers behave? They ignore the media block and keep the default styles, which is why the defaults must stand on their own.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion