Color scheme

User Media and Safe-Area Insets

CSS media queries can detect far more than viewport width. A second category of media features describes the person using the site: whether they prefer higher contrast, whether their pointing device is coarse or fine, and whether it can hover. A third group exposes the physical shape of the screen itself, including the notches and rounded corners that eat into the viewport on modern phones. This page covers both: the user preference and capability queries that let you adapt to how someone interacts with your site, and the safe-area environment variables that keep your layout clear of hardware obstructions.

User Preference Queries

prefers-contrast

Some users configure their operating system to request higher or lower contrast. The prefers-contrast media feature exposes that setting so your CSS can respond. It accepts three values: more when the user wants stronger contrast, less when they want reduced contrast, and no-preference (the default) when no setting is active.


        @media (prefers-contrast: more) {
            :root {
                --color-border: #000;
                --color-text-muted: #1a1a1a;
            }
        }
    

Responding to this query is a meaningful accessibility improvement. Users who set high contrast often have low vision, and honoring their preference means your interface works for them without a separate "accessible mode." Check Can I Use: prefers-contrast for current support.

forced-colors

The forced-colors media feature detects Windows High Contrast Mode, which overrides your colors entirely. This topic is covered in depth on the Print and Forced Colors reading from Unit 3. This page will not re-teach it; refer back to that page if you are assigned this topic for your Teaching Video.

Input Capability Queries

Two pairs of media features describe the input devices available to the user. Getting these right prevents a common problem: building an interface that only works with a mouse.

hover and any-hover

The hover feature tests whether the primary input device can hover. On a desktop with a mouse, (hover: hover) is true. On a phone with only a touchscreen, (hover: none) is true because a finger cannot hover. The any-hover feature is broader: it is true if any connected input can hover, which covers tablets with a paired keyboard or stylus.

The practical rule: never put essential content or actions behind a hover-only interaction. Use (hover: hover) to enhance (adding a tooltip preview, a subtle highlight) but make sure the underlying content is reachable without hovering. If an action only appears on hover, it is invisible to every touch user.


        /* Enhancement for devices that can hover */
        @media (hover: hover) {
            .card {
                transition: box-shadow var(--transition-base);
            }
            .card:hover {
                box-shadow: var(--shadow-lg);
            }
        }
    

pointer and any-pointer

The pointer feature describes the precision of the primary pointing device. A mouse is fine; a finger on a touchscreen is coarse. The any-pointer variant checks all connected devices. The most important use is sizing interactive targets: a 24px button is comfortable with a mouse but nearly impossible to tap accurately with a finger. When the pointer is coarse, increase tap targets to at least 44px, the minimum recommended by accessibility guidelines.


        @media (pointer: coarse) {
            button, a, input, select {
                min-height: 44px;
                min-width: 44px;
            }
        }
    

Both pairs have universal browser support. See Can I Use: Interaction Media Features.

Testing with DevTools Emulation

You cannot test these queries by simply resizing your browser window. A narrow window still reports (hover: hover) and (pointer: fine) because the mouse is still your primary input. To see how your CSS responds to different capabilities, use DevTools emulation.

In Chrome and Edge, open DevTools, click the three-dot menu, choose "More tools," then "Rendering." Scroll to the "Emulate CSS media feature" section. You will find dropdowns for prefers-contrast, forced-colors, and others. For hover and pointer, switch to device emulation mode (the phone/tablet icon in the toolbar), which sets both features to match the selected device profile. Firefox offers similar overrides in the Responsive Design Mode panel.

Seeing the Desktop Assumption Break

The demo below shows two versions of the same card component. The first version assumes a mouse: the "Details" action only appears on hover, and the tap target is small. The second version works for any input: the action is always visible and the target is generous. Toggle between them to see what a touch user would experience on the desktop-only version.

Mountain Trail
A demanding ascent through pine forest to a windswept summit.
Details
River Loop
A gentle walk along the river with shaded rest stops.
Details

Safe-Area Environment Variables

Modern phones have hardware that intrudes into the viewport: the notch on older iPhones, the Dynamic Island on newer ones, rounded screen corners, and the home indicator bar at the bottom. Without accommodating them, your content can clip behind these obstructions. The env() function exposes four safe-area inset values that tell you how far to pad your layout to stay clear.

  • env(safe-area-inset-top) for the notch or Dynamic Island
  • env(safe-area-inset-bottom) for the home indicator bar
  • env(safe-area-inset-left) and env(safe-area-inset-right) for landscape orientation on notched devices

These values are zero on devices without obstructions, so applying them unconditionally is safe. The most common use is adding the inset to padding on sticky headers and footers so they do not sit underneath hardware elements.


        .site-header {
            position: sticky;
            top: 0;
            padding-top: env(safe-area-inset-top, 0px);
        }

        .site-footer {
            position: sticky;
            bottom: 0;
            padding-bottom: env(safe-area-inset-bottom, 0px);
        }
    

The second argument to env() is a fallback for browsers that do not support the variable. Modern browsers all support env(), but including the fallback costs nothing and protects against edge cases. You may encounter older references to constant(), which was the original syntax in early iOS Safari. It has been replaced entirely by env() and should not be used in new code. Support for env() is universal in current browsers; see Can I Use: env().

For the insets to have any effect, you must also set the viewport meta tag to extend the page into the safe area. Without this, the browser pads the viewport automatically and the inset values stay at zero even on notched devices.


        <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
    

Safe Areas on a Notched Device

The demo below mocks a notched phone screen so you can see the problem safe-area insets solve. The phone contains a scrollable page with a sticky header. Toggle between "No insets" and "With insets" to see the header shift down to clear the notch. Scroll the content inside the phone to see the sticky header in action.

Color scheme
My Site

Teaching Video Checklist

When you record your teaching video, make sure your teammates come away understanding these concepts:

  • What prefers-contrast detects and why responding to it is an accessibility improvement, not just a nicety
  • Why hiding content behind :hover breaks on touch devices, and how (hover: hover) lets you treat hover as an enhancement rather than a requirement
  • The difference between pointer: fine and pointer: coarse, and the 44px minimum tap target guideline
  • How to use DevTools emulation to test hover, pointer, and contrast queries without a real device, demonstrated live in your browser
  • What env(safe-area-inset-*) values are, why viewport-fit=cover is needed to activate them, and how to apply them to a sticky header or footer
  • Your own demo on your portfolio showing at least one input capability query and one safe-area inset applied to real content

Start by showing a hover-only interaction that breaks in device emulation mode, then fix it with a capability query, so teammates see the problem before the solution.

Resources