網站導覽

v2.1.0
網站導覽

略過連結

頁面上的第一個連結,取得焦點時才顯示,可略過導覽並跳到內容。

本頁內容

略過連結是頁面上的第一個連結,可略過導覽直接跳到內容,讓鍵盤使用者不必在每一頁逐一按 Tab 經過頁首的所有連結。連結一律保留在文件中,只有取得焦點時才會顯示,因此單用指標瀏覽時看不到它。

略過連結

<a class="skip" href="#content">Skip to content</a><header class="navbar">  <a class="navbar-brand" href="#home">Atlas</a>  <nav class="navbar-nav" aria-label="Main">    <a class="navbar-link" href="#projects" aria-current="page">Projects</a>    <a class="navbar-link" href="#deployments">Deployments</a>    <a class="navbar-link" href="#settings">Settings</a>  </nav></header><main class="p-lg" id="content" tabindex="-1">  <h1 class="heading">Projects</h1>  <p class="lead lead-sm">Fourteen projects in three teams.</p></main>
<>  <a className="skip" href="#content">Skip to content</a>  <header className="navbar">    <a className="navbar-brand" href="#home">Atlas</a>    <nav className="navbar-nav" aria-label="Main">      <a className="navbar-link" href="#projects" aria-current="page">Projects</a>      <a className="navbar-link" href="#deployments">Deployments</a>      <a className="navbar-link" href="#settings">Settings</a>    </nav>  </header>  <main className="p-lg" id="content" tabIndex={-1}>    <h1 className="heading">Projects</h1>    <p className="lead lead-sm">Fourteen projects in three teams.</p>  </main></>
<a class="skip" href="#content">Skip to content</a><header class="navbar">  <a class="navbar-brand" href="#home">Atlas</a>  <nav class="navbar-nav" aria-label="Main">    <a class="navbar-link" href="#projects" aria-current="page">Projects</a>    <a class="navbar-link" href="#deployments">Deployments</a>    <a class="navbar-link" href="#settings">Settings</a>  </nav></header><main class="p-lg" id="content" tabindex="-1">  <h1 class="heading">Projects</h1>  <p class="lead lead-sm">Fourteen projects in three teams.</p></main>
<template>  <a class="skip" href="#content">Skip to content</a>  <header class="navbar">    <a class="navbar-brand" href="#home">Atlas</a>    <nav class="navbar-nav" aria-label="Main">      <a class="navbar-link" href="#projects" aria-current="page">Projects</a>      <a class="navbar-link" href="#deployments">Deployments</a>      <a class="navbar-link" href="#settings">Settings</a>    </nav>  </header>  <main class="p-lg" id="content" tabindex="-1">    <h1 class="heading">Projects</h1>    <p class="lead lead-sm">Fourteen projects in three teams.</p>  </main></template>
在預覽內點一下,再按 Tab。

#標記

設定時請確認以下兩點:

  1. 將連結放在頁首之前,讓它成為頁面上第一個可取得焦點的元素,並將 href 指向主要內容區域的 ID。
  2. 在目標元素上設定 tabindex="-1"。所有瀏覽器都會將頁面捲動到片段連結的目標,但目標必須能取得焦點,焦點才會移過去。否則,下一次按 Tab 時,焦點會回到剛略過的頁首。

在連結文字中寫明用途,例如「跳到主要內容」。如果頁面有很長的側邊欄,可以在第一個連結後加上第二個連結「跳到導覽」。

#顯示方式

平時連結位於檢視區上緣之外,完全透明,也無法點按,但仍保有 Tab 順序中的位置。若使用 display: none,連結本身也會被跳過。取得焦點時,它會滑入距檢視區上方角落 8px 的位置,以深色小型控制項呈現,並疊在黏附定位的導覽列與其他頁面內容上方。--skip-layer 設定它的堆疊層級。

略過連結會自行定位在檢視區角落,是少數自帶定位的元件之一。在有螢幕缺口的手機上,連結會留在安全區域內,避開缺口。

#無障礙

  • 凡是導覽位於內容之前的頁面,都需要提供略過連結。WCAG 2.4.1「略過區塊」要求提供略過各頁重複內容的方法,略過連結是常見做法。
  • 連結會在 :focus 時顯示,觸發範圍涵蓋 :focus-visible 以外的情況。因此,使用者透過輔助開關或螢幕閱讀器內建的導覽功能移到連結時,也會顯示。
  • 使用者偏好減少動態效果時,連結會直接出現在定位處,省略滑入動畫。
  • 若頁面上有多個類似 <main> 的區域,請用標題或 aria-label 為目標區域命名,讓使用者跳轉後知道所在區域。

#類別

類別說明
skip使用指向目標 id 的錨點連結,以前景色呈現為小型控制項,固定在頂角內側 8px 處。取得焦點時才顯示,較長的標籤會在檢視區內換行。

#自訂屬性

屬性預設值說明
--skip-layer1000讓元件顯示在黏附式導覽列上方的 z-index

#原始碼

@master/ui/components/skip.css
/* * Skip: the first link of a page, which jumps past the navigation to the content. * It is in the document for everyone and on the screen only while it has focus: a reader who starts with the * Tab key meets it first, and nobody else sees it. Focused, it drops in at the top corner of the viewport as a * small control of ink, over everything else on the page. * It stands there because standing there is its job: fixed, 8px from the corner, clear of a notch. * Geometry: 38px tall with a control's corner, as a button is. */@layer components.ui {  .skip {    --skip-layer: 1000;    position: fixed;    z-index: var(--skip-layer);    inset-block-start: max(.5rem, env(safe-area-inset-top));    inset-inline-start: max(.5rem, env(safe-area-inset-left));    display: inline-flex;    align-items: center;    box-sizing: border-box;    min-height: 2.375rem;    padding: 0 1rem;    border-radius: calc(var(--radius-control) * var(--corner-scale));    corner-shape: var(--corner-shape);    @apply --control-ink;    color: var(--color-text-inverse);    font: var(--font-weight-control) .84375rem/1.2 var(--font-family-sans);    font-feature-settings: inherit;    text-decoration: none;    /*     * A long label wraps inside the viewport instead of leaving it: the width of the viewport less its scrollbar     * (a percentage of a fixed box's viewport, where 100vw would count the scrollbar too) and both insets.     */    max-width: calc(100% - max(.5rem, env(safe-area-inset-left)) - max(.5rem, env(safe-area-inset-right)));    padding-block: .5rem;    text-wrap: balance;    /* At rest it is above the viewport and cannot be pressed; it keeps its place in the tab order. */    opacity: 0;    translate: 0 calc(-100% - 1rem);    pointer-events: none;    transition: translate var(--duration-quick) var(--easing-glide), opacity var(--duration-quick) var(--easing-glide);    &:focus {      opacity: 1;      translate: 0 0;      pointer-events: auto;    }    &:focus-visible {      @apply --focus-ring;    }    /* Shown, it answers as the primary button does: a lighter face under the pointer, the well under a press. */    @media (hover: hover) {      &:focus:hover {        @apply --control-ink-hover;      }    }    &:focus:active {      @apply --control-ink-pressed;    }    /* Where words run from the right, the start corner is the right one, clear of the notch on that side. */    &:dir(rtl) {      inset-inline-start: max(.5rem, env(safe-area-inset-right));    }    @media (pointer: coarse) {      &::after {        @apply --touch-target;      }    }    /* With reduced motion it appears where it stands, without the drop, and still fades in and out. */    @media (prefers-reduced-motion: reduce) {      translate: none;      transition-property: opacity;    }    @media (forced-colors: active) {      outline: 1px solid ButtonText;      outline-offset: -1px;    }  }}