October 30, 2026

CSS-only border on scroll

A wild border has appeared, but it’s been there all along?

Illustrated browser mockup with a page header scrolling upwards, revealing a bottom border that hugs the header.

Using some clever stacking, we can make a border appear below the header after scrolling. The idea is to have the “scroll state” fixed to the top, temporarily covered by another element.

In our case, the scroll state will be .header-border — a div with the height of our header plus one pixel to accomodate for the border width. This will be covered by .header-scroll — a div with the height of our header offset (how far down our header is on the page) plus the header height itself.

Markup

Note the use of aria-hidden to ensure screen readers do not announce an empty div.

<div aria-hidden="true" class="header-border"></div>
<div aria-hidden="true" class="header-scroll"></div>

<header></header>

Styles

:root {
	--header-offset: 4rem;
	--header-height: 4rem;
}

.header-border {
	--border-width: 1px;

	height: calc(var(--header-height) + var(--border-width));
	border-bottom: var(--border-width) solid #000;
	position: fixed;
	top: 0;
	inset-inline: 0;
	z-index: 97;
}

.header-scroll {
	height: calc(var(--header-offset) + var(--header-height));
	background: #fff;
	position: absolute;
	top: 0;
	inset-inline: 0;
	z-index: 98;
}

header {
	margin-block-start: var(--header-offset);
	backdrop-filter: saturate(180%) blur(20px);
	position: sticky;
	top: 0;
	z-index: 99;
}