Clean Markup

Styling Sticky Headers and Snapped Slides with CSS Scroll-State Queries

Styling Sticky Headers and Snapped Slides with CSS Scroll-State Queries

For years, styling a position: sticky header once it locks to the top edge of the viewport—or highlighting the active card in a CSS Scroll Snap carousel—required JavaScript workarounds. Developers either attached throttled scroll listeners or placed invisible 1px sentinel elements in the DOM for IntersectionObserver to watch, simply to toggle an .is-stuck or .is-active class and apply a subtle drop shadow or scale transition.

Introduced in CSS Conditional Rules Level 5 and shipping in modern Chromium browsers in 2025, container-type: scroll-state eliminates those script workarounds completely. By marking a sticky element, a scroll-snap item, or an overflow container as a scroll-state query container, its descendants can inspect three native states using @container scroll-state(...) rules: stuck (when a sticky box is pinned to an edge), snapped (when a snap child is aligned to its snapport), and scrollable (when an overflow container has hidden content remaining in a given direction). Because state evaluation runs directly inside the browser's rendering pipeline, UI feedback stays frame-accurate without main-thread JavaScript overhead.

Example

/* Declare the sticky header itself as a scroll-state container */
.page-header {
  position: sticky;
  top: 0;
  container-type: scroll-state;
  container-name: sticky-header;
}

/* Style a descendant when the header is pinned to the top edge */
.header-surface {
  background: transparent;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

@container sticky-header scroll-state(stuck: top) {
  .header-surface {
    background: rgb(255 255 255 / 0.92);
    backdrop-filter: blur(10px);
    box-shadow: 0 4px 20px rgb(15 23 42 / 0.08);
  }
}

Implementation

<header class="sticky-shell">
  <div class="sticky-bar">
    <span class="brand">CleanMarkup</span>
    <span class="status-pill">Pinned View</span>
  </div>
</header>

<section class="carousel-wrapper" aria-label="Featured releases">
  <div class="carousel-track">
    <article class="slide-slot">
      <div class="slide-card">
        <h3>Release 01</h3>
        <p>Active snapped slide receives elevated contrast.</p>
      </div>
    </article>
    <article class="slide-slot">
      <div class="slide-card">
        <h3>Release 02</h3>
        <p>Unfocused slides subtly recede along the track.</p>
      </div>
    </article>
    <article class="slide-slot">
      <div class="slide-card">
        <h3>Release 03</h3>
        <p>Zero IntersectionObserver sentinels required.</p>
      </div>
    </article>
  </div>
  <div class="scroll-hint" aria-hidden="true">Scroll for more</div>
</section>
/* ==========================================================================
   CSS Scroll-State Container Queries: Stuck, Snapped, and Scrollable
   ========================================================================== */

/* 1. Stuck State: Sticky Header Elevation */
.sticky-shell {
  position: sticky;
  inset-block-start: 0;
  z-index: 20;
  container-type: scroll-state;
  container-name: header-state;
}

.sticky-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid transparent;
  transition: background-color 0.25s ease, border-color 0.25s ease, box-shadow 0.25s ease;
}

.status-pill {
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

@container header-state scroll-state(stuck: top) {
  .sticky-bar {
    background-color: rgb(255 255 255 / 0.94);
    border-bottom-color: #e2e8f0;
    box-shadow: 0 8px 24px -12px rgb(15 23 42 / 0.18);
  }

  .status-pill {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 2. Scrollable State: Overflow Indicator Inside Scroll Container */
.carousel-wrapper {
  position: relative;
}

.carousel-track {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(18rem, 65%);
  gap: 1.25rem;
  overflow-x: auto;
  padding: 2rem 1.5rem;
  scroll-snap-type: x mandatory;
  container-type: scroll-state;
  container-name: track-overflow;
}

.scroll-hint {
  display: none;
  margin-top: 0.5rem;
  font-size: 0.875rem;
  color: #475569;
}

/* Show hint only when content overflows to the inline-end (right) */
@container track-overflow scroll-state(scrollable: right) {
  .scroll-hint {
    display: block;
  }
}

/* 3. Snapped State: Active Carousel Slide Styling */
.slide-slot {
  scroll-snap-align: center;
  container-type: scroll-state;
  container-name: slide-snap;
}

.slide-card {
  padding: 1.5rem;
  border-radius: 0.75rem;
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  opacity: 0.65;
  transform: scale(0.96);
  transition: opacity 0.25s ease, transform 0.25s ease, border-color 0.25s ease;
}

/* Progressive enhancement fallback when scroll-state queries are unsupported */
@supports not (container-type: scroll-state) {
  .slide-card {
    opacity: 1;
    transform: none;
  }
}

@container slide-snap scroll-state(snapped: x) {
  .slide-card {
    opacity: 1;
    transform: scale(1);
    background: #ffffff;
    border-color: #2563eb;
    box-shadow: 0 12px 28px -10px rgb(37 99 235 / 0.22);
  }
}

Main Point: CSS scroll-state container queries let sticky headers, snapped carousel items, and scrollable overflow regions style their descendants declaratively in response to real scroll physics without JavaScript observers.

Credits

Photo by Simone Hutsch on Unsplash

← Home