跳到正文
DEV Community· Design Den Solutions·· 3 小時前AI 評分56

鍵盤焦點是導覽狀態,而非裝飾

Keyboard Focus Is a Navigation State, Not a Decoration

AI 導讀

Design Den Solutions 將鍵盤焦點界定為導覽狀態而非裝飾,並整理導航選單的鍵盤可用性檢查方法。文章建議使用語意正確的連結與按鈕、保留清晰的焦點指示,並以 Tab 和 Shift+Tab 實測選單操作及焦點是否被遮擋。開發者亦可記錄頁面、視窗尺寸、瀏覽器、操作序列與結果,方便重現問題。

正文

Orange and black 3D browser navigation and Tab key illustration with a visible focus ring, the original Design Den Solutions logo, +91 98721 90965 and www.designdensolutions.com.

Prepared with AI assistance for Design Den Solutions. The cover is a conceptual AI illustration; the official company logo was composited unchanged. The examples below are illustrative code, not a claim about a client site.

A navigation menu can be perfectly usable with a mouse and almost impossible with a keyboard. The problem is often small: a design reset removes the browser outline, a sticky header covers the focused link, or a dropdown opens only on hover. Those are interaction failures, not merely cosmetic differences.

Start with semantic controls

Use links for navigation and buttons for actions. A <div> with a click handler does not gain keyboard activation, focusability or expected semantics merely because it looks like a menu item. A short, ordinary navigation pattern gives the browser useful defaults:

<nav aria-label="Primary">
  <a href="/services/">Services</a>
  <a href="/work/">Work</a>
  <a href="/contact/">Contact</a>
</nav>

Tab should reach each link in a sensible order. Enter should activate it. If a control reveals more options without navigating, make that control a button and expose its expanded state. Avoid inventing a custom keyboard model when ordinary links and buttons already fit the job.

Make the focused item obvious

The default browser focus indicator is valuable. If a design system replaces it, give users an equally clear state. For example:

nav a:focus-visible,
nav button:focus-visible {
  outline: 3px solid #ff6b1a;
  outline-offset: 3px;
  border-radius: 4px;
}

:focus-visible lets the browser decide when a focus indicator is appropriate for the current interaction. It commonly appears during keyboard navigation, but the matching rules are browser heuristics, so do not describe it as a keyboard-only switch. The W3C's Focus Visible explanation says keyboard-operable interfaces need a mode in which focus is visible. MDN explains that outline-offset creates space between an outline and the element's edge.

Do not write outline: none as a broad reset and hope another selector compensates later. Check the rendered state on the actual backgrounds, including dark headers, photos and mobile menu panels. A bright orange ring on white can be clear while the same ring against an orange hero disappears.

Test the path, not just the CSS rule

Open the page fresh, leave the mouse alone and press Tab. Can you tell where focus starts? Can you reach the menu trigger, open it, reach its options and leave it? Can you return with Shift+Tab? If the menu closes as soon as focus moves into it, a perfect outline does not solve the flow.

Test at a narrow viewport too. A sticky header can obscure the focused element after the page scrolls. The W3C treats Focus Not Obscured (Minimum) as a separate requirement: the focus indicator and target need to remain perceivable when navigation moves. Check zoom and any fixed cookie banner or chat widget as well.

Keep a compact review record

For each important navigation pattern, record the page, viewport, browser, control sequence and observed result. A useful issue is specific: “From the menu button, Tab moves to the first link, but the panel closes and the focus ring is hidden.” That gives the developer a reproducible state to fix. Retest with mouse and touch after changing focus or menu logic so the interaction remains coherent across input methods.

For teams reviewing a site handover, this keyboard path belongs beside form and mobile checks. Our website development service covers practical site builds and handovers; the steps above are useful for any developer auditing a navigation component.

Sources: W3C Focus Visible, W3C Focus Not Obscured, and MDN outline-offset.

來源:DEV Community · dev.to