網站導覽

v2.1.0
版面配置

表單

讓列、區段和操作按鈕隨欄寬調整,維持一致的表單版面配置。

本頁內容

表單版面配置統一了欄位之間的垂直間距,讓短欄位並排,並將操作按鈕放在表單末尾。這些類別只負責版面配置,並非元件。它們不提供外觀樣式,也不管理狀態,只依可用寬度決定何時改為垂直排列。

表單本身是尺寸容器,因此各列和區段會依表單所在的欄寬調整,與檢視區寬度無關。同一份標記在設定頁面上可排成兩欄,在窄對話框中則排成一欄。這些切換門檻由類別統一處理,無須自行組合多個工具類別。

表單

<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 時則改為垂直排列。每列放兩到三個短欄位即可。

#區段

form-section 將標題放在對應欄位的旁邊。表單寬度達 40rem 時,標題占三分之一,欄位占其餘空間,形成設定頁面常見的版面配置。低於 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 時,標題移至欄位旁,占三分之一寬度。區段之間以細線分隔
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;    }  }}