ナビゲーション

v2.1.0
レイアウト

フォーム

フォームの幅に応じて行、セクション、操作ボタンを配置するレイアウト。

このページの内容

フォームレイアウトは、フィールドの縦方向の間隔をそろえ、短いフィールドを横に並べ、末尾に操作ボタンを配置します。レイアウト専用で、独自の背景面や状態を持たず、幅に応じて縦並びに切り替えます。

フォームはサイズコンテナーです。行やセクションの配置は、ビューポートではなくフォームに割り当てられた幅に従います。同じマークアップでも、設定ページでは2列、狭いダイアログでは1列になります。この切り替え幅をまとめて扱うため、複数のユーティリティを組み合わせる代わりに専用クラスを使います。

フォーム

<form class="form max-w(480) mx-auto">  <div class="form-row">    <label class="field">      First name<input class="input" name="given-name" autocomplete="given-name" value="Ada">    </label>    <label class="field">      Last name<input class="input" name="family-name" autocomplete="family-name" value="Lovelace">    </label>  </div>  <div class="field">    <label for="form-email">Email</label><input class="input" id="form-email" type="email" name="email" autocomplete="email" value="ada@example.com" aria-describedby="form-email-hint"><span class="field-hint" id="form-email-hint">We send a receipt here.</span>  </div>  <label class="field">Message<textarea class="input input-auto" name="message"></textarea></label>  <div class="form-actions">    <button class="btn" type="button">Cancel</button>    <button class="btn btn-primary">Send</button>  </div></form>
<form className="form max-w(480) mx-auto">  <div className="form-row">    <label className="field">      First name<input className="input" name="given-name" autoComplete="given-name" defaultValue="Ada" />    </label>    <label className="field">      Last name<input className="input" name="family-name" autoComplete="family-name" defaultValue="Lovelace" />    </label>  </div>  <div className="field">    <label htmlFor="form-email">Email</label><input className="input" id="form-email" type="email" name="email" autoComplete="email" defaultValue="ada@example.com" aria-describedby="form-email-hint" /><span className="field-hint" id="form-email-hint">We send a receipt here.</span>  </div>  <label className="field">    Message<textarea className="input input-auto" name="message">{``}</textarea>  </label>  <div className="form-actions">    <button className="btn" type="button">Cancel</button>    <button className="btn btn-primary">Send</button>  </div></form>
<form class="form max-w(480) mx-auto">  <div class="form-row">    <label class="field">      First name<input class="input" name="given-name" autocomplete="given-name" value="Ada">    </label>    <label class="field">      Last name<input class="input" name="family-name" autocomplete="family-name" value="Lovelace">    </label>  </div>  <div class="field">    <label for="form-email">Email</label><input class="input" id="form-email" type="email" name="email" autocomplete="email" value="ada@example.com" aria-describedby="form-email-hint"><span class="field-hint" id="form-email-hint">We send a receipt here.</span>  </div>  <label class="field">Message<textarea class="input input-auto" name="message"></textarea></label>  <div class="form-actions">    <button class="btn" type="button">Cancel</button>    <button class="btn btn-primary">Send</button>  </div></form>
<template>  <form class="form max-w(480) mx-auto">    <div class="form-row">      <label class="field">        First name<input class="input" name="given-name" autocomplete="given-name" value="Ada">      </label>      <label class="field">        Last name<input class="input" name="family-name" autocomplete="family-name" value="Lovelace">      </label>    </div>    <div class="field">      <label for="form-email">Email</label><input class="input" id="form-email" type="email" name="email" autocomplete="email" value="ada@example.com" aria-describedby="form-email-hint"><span class="field-hint" id="form-email-hint">We send a receipt here.</span>    </div>    <label class="field">      Message<textarea class="input input-auto" name="message"></textarea>    </label>    <div class="form-actions">      <button class="btn" type="button">Cancel</button>      <button class="btn btn-primary">Send</button>    </div>  </form></template>

#行

form-rowには、姓と名、市区町村と郵便番号など、ひとまとまりで読めるフィールドを配置します。フォームの幅が28rem以上なら同じ幅の列に横並びになり、28rem未満なら縦に並びます。1行に並べる短いフィールドは2〜3個に収めます。

#セクション

form-sectionは見出しと、それに対応するフィールドを横に並べます。フォームの幅が40rem以上なら、設定ページのように見出しが幅の3分の1を占め、残りにフィールドを配置します。40rem未満では、見出しがフィールドの上に移ります。セクション同士は極細の線で区切ります。

セクション

<form class="form w-full">  <section class="form-section" aria-labelledby="form-profile">    <div class="form-heading">      <h3 class="form-title" id="form-profile">Profile</h3>      <p class="form-description">Shown on your public page and next to your comments.</p>    </div>    <div class="form-fields">      <div class="form-row">        <label class="field">          First name<input class="input" name="given-name" autocomplete="given-name" value="Ada">        </label>        <label class="field">          Last name<input class="input" name="family-name" autocomplete="family-name" value="Lovelace">        </label>      </div>      <label class="field">Bio<textarea class="input input-auto"></textarea></label>    </div>  </section>  <section class="form-section" aria-labelledby="form-notify">    <div class="form-heading">      <h3 class="form-title" id="form-notify">Notifications</h3>      <p class="form-description">Choose what reaches your inbox.</p>    </div>    <div class="form-fields">      <label class="check">        <input type="checkbox" class="checkbox" checked> Mentions and replies      </label>      <label class="check"><input type="checkbox" class="checkbox"> A weekly summary</label>    </div>  </section>  <div class="form-actions">    <button class="btn" type="button">Discard</button>    <button class="btn btn-primary">Save changes</button>  </div></form>
<form className="form w-full">  <section className="form-section" aria-labelledby="form-profile">    <div className="form-heading">      <h3 className="form-title" id="form-profile">Profile</h3>      <p className="form-description">Shown on your public page and next to your comments.</p>    </div>    <div className="form-fields">      <div className="form-row">        <label className="field">          First name<input className="input" name="given-name" autoComplete="given-name" defaultValue="Ada" />        </label>        <label className="field">          Last name<input className="input" name="family-name" autoComplete="family-name" defaultValue="Lovelace" />        </label>      </div>      <label className="field">Bio<textarea className="input input-auto">{``}</textarea></label>    </div>  </section>  <section className="form-section" aria-labelledby="form-notify">    <div className="form-heading">      <h3 className="form-title" id="form-notify">Notifications</h3>      <p className="form-description">Choose what reaches your inbox.</p>    </div>    <div className="form-fields">      <label className="check">        <input type="checkbox" className="checkbox" defaultChecked /> Mentions and replies      </label>      <label className="check">        <input type="checkbox" className="checkbox" /> A weekly summary      </label>    </div>  </section>  <div className="form-actions">    <button className="btn" type="button">Discard</button>    <button className="btn btn-primary">Save changes</button>  </div></form>
<form class="form w-full">  <section class="form-section" aria-labelledby="form-profile">    <div class="form-heading">      <h3 class="form-title" id="form-profile">Profile</h3>      <p class="form-description">Shown on your public page and next to your comments.</p>    </div>    <div class="form-fields">      <div class="form-row">        <label class="field">          First name<input class="input" name="given-name" autocomplete="given-name" value="Ada">        </label>        <label class="field">          Last name<input class="input" name="family-name" autocomplete="family-name" value="Lovelace">        </label>      </div>      <label class="field">Bio<textarea class="input input-auto"></textarea></label>    </div>  </section>  <section class="form-section" aria-labelledby="form-notify">    <div class="form-heading">      <h3 class="form-title" id="form-notify">Notifications</h3>      <p class="form-description">Choose what reaches your inbox.</p>    </div>    <div class="form-fields">      <label class="check">        <input type="checkbox" class="checkbox" checked> Mentions and replies      </label>      <label class="check"><input type="checkbox" class="checkbox"> A weekly summary</label>    </div>  </section>  <div class="form-actions">    <button class="btn" type="button">Discard</button>    <button class="btn btn-primary">Save changes</button>  </div></form>
<template>  <form class="form w-full">    <section class="form-section" aria-labelledby="form-profile">      <div class="form-heading">        <h3 class="form-title" id="form-profile">Profile</h3>        <p class="form-description">Shown on your public page and next to your comments.</p>      </div>      <div class="form-fields">        <div class="form-row">          <label class="field">            First name<input class="input" name="given-name" autocomplete="given-name" value="Ada">          </label>          <label class="field">            Last name<input class="input" name="family-name" autocomplete="family-name" value="Lovelace">          </label>        </div>        <label class="field">Bio<textarea class="input input-auto"></textarea></label>      </div>    </section>    <section class="form-section" aria-labelledby="form-notify">      <div class="form-heading">        <h3 class="form-title" id="form-notify">Notifications</h3>        <p class="form-description">Choose what reaches your inbox.</p>      </div>      <div class="form-fields">        <label class="check">          <input type="checkbox" class="checkbox" checked> Mentions and replies        </label>        <label class="check"><input type="checkbox" class="checkbox"> A weekly summary</label>      </div>    </section>    <div class="form-actions">      <button class="btn" type="button">Discard</button>      <button class="btn btn-primary">Save changes</button>    </div>  </form></template>

#ラベル横のヘルプ

一度読めば十分で、読まなくても入力できる補足情報は、ラベルの後ろの疑問符に収めます。ツールチップを表示する field-help ボタンです。ラベルとボタンを flex items-baseline gap(6) の行に入れると、疑問符はラベルの最後の文字に続き、ラベルのベースラインに揃います。フィールドのリズムは変わらず、コントロールの下に行が増えることもありません。

ラベル横のヘルプ

<form class="form max-w(480) mx-auto">  <label class="field">File name<input class="input" name="filename" value="icons"></label>  <div class="field">    <div class="flex items-baseline gap(6)">      <label for="form-size">Display size</label>      <button type="button" class="field-help" interestfor="form-size-help" popovertarget="form-size-help" aria-label="About display size">        ?      </button>    </div>    <input type="range" class="slider" id="form-size" name="size" min="16" max="64" value="48" aria-describedby="form-size-help">    <div id="form-size-help" popover="hint" class="tooltip">      Sizes above 24 px are scaled from the 24 px master. The ZIP holds the 24 px SVG.    </div>  </div>  <div class="form-actions">    <button class="btn" type="button">Cancel</button>    <button class="btn btn-primary">Download</button>  </div></form>
<form className="form max-w(480) mx-auto">  <label className="field">    File name<input className="input" name="filename" defaultValue="icons" />  </label>  <div className="field">    <div className="flex items-baseline gap(6)">      <label htmlFor="form-size">Display size</label>      <button type="button" className="field-help" {...{ interestfor: 'form-size-help' }} popoverTarget="form-size-help" aria-label="About display size">        ?      </button>    </div>    <input type="range" className="slider" id="form-size" name="size" min="16" max="64" defaultValue="48" aria-describedby="form-size-help" />    <div id="form-size-help" popover="hint" className="tooltip">      Sizes above 24 px are scaled from the 24 px master. The ZIP holds the 24 px SVG.    </div>  </div>  <div className="form-actions">    <button className="btn" type="button">Cancel</button>    <button className="btn btn-primary">Download</button>  </div></form>
<form class="form max-w(480) mx-auto">  <label class="field">File name<input class="input" name="filename" value="icons"></label>  <div class="field">    <div class="flex items-baseline gap(6)">      <label for="form-size">Display size</label>      <button type="button" class="field-help" interestfor="form-size-help" popovertarget="form-size-help" aria-label="About display size">        ?      </button>    </div>    <input type="range" class="slider" id="form-size" name="size" min="16" max="64" value="48" aria-describedby="form-size-help">    <div id="form-size-help" popover="hint" class="tooltip">      Sizes above 24 px are scaled from the 24 px master. The ZIP holds the 24 px SVG.    </div>  </div>  <div class="form-actions">    <button class="btn" type="button">Cancel</button>    <button class="btn btn-primary">Download</button>  </div></form>
<template>  <form class="form max-w(480) mx-auto">    <label class="field">File name<input class="input" name="filename" value="icons"></label>    <div class="field">      <div class="flex items-baseline gap(6)">        <label for="form-size">Display size</label>        <button type="button" class="field-help" interestfor="form-size-help" popovertarget="form-size-help" aria-label="About display size">          ?        </button>      </div>      <input type="range" class="slider" id="form-size" name="size" min="16" max="64" value="48" aria-describedby="form-size-help">      <div id="form-size-help" popover="hint" class="tooltip">        Sizes above 24 px are scaled from the 24 px master. The ZIP holds the 24 px SVG.      </div>    </div>    <div class="form-actions">      <button class="btn" type="button">Cancel</button>      <button class="btn btn-primary">Download</button>    </div>  </form></template>
疑問符にホバーするか、Tabキーで移動するか、タップします。

ボタンは二つの属性で同じツールチップを指します。interestfor はホバーとキーボードフォーカスで表示し、popovertarget はクリックまたはタップで表示します。タッチスクリーンにはホバーがないためです。疑問符は名前にならないので、aria-label で何を説明するボタンかを伝えます。ボタンは <label> の外に置きます。中に置くと、疑問符がコントロールの名前の一部になります。

入力に必要な情報は、field-hint で常に表示します。ツールチップに入れるのは一、二文のプレーンテキストだけで、クリックできるものは入れません。

#操作ボタン

form-actionsはフォームの末尾に置きます。ボタンを行の末尾に寄せ、確定ボタンを最後に配置します。フォームの幅が24rem未満になると、各ボタンが全幅で縦に並び、親指で押しやすい先頭に確定ボタンが来ます。マークアップでは読み順に従い、キャンセル、確定の順に記述します。縦並びのときはスタイルシートが表示順を反転します。

狭い列での表示

<form class="form max-w(300) mx-auto">  <div class="form-row">    <label class="field">      City<input class="input" name="city" autocomplete="address-level2" value="London">    </label>    <label class="field">      Postcode<input class="input" name="postcode" autocomplete="postal-code" value="N1 9GU">    </label>  </div>  <div class="form-actions">    <button class="btn" type="button">Cancel</button>    <button class="btn btn-primary">Save address</button>  </div></form>
<form className="form max-w(300) mx-auto">  <div className="form-row">    <label className="field">      City<input className="input" name="city" autoComplete="address-level2" defaultValue="London" />    </label>    <label className="field">      Postcode<input className="input" name="postcode" autoComplete="postal-code" defaultValue="N1 9GU" />    </label>  </div>  <div className="form-actions">    <button className="btn" type="button">Cancel</button>    <button className="btn btn-primary">Save address</button>  </div></form>
<form class="form max-w(300) mx-auto">  <div class="form-row">    <label class="field">      City<input class="input" name="city" autocomplete="address-level2" value="London">    </label>    <label class="field">      Postcode<input class="input" name="postcode" autocomplete="postal-code" value="N1 9GU">    </label>  </div>  <div class="form-actions">    <button class="btn" type="button">Cancel</button>    <button class="btn btn-primary">Save address</button>  </div></form>
<template>  <form class="form max-w(300) mx-auto">    <div class="form-row">      <label class="field">        City<input class="input" name="city" autocomplete="address-level2" value="London">      </label>      <label class="field">        Postcode<input class="input" name="postcode" autocomplete="postal-code" value="N1 9GU">      </label>    </div>    <div class="form-actions">      <button class="btn" type="button">Cancel</button>      <button class="btn btn-primary">Save address</button>    </div>  </form></template>
同じクラスを幅300pxで使った例です。行内のフィールドと操作ボタンは自動で縦に並びます。

#アクセシビリティ

  • フォームのクラスはレイアウト専用です。ラベル、ヒント、エラーメッセージにはfieldを使い、関連するコントロールのグループ化にはfieldsetを使います。
  • コントロールの aria-describedby を field-help が表示するツールチップに向けると、スクリーンリーダーはボタンを探さなくても、フィールドと一緒にヘルプを読み上げます。非表示のポップオーバーも説明として読み上げられます。
  • 各セクションに見出しを付け、<section>からaria-labelledbyでその見出しを参照すると、グループに名前が付きます。
  • 操作ボタンを縦に並べると、画面上の順序だけが変わります。文書内の順序とTabキーでの移動順は、キャンセル、確定のままです。
  • コンテナーには、配置先に応じた幅が必要です。inline-flexの親要素や幅を指定していないポップオーバーなど、内容に合わせて幅が縮むボックス内では、フォームに幅を指定します。

#クラス

クラス説明
formフィールドを等間隔で並べる列で、form という名前のサイズコンテナーです。幅は外側のレイアウトから決まる必要があります。
form-rowフィールドをひとまとまりの行として扱います。フォーム幅が 28rem 以上では等幅の列に横並びにし、それ未満では縦に並べます。
form-section見出しと、その見出しが説明するフィールドをまとめます。フォーム幅が 40rem 以上になると、見出しがフィールドの横に並び、幅の 3 分の 1 を占めます。セクション間は細い線で区切ります。
form-headingセクションのタイトルと説明
form-titleセクションのタイトル
form-descriptionセクションのタイトルの下に置く一文
form-fieldsフォーム所定の間隔で並ぶ、セクション内のフィールド。
form-actionsフォーム末尾に置く操作ボタンです。確定ボタンは最後に置きます。フォーム幅が 24rem 未満では、各ボタンを全幅で縦に並べ、確定ボタンを先頭に置きます。

#カスタムプロパティ

プロパティ既定値説明
--form-gap1.25remフィールド間の間隔。

#ソース

@master/ui/layouts/form.css
/* * Form: the rhythm of a form. Fields stack at one distance, a row puts short fields side by side, a section * sets a heading beside its fields, and the actions close it. * This is a layout: it draws no surface and holds no state. What it knows is when to stack. The form is a * size container, so rows and sections follow the column the form is given, not the viewport: in a narrow * dialog they stack by themselves. * The width must come from the context; inside a shrink-to-fit box a container collapses. */@layer components.ui {  .form {    --form-gap: 1.25rem;    display: grid;    gap: var(--form-gap);    min-width: 0;    margin: 0;    container: form / inline-size;  }  /* Fields that are read as one line: first and last name, city and postcode. At 28rem of form they share it. */  .form-row {    display: grid;    gap: var(--form-gap) .75rem;    min-width: 0;    @container form (inline-size >= 28rem) {      grid-auto-columns: minmax(0, 1fr);      grid-auto-flow: column;      align-items: start;    }  }  /*   * A section: a heading that says what the fields below are for. From 40rem of form the heading moves beside   * its fields, a third of the width, the way a settings page reads. Sections are parted by a hairline.   */  .form-section {    display: grid;    gap: 1rem var(--form-gap);    min-width: 0;    & + & {      padding-block-start: var(--form-gap);      border-block-start: 1px solid var(--color-line-hair);    }    @container form (inline-size >= 40rem) {      grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);      gap: var(--form-gap) 2rem;    }  }  .form-heading {    display: grid;    align-content: start;    gap: .25rem;    min-width: 0;  }  .form-title {    margin: 0;    color: var(--color-text-strong);    font-size: .9375rem;    font-weight: var(--font-weight-control);    line-height: 1.4;    letter-spacing: -.005em;    text-wrap: balance;    overflow-wrap: anywhere;  }  .form-description {    margin: 0;    color: var(--color-text-muted);    font-size: .8125rem;    line-height: 1.55;    text-wrap: pretty;    overflow-wrap: anywhere;  }  .form-fields {    display: grid;    align-content: start;    gap: var(--form-gap);    min-width: 0;  }  /*   * Actions: the buttons that close the form, at its end with the confirming one last. Below 24rem of form   * they stack at full width and the confirming button comes first, under the thumb.   */  .form-actions {    display: flex;    flex-wrap: wrap;    justify-content: flex-end;    gap: .5rem;    min-width: 0;    @container form (inline-size < 24rem) {      flex-direction: column-reverse;      align-items: stretch;    }  }}