基本单选按钮
与复选框类似,单选按钮使用 .radio 类设置样式。不过,我们没有使用自定义 SVG,因为我们使用纯 CSS 为输入框及其选中状态设置样式。
没有可见标签的单选按钮仍然需要一个可供辅助技术使用的无障碍名称,例如使用 aria-label。该名称应说明该选项是什么,例如颜色选择器中的 aria-label="Red",并替换下面示例中的 ... 占位符。详情请参阅表单概览无障碍部分。
<input type="radio" id="radio" class="radio" aria-label="..." />配合表单字段
将 .radio 包裹在 .form-field 布局包装器中,并添加你的标签。网格布局会将单选按钮和标签并排显示。
<div class="form-field">
<input class="radio" name="radios" type="radio" id="radioLabel" />
<label for="radioLabel">First example radio</label>
</div>
<div class="form-field">
<input class="radio" name="radios" type="radio" id="radioLabel2" />
<label for="radioLabel2">Second example radio</label>
</div>描述
在标签后添加描述或其他内容。我们建议将标签和描述包裹在 .form-field-content 中,以确保适当的间距和对齐。
Supporting description for the above label.
<div class="form-field">
<input type="radio" id="radioLabelDescription" class="radio" />
<div class="form-field-content">
<label for="radioLabelDescription" class="fw-medium">Example new radio</label>
<small class="form-text">Supporting description for the above label.</small>
</div>
</div>主题颜色
通过向 .radio 元素添加 .theme-{color} 类来修改选中单选按钮的外观。这会将选中的背景和边框颜色设置为对应的主题颜色。
<div class="form-field">
<input type="radio" id="radioprimary" class="radio theme-primary" checked />
<label for="radioprimary">Example primary radio</label>
</div>
<div class="form-field">
<input type="radio" id="radioaccent" class="radio theme-accent" checked />
<label for="radioaccent">Example accent radio</label>
</div>
<div class="form-field">
<input type="radio" id="radiosuccess" class="radio theme-success" checked />
<label for="radiosuccess">Example success radio</label>
</div>
<div class="form-field">
<input type="radio" id="radiodanger" class="radio theme-danger" checked />
<label for="radiodanger">Example danger radio</label>
</div>
<div class="form-field">
<input type="radio" id="radiowarning" class="radio theme-warning" checked />
<label for="radiowarning">Example warning radio</label>
</div>
<div class="form-field">
<input type="radio" id="radioinfo" class="radio theme-info" checked />
<label for="radioinfo">Example info radio</label>
</div>
<div class="form-field">
<input type="radio" id="radioinverse" class="radio theme-inverse" checked />
<label for="radioinverse">Example inverse radio</label>
</div>
<div class="form-field">
<input type="radio" id="radiosecondary" class="radio theme-secondary" checked />
<label for="radiosecondary">Example secondary radio</label>
</div>禁用
添加 disabled 属性,关联的 <label> 会自动应用较浅的颜色样式以匹配,帮助指示输入框的状态。
<div class="form-field">
<input type="radio" id="radioDisabled" class="radio" disabled />
<label for="radioDisabled">Example new radio</label>
</div>
<div class="form-field">
<input type="radio" id="radioCheckedDisabled" class="radio" disabled checked />
<label for="radioCheckedDisabled">Example checked disabled radio</label>
</div>尺寸
添加 .radio-sm 或 .radio-lg 使单选按钮显示得更小或更大。
<div class="form-field">
<input type="radio" id="radioSizeSm" class="radio radio-sm" checked />
<label for="radioSizeSm">Small radio</label>
</div>
<div class="form-field">
<input type="radio" id="radioSizeMd" class="radio" checked />
<label for="radioSizeMd">Default radio</label>
</div>
<div class="form-field">
<input type="radio" id="radioSizeLg" class="radio radio-lg" checked />
<label for="radioSizeLg">Large radio</label>
</div>CSS
变量
单选按钮组件的 CSS 变量基于 Sass 变量构建。
// stylelint-disable-next-line scss/dollar-variable-default
$radio-tokens: defaults(
(
--radio-size: 1.25rem,
--radio-margin-block: .125rem,
--radio-bg: var(--bg-body),
--radio-border-color: var(--border-color),
--radio-checked-bg: var(--control-checked-bg),
--radio-checked-border-color: var(--control-checked-border-color),
--radio-disabled-bg: var(--control-disabled-bg),
--radio-disabled-opacity: var(--control-disabled-opacity),
),
$radio-tokens
);