← blog
CSSDecember 4, 2026 · 5 min read

I Stopped Checking relatedTarget. :focus-within Does It in CSS.

A search box's own clear button kept un-focusing the whole wrapper, so I started checking event.relatedTarget on every blur to tell 'focus left the widget' from 'focus moved inside it.' :focus-within in CSS made the check unnecessary.

Parsa Jiravand · Frontend engineer · building bestpractic
I Stopped Checking relatedTarget. :focus-within Does It in CSS.

I built a search box with a clear button inside it — the little × that shows up once you've typed something. Standard stuff. The input had a nice blue ring when focused, so I matched it on the wrapper too: border-color: var(--focus-color) the moment the input gets focus, so the whole pill looks selected, not just the text caret.

It worked, until a teammate typed a query and clicked the ×. For one frame, the wrapper's border flashed back to gray — like the whole box had lost focus — before the input (correctly) still showing the cleared, empty state kept its own focus ring. It looked like a rendering glitch. It wasn't. It was onBlur doing exactly what onBlur does.

The wrapper's "focused" look was driven by state, toggled from the input's own focus and blur events:

JSX
1
2
3
4
5
6
7
8
9
10
11
12
13
function SearchBox() { const [focused, setFocused] = useState(false); return ( <div className={focused ? "search focused" : "search"}> <input onFocus={() => setFocused(true)} onBlur={() => setFocused(false)} /> <button onClick={clear}>×</button> </div> ); }

Click the ×, and here's the order of events: the button steals focus away from the input, so the input's blur fires, setFocused(false) runs, and the wrapper's "focused" class disappears — a full render cycle before anyone notices the focus is still inside the same component, just on a different element.

The standard patch, and the one you'll find in a dozen Stack Overflow answers for exactly this: check where focus is going before you believe it left.

JSX
1
2
3
4
5
onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget)) { setFocused(false); } }}

relatedTarget is the element about to receive focus — MDN documents it on the FocusEvent interface for exactly this reason. currentTarget.contains(relatedTarget) asks "is the new focus target a descendant of the wrapper I'm attached to?" If yes, don't un-focus. It works. It also has to be written again for every dropdown, every combobox, every form row with more than one focusable child — and it quietly breaks the moment the new target sits behind a portal or an iframe, where contains() can't see it at all.

focus and blur are defined per element. The browser has no built-in notion that the input and its clear button are "one search box" — that grouping exists only in your head and your JSX. So every time focus moves within a thing you consider one unit, the browser reports it as a departure from one element and an arrival at another, and you're stuck inferring "same widget" from DOM containment by hand.

CSS already has the word for that grouping. It's a pseudo-class, not a state you manage:

CSS
1
2
3
.search:focus-within { border-color: var(--focus-color); }

:focus-within matches an element if that element or any descendant currently has focus. Click into the input — matches. Tab to the clear button — still matches, because the button is a descendant of .search. Click the × to actually blur out of the whole component — only then does it stop matching. No useState, no onFocus/onBlur pair, no relatedTarget containment check, and no render at all: the browser repaints the border because the CSS rule re-evaluates, not because your component re-rendered.

:focus, :focus-visible, and :focus-within read like three names for the same idea. They're not:

  • :focus — matches the element that literally has focus right now. Just that one element.
  • :focus-visible — matches only when the browser thinks the focus indicator should show (keyboard navigation, mostly — a mouse click on a <button> usually doesn't trigger it). I wrote about this one back when outline: none was the fight.
  • :focus-within — matches an ancestor of whatever has focus, all the way up. The one this post is about.

They compose. .search:focus-within:not(:focus) matches the wrapper exactly when focus is inside it but not on it — useful if the wrapper itself is also focusable and you want different styling for "I'm focused" versus "something inside me is."

The same pattern shows up anywhere a widget is more than one focusable element and you want it to read as one thing:

  • Form rows — highlight the whole label-plus-input row when the input inside it is focused, without a listener on every row.
  • Toolbars with grouped fields — a date range picker with two inputs should look "active" if focus is on either one.
  • Custom dropdowns and comboboxes — keep a wrapper visually "open" while focus bounces between the trigger and the menu, instead of tracking that by hand.

One honest limit: :focus-within is CSS-only. It changes what gets styled, not what your JavaScript knows. If a component genuinely needs a boolean in JS — to fire an analytics event, say, or gate some other logic — you still need focus/blur listeners (with the relatedTarget check, if you only want to know when focus leaves the whole group). What you get to delete is every line of state and every re-render that existed only to drive a style.

Support isn't the concern here — :focus-within has shipped in every engine for years (Chrome 60+, Firefox 52+, Safari 10.1+), no polyfill, no flag. The concern was ever having written the JS in the first place.

Runs right in your browser — poke at it and watch the concept react live.

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.

Go find the component in your codebase with an onFocus/onBlur pair whose only job is toggling a CSS class on a parent. I'd bet money you have at least one. Rip it out and see if the browser was already willing to do it for free — then tell me what broke, because I'd genuinely like to hear about the one case where it didn't just work.


🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

Keep reading

One post a day, in your inbox

Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.

0 comments

Sign in to join the discussion, like comments, and save articles for later.