The effect and its markup

“Raise the curtains” describes a background that shifts from dark to light as the page scrolls, while content pinned in a sticky position simultaneously flips from light to dark. The whole thing can be built with HTML and CSS alone.

The component is intentionally small: three elements. A container carries the .curtain class, an .invert child acts as the sticky box, and a heading — an <h2> here — sits inside it.

<div class="curtain">
  <div class="invert">
    <h2>Section title</h2>
  </div>
</div>

Starting with variables

Three values are known up front, so they become custom properties that are easy to reuse and change later:

  • --minh — the container height
  • --color1 — the light color
  • --color2 — the dark color
:root {
  --minh: 98vh;
  --color1: wheat;
  --color2: midnightblue;
}

Drawing the curtain

The .curtain itself relies on a linear-gradient for the split background and a min-height for extra room at the bottom. That extra space is generated with the ::after pseudo-element, which is what lets the sticky content keep sticking to the container while scrolling past it — the illusion depends on this.

.curtain {
  /** create the "split" background **/
  background-image: linear-gradient(to bottom, var(--color2) 50%, var(--color1) 50%);
}

/** add extra space to the bottom (need this for the "sticky" effect) **/
.curtain::after {
  content: "";
  display: block;
  min-height: var(--minh);
}

Pinning the content

The .invert child handles the sticking and the color swap through a handful of declarations:

  • position: sticky plus top set where it sticks.
  • mix-blend-mode: difference blends the heading’s text color into the curtain’s gradient.
  • display: flex for centering.
  • min-height to define the height and permit the extra space below.
  • color for the h2 itself.
.invert {
  /** make the content sticky **/
  position: sticky;
  top: 20px;

  /** blend the content with the contrast effect **/
  mix-blend-mode: difference;

  /** center the content **/
  display: flex;
  align-items: center;
  justify-content: center;
  
  /** set the minimum height of the section **/
  min-height: var(--minh);
}

h2 {
  /** set the color of the text **/
  color: var(--color1);
}

The sticky position and flexbox are straightforward. The height is set with the variable and matches the height of .curtain::after. The interesting piece is mix-blend-mode: difference, which blends content against the background — a rough way to picture it is text color inverted against whatever sits behind it. For the blend to work, the heading needs a color: here a light value (wheat) is assigned to --color1.

Where it breaks

A few snags surfaced while refining the effect.

Images in the sticky content are risky: avoid any that look bad with inverted colors. A simple SVG and a transparent PNG, as in the demo, hold up fine.

It is not possible to apply mix-blend-mode: difference to specific children such as headings while exempting images. Among the reasons: position: sticky cancels the blending. The same happens when transform: skewY is used on the container to add a slight tilt. Other properties likely interfere as well, though that was not explored further. A non-scrolling demo without the problematic properties shows the base case.