Go back 🤯 Focus vs Focus-Visible in CSS (Without the Headache) /* by Vishalkumar Prajapati - June 20, 2025 */ Tech Update Ever seen a weird blue outline around buttons or links after clicking them? That’s :focus doing its job. But guess what? It doesn’t always do it well. Let’s talk about the difference between :focus and :focus-visible, and why using the right one can make your UI feel polished and accessible. đź§ What is :focus? The :focus pseudo-class applies styles to an element when it receives focus, typically via keyboard navigation (e.g., pressing the Tab key) or clicking or tap on touch devices. It’s great for indicating which element is currently active. button:focus { outline: 2px solid blue; } This adds a blue outline to a button when it’s focused. ⚠️ Caution with :focus: Avoid overuse: Applying :focus to elements in headers or footers (like nav links or social icons) can clutter the user experience. Too many focus indicators can overwhelm users, especially those relying on keyboards. Best practice: Reserve :focus for interactive elements in the main content, like links in paragraphs or form inputs. For example, if a user navigates to a link in a blog post, comes back to the page, and sees a focus indicator, it reminds them where they were. đź’ˇ What is :focus-visible? The :focus-visible pseudo-class is smarter—it only applies styles when the browser determines that a focus indicator is needed, typically for keyboard navigation. It avoids showing focus styles for mouse clicks or touch interactions, keeping the UI clean. button:focus-visible { outline: 2px solid green; } This adds a green outline only when the button is focused via keyboard. 🤔 Why use :focus-visible? Accessibility win: It ensures keyboard users get clear focus indicators without annoying mouse users with unnecessary outlines. Modern and tidy: It’s supported in all modern browsers and aligns with how users expect interfaces to behave. 🎯 Real-World Tip: Use Focus Where It Matters Avoid putting focus styles on links/buttons in headers or footers. Why? Because they’re usually navigational, not part of the “main content story.” âś… Instead, use focus styles on content links — like a link in a paragraph. Imagine this: You click a link in an article → go to another page → hit “Back” → And you see a subtle highlight on the link you clicked. Your brain goes: “Oh yeah! I was just here.” That’s smart UX. And :focus-visible makes it seamless. đź§© TL;DR SelectorTriggers onBest For:focusMouse, keyboard, touchLow control (use carefully):focus-visibleKeyboard & assistive tech onlyHigh control, better UX đź’ Final Thoughts The difference between :focus and :focus-visible may seem subtle, but it plays a huge role in creating user-friendly, accessible, and polished web experiences. Use :focus-visible like a polite tour guide — it shows users where they are only when they need to know. Save :focus for rare, intentional cases where you want all interactions (including mouse clicks) to show a focus style. As developers, we often focus on pixels and performance — but a small CSS tweak like this can make your interface feel smarter and more human. Because good UX isn’t just how it looks.It’s how it feels to use.