按钮
凸起的控件,悬停时抬升,按下时下沉。
本页内容
按钮是一块带有半像素轮廓的小型凸起面板。指针悬停时抬高半像素,按下时沉入凹槽,状态变化靠光影和凹凸来区分,无需改变颜色。执行操作时,将 .btn 用在 <button> 上。需要让导航链接呈现按钮外观时,将同一个类用在 <a> 上。
按钮
<button type="button" class="btn">Cancel</button><button type="button" class="btn btn-primary">Save changes</button><> <button type="button" className="btn">Cancel</button> <button type="button" className="btn btn-primary">Save changes</button></><button type="button" class="btn">Cancel</button><button type="button" class="btn btn-primary">Save changes</button><template> <button type="button" class="btn">Cancel</button> <button type="button" class="btn btn-primary">Save changes</button></template>#变体
默认按钮采用 Moonstone 材质。根据操作的重要程度选择变体:
btn-primary在 Moonstone 上呈墨色,在 Obsidian 上呈珍珠色。每个视图只用一个,用于该视图的核心操作。btn-quiet在交互时才显现材质,适合工具栏、页头和紧凑的行。btn-danger保留凸起材质,标签使用危险状态的前景色。btn-brand是唯一带色相的控件,采用品牌靛蓝,适合首页的主操作或升级等需要突出显示的操作。每个视图最多使用一个。按钮的表面、边缘和光晕都由--color-brand混合生成,随品牌色变化。
变体
<button type="button" class="btn">Default</button><button type="button" class="btn btn-primary">Primary</button><button type="button" class="btn btn-quiet">Quiet</button><button type="button" class="btn btn-danger">Delete</button><button type="button" class="btn btn-brand">Get started</button><> <button type="button" className="btn">Default</button> <button type="button" className="btn btn-primary">Primary</button> <button type="button" className="btn btn-quiet">Quiet</button> <button type="button" className="btn btn-danger">Delete</button> <button type="button" className="btn btn-brand">Get started</button></><button type="button" class="btn">Default</button><button type="button" class="btn btn-primary">Primary</button><button type="button" class="btn btn-quiet">Quiet</button><button type="button" class="btn btn-danger">Delete</button><button type="button" class="btn btn-brand">Get started</button><template> <button type="button" class="btn">Default</button> <button type="button" class="btn btn-primary">Primary</button> <button type="button" class="btn btn-quiet">Quiet</button> <button type="button" class="btn btn-danger">Delete</button> <button type="button" class="btn btn-brand">Get started</button></template>#破坏性操作
单独使用 btn-danger 时,按钮保持凸起,仅标签采用危险状态的前景色,适合与其他操作并列的删除按钮。与 btn-primary 搭配时,按钮用危险色填充,用于确认破坏性操作,填充方式与品牌按钮使用品牌色相同。只在最后一步使用填充样式,例如在对话框内提供确认按钮,并在标签中写明具体操作。
破坏性操作
<button type="button" class="btn btn-danger">Delete</button><button type="button" class="btn btn-primary btn-danger">Delete project</button><button type="button" class="btn btn-primary btn-danger" disabled>Delete project</button><> <button type="button" className="btn btn-danger">Delete</button> <button type="button" className="btn btn-primary btn-danger">Delete project</button> <button type="button" className="btn btn-primary btn-danger" disabled>Delete project</button></><button type="button" class="btn btn-danger">Delete</button><button type="button" class="btn btn-primary btn-danger">Delete project</button><button type="button" class="btn btn-primary btn-danger" disabled>Delete project</button><template> <button type="button" class="btn btn-danger">Delete</button> <button type="button" class="btn btn-primary btn-danger">Delete project</button> <button type="button" class="btn btn-primary btn-danger" disabled>Delete project</button></template>#尺寸
默认高度为 38px,紧凑界面用 32px,需要突出显示的操作用 44px。内边距、标签字号、图标尺寸和圆角半径随尺寸一同变化。在触摸屏上,各尺寸都保持原有高度和外观;按钮周围有一个不可见的触控区域,扩展至 44px,因此一排按钮和输入框仍保持齐平。
尺寸
<button type="button" class="btn btn-sm">Small</button><button type="button" class="btn">Medium</button><button type="button" class="btn btn-lg">Large</button><> <button type="button" className="btn btn-sm">Small</button> <button type="button" className="btn">Medium</button> <button type="button" className="btn btn-lg">Large</button></><button type="button" class="btn btn-sm">Small</button><button type="button" class="btn">Medium</button><button type="button" class="btn btn-lg">Large</button><template> <button type="button" class="btn btn-sm">Small</button> <button type="button" class="btn">Medium</button> <button type="button" class="btn btn-lg">Large</button></template>#搭配图标
将 icon 加到图标本身。图标尺寸由按钮设定,默认 16px,小按钮 14px,大按钮 18px,线宽沿用图稿本身。btn-square 将按钮设为正方形,用 aria-label 为它提供无障碍名称。
搭配图标
<button type="button" class="btn btn-primary"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /><path d="M8 3L8 13" /> </svg> New project</button><button type="button" class="btn btn-square" aria-label="Search"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="7" cy="7" r="4.5" /><path d="M10.182 10.182L14 14" /> </svg></button><button type="button" class="btn btn-quiet btn-square" aria-label="More"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="3.5" cy="8" r="1.05" fill="currentColor" stroke="none" /><circle cx="8" cy="8" r="1.05" fill="currentColor" stroke="none" /><circle cx="12.5" cy="8" r="1.05" fill="currentColor" stroke="none" /> </svg></button>import PlusIcon from '@master/icons-react/native/16/soft/plus'import SearchIcon from '@master/icons-react/native/16/soft/search'import MoreHorizontalIcon from '@master/icons-react/native/16/soft/more-horizontal'<> <button type="button" className="btn btn-primary"> <PlusIcon className="icon" /> New project </button> <button type="button" className="btn btn-square" aria-label="Search"> <SearchIcon className="icon" /> </button> <button type="button" className="btn btn-quiet btn-square" aria-label="More"> <MoreHorizontalIcon className="icon" /> </button></><script> import PlusIcon from '@master/icons-svelte/native/16/soft/plus' import SearchIcon from '@master/icons-svelte/native/16/soft/search' import MoreHorizontalIcon from '@master/icons-svelte/native/16/soft/more-horizontal'</script><button type="button" class="btn btn-primary"><PlusIcon class="icon" /> New project</button><button type="button" class="btn btn-square" aria-label="Search"> <SearchIcon class="icon" /></button><button type="button" class="btn btn-quiet btn-square" aria-label="More"> <MoreHorizontalIcon class="icon" /></button><script setup>import PlusIcon from '@master/icons-vue/native/16/soft/plus'import SearchIcon from '@master/icons-vue/native/16/soft/search'import MoreHorizontalIcon from '@master/icons-vue/native/16/soft/more-horizontal'</script><template> <button type="button" class="btn btn-primary"><PlusIcon class="icon" /> New project</button> <button type="button" class="btn btn-square" aria-label="Search"> <SearchIcon class="icon" /> </button> <button type="button" class="btn btn-quiet btn-square" aria-label="More"> <MoreHorizontalIcon class="icon" /> </button></template>#胶囊形
btn-pill 将两端变成半圆,并略微增加行内方向的留白,因为圆弧端部比平直端部看起来更紧凑。
胶囊形
<button type="button" class="btn btn-pill">Follow</button><button type="button" class="btn btn-primary btn-pill">Subscribe</button><> <button type="button" className="btn btn-pill">Follow</button> <button type="button" className="btn btn-primary btn-pill">Subscribe</button></><button type="button" class="btn btn-pill">Follow</button><button type="button" class="btn btn-primary btn-pill">Subscribe</button><template> <button type="button" class="btn btn-pill">Follow</button> <button type="button" class="btn btn-primary btn-pill">Subscribe</button></template>#切换按钮
低调样式的按钮带有 aria-pressed="true" 时会保持下沉,呈现凹槽效果。用自己的脚本更新该属性。紧跟在这类按钮之后的菜单或弹出层打开时,按钮也会自动保持下沉。
切换按钮
<button type="button" class="btn btn-quiet btn-square" aria-label="Bold" aria-pressed="true"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4.6667 3H8.3333C12.3333 3 12.3333 7.6667 8.3333 7.6667H4.6667M4.6667 3V13H9C13.3333 13 13.3333 7.6667 9 7.6667" /> </svg></button><button type="button" class="btn btn-quiet btn-square" aria-label="Italic" aria-pressed="false"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6.6667 3L12.6667 3" /><path d="M3.3333 13L9.3333 13" /><path d="M9.6667 3L6.3333 13" stroke-linecap="butt" /> </svg></button>import BoldIcon from '@master/icons-react/native/16/soft/bold'import ItalicIcon from '@master/icons-react/native/16/soft/italic'<> <button type="button" className="btn btn-quiet btn-square" aria-label="Bold" aria-pressed="true"> <BoldIcon className="icon" /> </button> <button type="button" className="btn btn-quiet btn-square" aria-label="Italic" aria-pressed="false"> <ItalicIcon className="icon" /> </button></><script> import BoldIcon from '@master/icons-svelte/native/16/soft/bold' import ItalicIcon from '@master/icons-svelte/native/16/soft/italic'</script><button type="button" class="btn btn-quiet btn-square" aria-label="Bold" aria-pressed="true"> <BoldIcon class="icon" /></button><button type="button" class="btn btn-quiet btn-square" aria-label="Italic" aria-pressed="false"> <ItalicIcon class="icon" /></button><script setup>import BoldIcon from '@master/icons-vue/native/16/soft/bold'import ItalicIcon from '@master/icons-vue/native/16/soft/italic'</script><template> <button type="button" class="btn btn-quiet btn-square" aria-label="Bold" aria-pressed="true"> <BoldIcon class="icon" /> </button> <button type="button" class="btn btn-quiet btn-square" aria-label="Italic" aria-pressed="false"> <ItalicIcon class="icon" /> </button></template>#按钮组
btn-group 将相关操作合成一个整体轮廓。每个按钮保留各自的材质,组内相接处的圆角改为直角,相邻轮廓重叠成一道接缝。组内按钮不会单独抬升,以维持整体形状。为按钮组设置 role="group",并提供无障碍名称。
按钮组
<div class="btn-group" role="group" aria-label="History"> <button type="button" class="btn">Undo</button> <button type="button" class="btn">Redo</button></div><div class="btn-group" role="group" aria-label="Deploy"> <button type="button" class="btn btn-primary">Deploy</button> <button type="button" class="btn btn-primary btn-square" aria-label="Deploy options"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" /> </svg> </button></div>import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'<> <div className="btn-group" role="group" aria-label="History"> <button type="button" className="btn">Undo</button> <button type="button" className="btn">Redo</button> </div> <div className="btn-group" role="group" aria-label="Deploy"> <button type="button" className="btn btn-primary">Deploy</button> <button type="button" className="btn btn-primary btn-square" aria-label="Deploy options"> <ChevronDownIcon className="icon" /> </button> </div></><script> import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><div class="btn-group" role="group" aria-label="History"> <button type="button" class="btn">Undo</button> <button type="button" class="btn">Redo</button></div><div class="btn-group" role="group" aria-label="Deploy"> <button type="button" class="btn btn-primary">Deploy</button> <button type="button" class="btn btn-primary btn-square" aria-label="Deploy options"> <ChevronDownIcon class="icon" /> </button></div><script setup>import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'</script><template> <div class="btn-group" role="group" aria-label="History"> <button type="button" class="btn">Undo</button> <button type="button" class="btn">Redo</button> </div> <div class="btn-group" role="group" aria-label="Deploy"> <button type="button" class="btn btn-primary">Deploy</button> <button type="button" class="btn btn-primary btn-square" aria-label="Deploy options"> <ChevronDownIcon class="icon" /> </button> </div></template>分体按钮中的第二个按钮通常用于打开菜单。添加 popovertarget 后,菜单会自动贴着该按钮定位。
#分页
用一排低调样式的方形按钮作为页码链接。当前页的按钮带有 aria-current="page",像按下的切换按钮一样保持下沉,呈现凹槽效果。
分页
<nav class="flex items-center gap(2)" aria-label="Pagination"> <a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(180 8 8)" /> </svg> </a> <a class="btn btn-quiet btn-square btn-sm" href="#page-1">1</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-2" aria-current="page">2</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-3">3</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" /> </svg> </a></nav>import ChevronLeftIcon from '@master/icons-react/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'<nav className="flex items-center gap(2)" aria-label="Pagination"> <a className="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page"> <ChevronLeftIcon className="icon" /> </a> <a className="btn btn-quiet btn-square btn-sm" href="#page-1">1</a> <a className="btn btn-quiet btn-square btn-sm" href="#page-2" aria-current="page">2</a> <a className="btn btn-quiet btn-square btn-sm" href="#page-3">3</a> <a className="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page"> <ChevronRightIcon className="icon" /> </a></nav><script> import ChevronLeftIcon from '@master/icons-svelte/native/16/soft/chevron-left' import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><nav class="flex items-center gap(2)" aria-label="Pagination"> <a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page"> <ChevronLeftIcon class="icon" /> </a> <a class="btn btn-quiet btn-square btn-sm" href="#page-1">1</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-2" aria-current="page">2</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-3">3</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page"> <ChevronRightIcon class="icon" /> </a></nav><script setup>import ChevronLeftIcon from '@master/icons-vue/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'</script><template> <nav class="flex items-center gap(2)" aria-label="Pagination"> <a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page"> <ChevronLeftIcon class="icon" /> </a> <a class="btn btn-quiet btn-square btn-sm" href="#page-1">1</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-2" aria-current="page">2</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-3">3</a> <a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page"> <ChevronRightIcon class="icon" /> </a> </nav></template>#忙碌状态
操作执行期间设置 aria-busy="true"。按钮保留原有标签和材质,不再抬升,并在行内显示一个与按钮图标同尺寸的加载指示器。更新标签以说明正在执行的操作,并在事件处理函数中忽略后续触发。
忙碌状态
<button type="button" class="btn" aria-busy="true"> <span class="spinner spinner-inline" aria-hidden="true"></span> Saving…</button><button type="button" class="btn btn-primary" aria-busy="true"> <span class="spinner spinner-inline" aria-hidden="true"></span> Publishing…</button><> <button type="button" className="btn" aria-busy="true"> <span className="spinner spinner-inline" aria-hidden="true"></span> Saving… </button> <button type="button" className="btn btn-primary" aria-busy="true"> <span className="spinner spinner-inline" aria-hidden="true"></span> Publishing… </button></><button type="button" class="btn" aria-busy="true"> <span class="spinner spinner-inline" aria-hidden="true"></span> Saving…</button><button type="button" class="btn btn-primary" aria-busy="true"> <span class="spinner spinner-inline" aria-hidden="true"></span> Publishing…</button><template> <button type="button" class="btn" aria-busy="true"> <span class="spinner spinner-inline" aria-hidden="true"></span> Saving… </button> <button type="button" class="btn btn-primary" aria-busy="true"> <span class="spinner spinner-inline" aria-hidden="true"></span> Publishing… </button></template>#禁用状态
禁用的按钮沉入表面,呈现平坦的凹槽,标签使用禁用状态的前景色。链接本身不支持禁用,需使用 aria-disabled="true" 并移除 href。如果禁用的原因并不明显,请在按钮旁用文字说明,并通过 aria-describedby 关联:禁用按钮上的工具提示无法通过键盘或手指触达。
禁用状态
<div class="flex flex-col items-center gap-xs"> <div class="flex flex-wrap justify-center gap-sm"> <button type="button" class="btn" disabled>Default</button> <button type="button" class="btn btn-primary" disabled aria-describedby="publish-reason"> Publish </button> </div> <p class="m(0) text-sm fg-text-muted" id="publish-reason">Add a title before you publish.</p></div><div className="flex flex-col items-center gap-xs"> <div className="flex flex-wrap justify-center gap-sm"> <button type="button" className="btn" disabled>Default</button> <button type="button" className="btn btn-primary" disabled aria-describedby="publish-reason"> Publish </button> </div> <p className="m(0) text-sm fg-text-muted" id="publish-reason">Add a title before you publish.</p></div><div class="flex flex-col items-center gap-xs"> <div class="flex flex-wrap justify-center gap-sm"> <button type="button" class="btn" disabled>Default</button> <button type="button" class="btn btn-primary" disabled aria-describedby="publish-reason"> Publish </button> </div> <p class="m(0) text-sm fg-text-muted" id="publish-reason">Add a title before you publish.</p></div><template> <div class="flex flex-col items-center gap-xs"> <div class="flex flex-wrap justify-center gap-sm"> <button type="button" class="btn" disabled>Default</button> <button type="button" class="btn btn-primary" disabled aria-describedby="publish-reason"> Publish </button> </div> <p class="m(0) text-sm fg-text-muted" id="publish-reason">Add a title before you publish.</p> </div></template>#调整单个实例
utilities 层排在 components 层之后,因此工具类优先于组件样式:
占满宽度
<button type="button" class="btn btn-primary btn-lg w-full">Continue</button><button type="button" className="btn btn-primary btn-lg w-full">Continue</button><button type="button" class="btn btn-primary btn-lg w-full">Continue</button><template> <button type="button" class="btn btn-primary btn-lg w-full">Continue</button></template>#类名
| 类名 | 说明 |
|---|---|
btn | 采用凸起样式的基础按钮 |
btn-primary | 用于视图的主要操作。Moonstone 中使用墨色,Obsidian 中使用珍珠色。与 btn-danger 搭配时,以危险色填充,用于确认破坏性操作与 btn 一同使用 |
btn-quiet | 用于工具栏、标题栏和紧凑的行,交互时才显现表面样式与 btn 一同使用 |
btn-brand | 视图中唯一使用彩色填充的控件,填充色取自 --color-brand。每个视图最多使用一个与 btn 一同使用 |
btn-danger | 破坏性操作标签使用的危险墨色与 btn 一同使用 |
btn-sm | 高度为 32px,默认为 38px与 btn 一同使用 |
btn-lg | 高度为 44px,默认为 38px与 btn 一同使用 |
btn-square | 只放一个图标的方形按钮,需提供可访问名称。与 btn 一同使用 |
btn-pill | 两端全圆与 btn 一同使用 |
btn-hang-start | 用于没有外框的横栏或页头开头的 quiet 按钮:按钮框伸入内边距,使图形与内容的起始边缘对齐与 btn 一同使用 |
btn-hang-end | 用于结尾的同一做法:图形与内容的结束边缘对齐与 btn 一同使用 |
btn-group | 将多个按钮连成一个整体的容器。 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--btn-height | 2.375rem | 最小块轴尺寸 |
--btn-padding-x | 1rem | 行内方向的内边距 |
--btn-gap | .5rem | 图标与标签的间距 |
--btn-font-size | .84375rem | 标签字号 |
--btn-radius | var(--radius-control) | 默认圆角半径为 10px,小按钮用 8px,大按钮用 12px |
--icon-size | 1rem | 按钮内的图标尺寸 |