ナビゲーション

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キーを押します。

#マークアップ

動作に必要な指定は2つです。

  1. リンクをヘッダーの前に置き、ページで最初にフォーカスできる要素にします。リンクのhrefには、メイン領域のIDを指すフラグメントを指定します。
  2. 移動先にtabindex="-1"を指定します。フラグメントリンクはどのブラウザーでも移動先までスクロールしますが、フォーカスが移るのは移動先がフォーカス可能な場合だけです。この指定がないと、次にTabを押したときに、飛ばしたはずのヘッダーにフォーカスが戻ります。

リンクのテキストには「本文へ移動」のように、何が起こるかを書きます。長いサイドバーがあるページでは、その次に「ナビゲーションへ移動」という2つ目のリンクを置くこともできます。

#表示の仕組み

通常時はビューポートの上端より外側にあり、透明でクリックできませんが、Tabキーでのフォーカス順には残ります。要素にdisplay: noneを指定すると、フォーカス順からも外れます。フォーカスを受けると、文字色で塗った小さなコントロールが上から現れ、ビューポート上部の角から8px内側に表示されます。スクロールに追従するナビゲーションバーを含め、ページ上のほかの要素より前面に重なります。重なり順のz-indexは--skip-layerで指定します。

スキップリンクは、ビューポートの角に自動で配置される数少ないコンポーネントの1つです。ノッチのあるスマートフォンでは、ノッチを避けてセーフエリア内に配置されます。

#アクセシビリティ

  • 本文の前にナビゲーションがあるページには、スキップリンクが必要です。WCAGの達成基準2.4.1「ブロックスキップ」では、複数のページで繰り返されるコンテンツを飛ばす手段が求められます。スキップリンクは、その一般的な実装方法です。
  • リンクは:focus-visibleだけでなく:focusでも表示されるため、スイッチ操作やスクリーンリーダー固有のナビゲーションでリンクに移動したときにも表示されます。
  • 動きを抑える設定が有効な場合は、上から移動するアニメーションを省き、所定の位置にすぐ表示されます。
  • ページに<main>に相当する領域が複数ある場合は、移動先に見出しまたはaria-labelで名前を付け、利用者がどこに移動したかを把握できるようにします。

#クラス

クラス説明
skipid を参照するアンカーに適用します。上部の角から 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;    }  }}