I have spent countless hours deep within the murky depths of a component library’s CSS imposing my will on it.
I’m not even complaining, because that’s pretty much exactly how my brain likes to learn. I’d poke around in Chrome’s inspector until I found the teeny bit of CSS responsible for whatever was bugging me, jump over to Visual Studio to write a little override class, recompile, refresh, silently curse at the screen when nothing changed, and go around and around again and again until something finally did. A nested selector here, a pseudo-element there, and eventually the brand new third-party component we needed would look like it had been part of the design all along.
And for a shockingly long time, that worked. Like, really well. At the time, we were building custom software for a very small group of people (small enough that I knew nearly every one of them by name), so there was plenty of room to learn whatever was right in front of me, solve it, and toss the solution in the ol’ toolbox.
Fun fact: the toolbox was a real thing. It was a file called
toolbox.jsthat followed me from project to project, chock full of all the nifty stuff I kept reusing (and/or kept forgetting the syntax for).
We built some incredibly impressive tools that way, but every one of them was designed around exactly one way of using it. Mine.
The annoying blue ring
If you’ve ever spent time dressing up browser-native elements to look like they belong in your design, you’ve probably run smack into the blue ring. You get the colors just right. The spacing feels solid and intentional. The interaction states are starting to cooperate. Then a blue outline appears around an element, seemingly at random, looking like someone took a crayon to your masterpiece.
That thin blue outline became my white whale. It was easy enough to find a little snippet online that would remove it, but new components added to the site kept reintroducing the stupid thing. From there it wasn’t too hard to make a broad, site-sweeping class that erased the outline from an entire project, but then there were all the other projects that kept popping up. The answer, of course, was to build out a shared design system and component library that maintained a tight grip on allowed visual styes.
The whole time the temptation to bypass the entire situation by just building something that looked like a button out of a regular, old DOM element with a click handler grew and grew.
Eventually though, I got to the place where the application(s) consistently looked exactly how I wanted. Problem solved!
…right?
Feedback started coming in around not being able to know when a button could be activated, or which input field was currently selected. When I first heard the news, it didn’t even sound like they were bugs to me. I was sure it had to be a textbook case of user error. I mean, we had disabled states that were clearly different from enabled states, there was no way you wouldn’t be able to tell which button could be clicked at any given time. The input field that was selected was the one that you had clicked on… like, how do you miss that?
I couldn’t wrap my brain around how these things could be issues, so I went up to the 3rd floor to talk to the person who had submitted the feedback (bespoke software, ridiculously small user base) so I could watch them work and we could clear up this misunderstanding.
They never even touched their mouse once. It was like watching a hacker operate in a low-budget movie, just randomly tapping away on the keyboard as things inexplicably progressed on the screen. Once they arrived at the form that was a core piece of the whole entire point of the entire app, the submitted feedback immediately made perfect sense.
This experience was broken.
Without a visible focus state, there was no way to tell which button or input field you had tabbed to without counting how many times you’d tapped the tab key and matching that up to the number of items in the form. It was disorienting. It was frustrating. It was bad user experience. There was no dependable answer to a question so fundamental that I’d completely taken it for granted: where am I on the page right now?
Not yet thinking big enough
I didn’t immediately understand the whole accessibility aspect at play. What I understood first was that people using their keyboard were having a poor experience, and I needed to give them a way to see where their focus was.
So I built custom focus states.
Inputs that had been light gray, flat, and borderless now popped up off the page in bright white when focused. They picked up a blue border, a box shadow, and just enough elevation to make the active field feel unmistakably active without being in-your-face about it. Buttons got the elevated lift and background color change treatment they were already doing for mouse hover over states.
It was a drastic improvement. You could actually tab through the form now and tell where you were, and buttons were no longer playing coy when you ditched the mouse and went fully keyboard. But I had only created new, custom focus states for text fields and buttons. The forms didn’t have any checkboxes on them, so my design system dictating the visual styles had nothing for them.
I was also only considering keyboard users with a mouse aversion. Elements still weren’t being properly labeled or semantically implemented for the benefit of screen readers because… well, we didn’t have any screen reader users and my lens wasn’t wide enough yet.
The pieces were already there
Thankfully, a bit down the road, I had the chance to work alongside passionate and incredibly knowledgable accessiblity advocates who helped connect the dots for me. It wasn’t just keyboard users. Some people can’t see your carefully crafted visual flair at all and rely on a screen reader to speak back to them what the browser knows about your rendered code.
Are you using heading elements? Are they properly nested? Do your input fields announce their label?
People rely on predictable focus behavior and coherent descriptions, and semantic elements already include a huge amount of out-of-the-box solutions: keyboard interaction, assistive-technology expectations, and the familiar ways controls are supposed to work are all already built within native HTML components. When I bypassed those elements to get complete visual freedom, I was able to finally remove the annoying blue ring, sure. But I was inadvertently also dropping handfuls of critical accessibility features.
My approach was in need of change.
Instead of asking how to make the browser disappear from the design, I started to ask how to work with the useful behavior that was already there. Instead of making one-off pieces that merely looked great, I started treating semantics, clear focus, and predictable interaction as non-negotiable items and designed intentionally around them so that they behaved great as well.
I love working with constraints. Knowing something has to be part of the feature silences the part of my brain that would have been wasting time debating how best to not do it and let’s me focus in on the design instead.
Skip to the recipe
Okay, the actionable part. Each of the following examples is built on plain HTML elements, which let’s the browser handle the keyboard and screen reader behavior while we merely tailor the styling to decide what the focus state looks like.
Pattern 1: How I should’ve done it in the first place
The text field starts out light gray and minmalist. Once you give it focus though it turns bright white, picks up a blue border and a soft shadow, and lifts slightly off the page, with a focus ring around all of it so there’s no question which field you’re in. The button next to it gets the same shadow and a tight, high-contrast ring so the two feel like a set.
Click the input, then Tab to the button and Shift+Tab back to compare. Code, Preview, and Split switch what you’re looking at.
<style>
/* Pattern 1: elevated fields and clearly focused buttons.
Scope the example so it doesn't restyle the rest of the page. */
.focus-recipe {
--surface-muted: #e2e8f0;
--surface: #ffffff;
--accent: #0b57d0;
--focus-ring: #0b57d0;
display: grid;
gap: 1rem;
padding: 1.5rem;
background: #f8fafc;
color: #172033;
border-radius: 0.75rem;
}
.focus-recipe label {
display: grid;
gap: 0.5rem;
}
.focus-recipe input,
.focus-recipe button {
box-sizing: border-box;
min-width: 0;
padding: 0.75rem 1rem;
border-radius: 0.5rem;
font: inherit;
color: inherit;
}
.focus-recipe input {
width: 100%;
background: var(--surface-muted);
border: 1px solid transparent;
box-shadow: none;
transition: background-color 160ms ease, border-color 160ms ease,
box-shadow 160ms ease, transform 160ms ease;
}
.focus-recipe input:focus-visible {
background: var(--surface);
border-color: var(--accent);
box-shadow: 0 0.4rem 1rem color-mix(in srgb, var(--accent) 18%, transparent);
outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent);
outline-offset: 2px;
transform: translateY(-1px);
}
.focus-recipe button {
justify-self: start;
background: var(--surface);
border: 1px solid #64748b;
}
.focus-recipe button:focus-visible {
box-shadow: 0 0.35rem 0.8rem color-mix(in srgb, black 18%, transparent);
outline: 3px solid var(--focus-ring);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.focus-recipe input {
transition: none;
}
.focus-recipe input:focus-visible {
transform: none;
}
}
</style>
<div class="focus-recipe">
<label>
Your name
<input type="text" autocomplete="off" placeholder="Try keyboard focus">
</label>
<button type="button">Try this button</button>
</div>Fun fact:
:focus-visiblelets the browser decide when the focus ring is going to be helpful. So tabbing to a button shows the ring, but clicking with a mouse or tapping it doesn’t.
Pattern 2: Focus for any background
A focus color is only as good as the level of contrast it has againt the background it appears over. Put the same button on a dark band, a photo, or a gradient and the ring styled for your nearly white document body can all but disappear.
WCAG asks for at least 3:1 contrast between a focus indicator and the colors next to it, and one color is going to have an exceptionally difficult time clearing that everywhere. If you know every background ahead of time, you can give each one its own focus color class. When you don’t, try pairing two colors: white right against the control and a dark accent color outside it, so at least one of them stands out against whatever happens to be behind the button.
For some additional pizzaz, here the two bands get cut down to four corner brackets that snap in around the button, kinda like a camera locking in focus.
<style>
/* Pattern 2: two-tone corner brackets, so one band always contrasts
with the ground. Shadows vanish in forced-colors mode, so a
transparent outline steps in as a real ring there. */
.ring-recipe {
--ring-inner: #ffffff;
--ring-outer: #0b57d0;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
gap: 1rem;
}
.ring-recipe .ground {
display: grid;
place-items: center;
min-height: 8rem;
padding: 1.5rem;
border-radius: 0.75rem;
}
.ring-recipe .light { background: #f8fafc; }
.ring-recipe .dark { background: #172033; }
.ring-recipe .busy {
background: linear-gradient(135deg, #0b57d0, #f8fafc 50%, #172033);
}
.ring-recipe button {
position: relative;
padding: 0.75rem 1rem;
border: 1px solid #64748b;
border-radius: 0.5rem;
background: #ffffff;
color: #172033;
font: inherit;
}
/* Both bands drawn as inset shadows, then masked down to the corners. */
.ring-recipe button::after {
--arm: 1.25rem;
content: "";
position: absolute;
inset: -9px;
border-radius: 0.75rem;
box-shadow:
inset 0 0 0 3px var(--ring-outer),
inset 0 0 0 6px var(--ring-inner);
mask:
linear-gradient(#000 0 0) top left,
linear-gradient(#000 0 0) top right,
linear-gradient(#000 0 0) bottom left,
linear-gradient(#000 0 0) bottom right;
mask-size: var(--arm) var(--arm);
mask-repeat: no-repeat;
opacity: 0;
scale: 1.25;
pointer-events: none;
transition: opacity 120ms ease,
scale 220ms cubic-bezier(0.2, 0.9, 0.3, 1.3);
}
.ring-recipe button:focus-visible {
outline: 3px solid transparent;
outline-offset: 3px;
}
.ring-recipe button:focus-visible::after {
opacity: 1;
scale: 1;
}
@media (prefers-reduced-motion: reduce) {
.ring-recipe button::after {
scale: 1;
transition: opacity 120ms ease;
}
}
</style>
<div class="ring-recipe">
<div class="ground light"><button type="button">On light</button></div>
<div class="ground dark"><button type="button">On dark</button></div>
<div class="ground busy"><button type="button">On a gradient</button></div>
</div>Heads up: corners get you to AA, not AAA. WCAG’s AAA Focus Appearance criterion (2.4.13) wants the indicator to cover at least as much area as a 2px outline around the whole control, and four brackets don’t. If you’re aiming for AAA, lengthen the arms until they make up the difference, or close them into a full ring.
If the brackets seem to be missing, check for overflow: hidden on a parent before anything else. They sit outside the button just like an offset ring would, and some ancestor you forgot about is very often the reason a focus state “isn’t there.” If you can’t get rid of the clipping, change inset to a small positive value and the brackets will frame the inside of the control instead.
Pattern 3: Let the surroundings answer too
Sometimes the control is tiny and the thing it belongs to is a lot bigger, like a toggle at the end of a settings row or a link tucked into a card. The ring shows you exactly where focus is, but that doesn’t always tell you what the focused thing is for.
So let the container react too. When anything in a row below gets keyboard focus, the row tints and an accent bar slides in along its edge, while the ring on the switch keeps doing the real work. Using :has(:focus-visible) rather than :focus-within keeps the row from lighting up on mouse clicks, the same way Pattern 1 does.
<style>
/* Pattern 3: the row answers too; the ring still does the real work. */
.row-recipe {
--accent: #0b57d0;
--tint: #eef3fd;
--ink: #172033;
--muted: #4b5568;
padding: 1.5rem;
background: #f8fafc;
border-radius: 0.75rem;
color: var(--ink);
}
.row-recipe .panel {
display: grid;
gap: 0.25rem;
max-width: 28rem;
margin-inline: auto;
padding: 0.5rem;
background: #ffffff;
border: 1px solid #d5dde8;
border-radius: 1rem;
box-shadow: 0 1.25rem 2.5rem -1.5rem rgb(23 32 51 / 0.35);
}
.row-recipe .setting {
position: relative;
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
column-gap: 1rem;
padding: 0.875rem 1rem 0.875rem 1.5rem;
border-radius: 0.75rem;
transition: background-color 160ms ease;
}
/* The accent bar: parked flat, grows in on keyboard focus. */
.row-recipe .setting::before {
content: "";
position: absolute;
inset-block: 0.75rem;
left: 0.5rem;
width: 4px;
border-radius: 999px;
background: var(--accent);
scale: 1 0;
transition: scale 200ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.row-recipe .setting:has(:focus-visible) {
background: var(--tint);
}
.row-recipe .setting:has(:focus-visible)::before {
scale: 1 1;
}
/* Stretch the label over the row so a click anywhere flips the switch. */
.row-recipe label {
font-weight: 600;
cursor: pointer;
}
.row-recipe label::after {
content: "";
position: absolute;
inset: 0;
}
.row-recipe .hint {
grid-column: 1;
margin: 0;
font-size: 0.875rem;
color: var(--muted);
}
/* A native checkbox, drawn as a switch with backgrounds only. */
.row-recipe input {
position: relative;
z-index: 1;
grid-column: 2;
grid-row: 1 / span 2;
appearance: none;
width: 2.75rem;
height: 1.5rem;
margin: 0;
border-radius: 999px;
background:
radial-gradient(circle closest-side, #ffffff 92%, transparent)
0.25rem 50% / 1rem 1rem no-repeat,
#64748b;
cursor: pointer;
transition: background-color 160ms ease, background-position 160ms ease;
}
.row-recipe input:checked {
background-color: var(--accent);
background-position: calc(100% - 0.25rem) 50%;
}
.row-recipe input:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.row-recipe .setting,
.row-recipe .setting::before,
.row-recipe input {
transition: none;
}
}
/* Forced colors repaints the backgrounds the switch is drawn with,
so hand it back to the system checkbox. */
@media (forced-colors: active) {
.row-recipe input {
appearance: auto;
width: auto;
height: auto;
}
}
</style>
<div class="row-recipe">
<div class="panel">
<div class="setting">
<label for="row-recipe-motion">Reduce motion</label>
<p class="hint" id="row-recipe-motion-hint">Swaps slides and zooms for simple fades.</p>
<input type="checkbox" role="switch" id="row-recipe-motion" aria-describedby="row-recipe-motion-hint">
</div>
<div class="setting">
<label for="row-recipe-focus">Hide distractions</label>
<p class="hint" id="row-recipe-focus-hint">Tucks away sidebars and related links while you read.</p>
<input type="checkbox" role="switch" id="row-recipe-focus" aria-describedby="row-recipe-focus-hint">
</div>
<div class="setting">
<label for="row-recipe-text">Larger text</label>
<p class="hint" id="row-recipe-text-hint">Bumps body text up a step across the site.</p>
<input type="checkbox" role="switch" id="row-recipe-text" aria-describedby="row-recipe-text-hint" checked>
</div>
</div>
</div>Under the styling these are still native checkboxes. role=“switch” gets them announced as on or off instead of checked, and each description hangs off aria-describedby rather than getting stuffed into the label, so the name stays short. The label’s ::after stretches across the whole row, which means clicking anywhere in it flips the switch without a line of JavaScript.
Know better? Do better
I’m still the person who can lose an entire afternoon to nudging a box shadow around a pixel at a time, and I still care a lot about things looking like they belong together. But now that my lens is larger and I know a little bit better, focus is one of the things I’m nudging around and focusing on too. I design it right next to hover and disabled from the start, and the thin blue outline I spent so long trying to erase gets replaced with something that looks consistent and intentional instead of just getting deleted.