# Shrinking Header

> A sticky header that shrinks, gains a background and casts a shadow over the first 120px of scrolling, in pure CSS.

- Category: [CSS Effects](https://copy-css.com/category/effects)
- Tags: sticky header, shrink on scroll, scroll-driven, navbar, animation-range, scroll timeline
- Live preview: https://copy-css.com/snippets/shrinking-header
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<header class="shrink-head">
  <a class="shrink-head-logo" href="#">Acme</a>
  <nav aria-label="Main">
    <a href="#">Docs</a>
    <a href="#">Blog</a>
  </nav>
</header>
```

## CSS

```css
.shrink-head {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 22px 20px;
  color: #0f172a;
  font-family: system-ui, sans-serif;
}

.shrink-head-logo {
  color: inherit;
  font-size: 24px;
  font-weight: 800;
  text-decoration: none;
}

.shrink-head nav {
  display: flex;
  gap: 16px;
}

.shrink-head nav a {
  color: #475569;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
}

.shrink-head a:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 3px;
  border-radius: 2px;
}

@supports (animation-timeline: scroll()) {
  .shrink-head {
    animation: shrink-head-pad linear both;
    animation-timeline: scroll();
    animation-range: 0 120px;
  }

  .shrink-head-logo {
    animation: shrink-head-logo linear both;
    animation-timeline: scroll();
    animation-range: 0 120px;
  }
}

@keyframes shrink-head-pad {
  to {
    padding-block: 10px;
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 4px 16px -8px rgba(15, 23, 42, 0.3);
  }
}

@keyframes shrink-head-logo {
  to {
    font-size: 18px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .shrink-head,
  .shrink-head-logo {
    animation: none;
  }
}
```

## Notes

- This CSS Shrinking Header is 71 lines of pure CSS (1.2 KB) with no JavaScript and no dependencies. It uses scroll-driven animations, keyframe animations, box shadows and Flexbox.
- scroll-driven animations: Chrome and Edge 115+ and Safari 26+. Firefox support is very recent; browsers without it skip the animation and show the content as is.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
