导航

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 显示的工具提示,屏幕阅读器就会在读出字段时一并读出说明,不必先找到按钮。隐藏中的弹出层仍会被当作描述读出。
  • 为每个分区添加标题,再用 aria-labelledby 将 <section> 关联到该标题,为这组控件提供无障碍名称。
  • 操作按钮堆叠时只改变视觉顺序。文档顺序保持原样,按 Tab 键仍会先聚焦取消按钮,再聚焦确认按钮。
  • 容器需要由所在布局提供宽度。如果表单位于按内容收缩的盒子内,例如父元素使用 inline-flex,或弹出层未指定宽度,请为表单设置宽度。

#类名

类名说明
form表单字段以等间距纵向排列,所在容器是名为 form 的尺寸查询容器。其宽度必须由外部布局决定。
form-row表单宽度达到 28rem 时,字段在同一行中等宽排列,低于该宽度时纵向堆叠
form-section将标题与它说明的字段放在同一分区。表单宽度达到 40rem 时,标题移到字段旁,占三分之一宽度。分区之间用细线隔开
form-heading分区的标题和描述
form-title分区标题
form-description分区标题下的一句话
form-fields分区内的字段沿用 form 的字段间距
form-actions表单底部的操作按钮组,确认按钮排在最后。form 宽度低于 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;    }  }}