Scroll-linked navigation colour

Keep fixed navigation readable across light and dark sections with explicit themes, viewport observation and predictable boundary behaviour.

Fixed navigation can sit over several different backgrounds during one visit. White links that work over a dark hero can disappear once the next section moves underneath them.

A theme change can solve that, but only if the trigger matches the area behind the navigation. Checking whether a section is visible somewhere on screen isn’t enough.

Let the section declare the intended theme

Inferring contrast from photographs or computed background colours quickly becomes unreliable. Transparency, overlapping elements and moving media make the answer more complicated than a single CSS value.

An explicit section attribute is easier to understand. For example, data-nav-light can mark light sections that need dark navigation text. The stylesheet can then apply the matching theme through a state on the document root.

The naming has to be clear. Does “light” describe the background or the navigation? Pick one meaning and document it, so the next editor doesn’t reverse it by accident.

In one implementation, marked light sections switch the navigation to its dark theme when they intersect the strip the navigation occupies.

Observe the navigation region

IntersectionObserver reports when targets overlap a defined region. A root margin can narrow the useful part of the viewport down to a strip rather than the whole screen.

For a navigation bar at the top of the viewport, the calculation depends on its height and the viewport height. A bar with a top offset needs that offset included. A floating pill over only part of a section may call for a different design decision.

Recalculate the observer when the viewport or the navigation’s dimensions change. An observer configured for the old height can switch at the wrong point after a resize, a wrapped menu label or a font change.

A common approach rebuilds the observer on a debounced resize. If the navigation’s size can change independently, a ResizeObserver may also be appropriate.

Decide what happens at a boundary

Two sections can overlap the observed strip at the same time. Don’t let whichever callback entry happens to be processed last define the appearance.

Keep a record of the intersecting sections and apply a stated rule. That implementation uses a set: if any marked light section intersects the strip, the navigation turns dark.

That’s predictable, but it’s still a design choice. A sharp boundary can leave some links over each background at once. If neither text colour stays readable across that transition, a solid or translucent navigation background may be the better answer.

Colour switching shouldn’t be asked to compensate for a layout that can’t hold contrast.

Smooth scrolling and transformed content need testing

Some smooth-scroll systems move a wrapper with transforms. The navigation can stay fixed while sections move inside another coordinate context.

Test the actual geometry rather than assuming native scroll events tell the whole story. Viewport observation can suit this setup, but nested scroll roots and clipping ancestors still affect what counts as intersecting.

Check normal scrolling, fast scrolling, anchor links and a visit that starts midway down the page. Back navigation and restored scroll positions can briefly show a default theme that’s wrong.

Keep the state change simple

Switch a class or attribute and let CSS handle the appearance. Update the text, icons and borders together, so the brand mark doesn’t vanish while the links stay readable. The menu article covers the same header in its open state.

A long colour transition can pass through an intermediate colour with poor contrast. Prefer a short, useful change, or an immediate switch where necessary. Respect reduced-motion preferences when animation is involved.

If the observation code fails, fall back to a legible default navigation treatment. Essential links shouldn’t depend on a decorative script to be visible.

Review with real section content

Test photographs, gradients, open menus and narrow screens. Check hover and keyboard focus colours in both themes.

If editors can change section backgrounds, explain how they choose the navigation treatment as well. Otherwise the original build may work perfectly until a routine image replacement produces an unreadable header.

Reference

  • Interaction design