Native CSS, Part 2: Popover and Focus Patterns Worth Stealing

Native CSS, Part 2: Popover and Focus Patterns Worth Stealing

19 November 2026

This is part two of the native CSS series. Part one covered the patterns that replaced my JavaScript: anchor positioning, popover, dialog modals, and disclosure without a single script. You can read it here: Native CSS First: Patterns That Replaced My JavaScript. This part goes deeper on the two patterns that took the most work to get right: popover and focus.

The popover and focus demos were the weakest part of the first showcase. I knew it when I built them, and the fix took a second pass and a deep dive into Jhey's patterns, which added nineteen new entries to my native CSS skill. This post is what that second pass taught me.

What we cover

  • Popover, done properly
  • Focus, and why it is the whole game
  • The patterns I stole from Jhey
  • What still needs JavaScript

Popover, done properly

The popover API is the browser's built-in way to show something on top of the page: a menu, a tooltip, a picker. You add the popover attribute to an element, and a button with popovertarget opens it. No JavaScript, no positioning library, no state management.

The part that makes it work is the top layer. Popovers render in the top layer, above everything else on the page, so they escape z-index wars and overflow clipping. That alone kills a whole class of bugs that used to need JavaScript and careful CSS to work around.

What the first demo got wrong was the details. A popover that opens is easy. A popover that closes when you expect it to, returns focus to the button that opened it, and does not trap a keyboard user, takes care. The fixes: use popovertargetaction for explicit open and close, let the browser handle light dismiss (click outside to close), and test with a keyboard, not just a mouse.

Anchor positioning pairs with popover. You anchor the popover to its button with anchor-name and position-anchor, and the browser keeps it attached even as the page moves. A dropdown that follows its trigger without a resize listener is the kind of thing that used to take a library. Now it takes two properties.

Try the native popover menu

Click Open menu or press Enter. Escape or clicking outside closes it.

<button type="button" class="menu-button" popovertarget="menu">Open menu</button>
<div class="menu" id="menu" popover>
  <a href="/">Home</a>
  <a href="/blog">All blog posts</a>
  <a href="/projects">Projects</a>
</div>
/* The trigger names itself as an anchor. */
.menu-button {
  anchor-name: --menu-button;
}

/* The popover anchors to its button and sits in the top layer. */
.menu {
  position-anchor: --menu-button;
  position-area: bottom;
  inset: auto; /* Clear the browser default so the anchor can place it. */
  justify-self: start;
  align-self: start;
  margin: 0.4rem 0 0;
  padding: 0.35rem;
  border: 1px solid #1a1a1a;
  border-radius: 4px;
  background: #fff;
}

.menu a {
  display: block;
  padding: 0.4rem 0.65rem;
  border-radius: 3px;
  color: #1a1a1a;
  text-decoration: none;
}

Focus, and why it is the whole game

Focus is where native CSS either wins or loses. A component that looks right and cannot be used from a keyboard is a broken component, and focus styles are how you tell a keyboard user where they are.

The rule is :focus-visible. It shows a focus ring when the user is navigating with a keyboard, and hides it when they are clicking with a mouse. That distinction is the whole trick: mouse users do not want rings everywhere, and keyboard users need them always. :focus-visible gives each group what it needs without a single line of JavaScript.

The fix that seeded this whole approach came from Coyier: style :focus-visible with a clear, high-contrast outline, and never remove focus styles without replacing them. outline: none on its own is an accessibility bug, not a design choice. If the default ring offends you, design a better one. Do not delete it.

Try the focus ring

Tab through these three links and watch the ring appear on each one. Click one with a mouse and there is no ring. That is :focus-visible doing its job.

<a href="https://austincodez.dev/blog/2026-11/native-css-first-patterns-replaced-my-javascript">Part one</a>
<a href="https://austincodez.dev/">Home</a>
<a href="https://austincodez.dev/projects/ai-compendium">AI Compendium project</a>
/* Never remove a focus ring without replacing it. */
:focus {
  outline: none;
}

/* Design a better ring instead. */
:focus-visible {
  outline: 3px solid #005fcc;
  outline-offset: 2px;
}

Dialog and popover both move focus for you when they open, and return it when they close. That is free behavior, and it is correct behavior. The moment you rebuild a dialog in JavaScript, you own all of that, and you will get some of it wrong.

The patterns I stole from Jhey

Jhey's patterns are the best catalog of what native CSS can do that I have found. Nineteen of them went into my skill after the deep dive, and the ones that changed this post are the popover transitions, the focus ring treatments, and the anchor-positioned menus that adapt to the viewport.

The pattern worth stealing first is the popover with an entry transition. Popovers can animate in with pure CSS now, using the transition on the top layer and the starting style. A menu that fades and slides in, with no JavaScript, used to be a framework feature. Now it is a few lines of CSS.

Try the popover entry transition

Click Open menu and watch it fade and slide in. The whole transition is pure CSS.

<button type="button" class="menu-button" popovertarget="menu">Open menu</button>
<div class="menu" id="menu" popover>
  <a href="/">Home</a>
  <a href="/blog">All blog posts</a>
  <a href="/projects">Projects</a>
</div>
/* The trigger names itself as an anchor. */
.menu-button {
  anchor-name: --menu-button;
}

/* The popover anchors to its button and sits in the top layer. */
.menu {
  position-anchor: --menu-button;
  position-area: bottom;
  inset: auto; /* Clear the browser default so the anchor can place it. */
  justify-self: start;
  align-self: start;
  margin: 0.4rem 0 0;
  padding: 0.35rem;
  border: 1px solid #1a1a1a;
  border-radius: 4px;
  background: #fff;
}

.menu a {
  display: block;
  padding: 0.4rem 0.65rem;
  border-radius: 3px;
  color: #1a1a1a;
  text-decoration: none;
}

/* Animate the popover in and out with pure CSS. */
.menu {
  opacity: 0;
  transform: translateY(-4px);
  transition:
    opacity 0.2s ease,
    transform 0.2s ease,
    display 0.2s ease allow-discrete,
    overlay 0.2s ease allow-discrete;
}

.menu:popover-open {
  opacity: 1;
  transform: translateY(0);
}

@starting-style {
  .menu:popover-open {
    opacity: 0;
    transform: translateY(-4px);
  }
}

Live demo: a card that restyles itself

Drag the slider to change the card width. At 224px the card switches layout, and the switch is pure CSS, a container query doing the work.

What still needs JavaScript

Not everything. Complex state, data fetching, and anything that talks to a server still needs a script. The rule is not no JavaScript. The rule is native first: if the platform can do it, let the platform do it, and save the JavaScript for the work only JavaScript can do.

Live demo: the part that still needs a script

Click Add one. The count is state, and state is what JavaScript is for.

That rule is also how I build client sites. Native CSS at scaffold time means the accessibility and performance work is done before the first feature lands, not bolted on at the end. It is cheaper, it is faster, and it is easier to maintain. The JavaScript you do not write is the JavaScript you never have to debug.

Final thoughts

Popover and focus are the two patterns that separate a native CSS demo from a native CSS site. Get them right and the rest follows. Get them wrong and you have a pretty page that some of your users cannot use.

The short version: popover plus anchor positioning replaces the dropdown library, :focus-visible replaces the focus hack, and the JavaScript you do not write is the JavaScript you never debug.

Tags

/blog/2026-11/native-css-part-2-popover-and-focus-patterns-worth-stealing austin.amento.dev