
CSS appearance 属性会移除表单控件的原生视觉提示
来源标题:What CSS Appearance Does
CSS 的 appearance: none 会移除 select 的下拉箭头、checkbox 的原生外观等视觉提示,但元素仍保留 HTML 功能。这些控件仍可正常点击和键盘操作,只是失去了原有的视觉线索,因此应把 appearance 当作一项有意的样式选择。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
appearance: none 可以把 select 上那个熟悉的箭头去掉,也可以去掉 checkbox 的原生外观。元素仍然保留 HTML 功能,但它原本依赖的视觉提示可能就没有了。
CSS appearance 值得当作一项有意的样式选择来对待。一次抹掉控件身份的 reset,会带来额外的工作量,而调整边框或颜色本来可以避免这些工作。
复制 对比原生、裸样式和重新设计样式的 select原生外观LatviaEstoniaLithuania appearance: noneLatviaEstoniaLithuania 用箭头重新设计LatviaEstoniaLithuania 这三个都是真正的 select。试着打开每一个并用键盘操作;变化的只是它们的外观。 <label for="native">Native presentation</label><select id="native"><option>Latvia</option><option>Estonia</option><option>Lithuania</option></select> <label for="bare">appearance: none</label><select id="bare" class="bare"><option>Latvia</option><option>Estonia</option><option>Lithuania</option></select> <label for="styled">Restyled with an arrow</label><span class="select-wrap"><select id="styled"><option>Latvia</option><option>Estonia</option><option>Lithuania</option></select></span>
<p class="note">All three are real selects. Try opening each one and using the keyboard; only their presentation changes.</p> CSS button, select { padding: .35rem .6rem; border: 1px solid var(--clg); background: var(--cw); color: var(--cb); } input[type="range"] { width: min(100%, 18rem); vertical-align: middle; } h2, h3 { font-size: 1rem; } .note { color: var(--clg); } label { display: block; margin-block: .75rem .3rem; } select { display: block; width: min(100%, 18rem); padding: .5rem; } .bare, .select-wrap select { appearance: none; } .select-wrap { position: relative; display: block; width: min(100%, 18rem); } .select-wrap select { width: 100%; padding-inline-end: 2.5rem; } .select-wrap::after { content: ''; position: absolute; inset-inline-end: 1rem; top: 50%; width: .4rem; height: .4rem; border-inline-end: 2px solid currentColor; border-block-end: 2px solid currentColor; transform: translateY(-70%) rotate(45deg); pointer-events: none; } 看看 appearance 会从 select 上移除什么 先从一个带标签的原生 select 开始: <label for="country">Country</label> <select id="country" name="country"> <option>Latvia</option> <option>Estonia</option> <option>Lithuania</option> </select> select { appearance: none; appearance 的文档说明,none 会抑制原生外观,同时保留功能。去掉 select 的常规箭头,可能让它看起来像文本输入框,尽管它仍然会打开选项列表。这一样式改动并没有用自定义组件替换 select。它的原生行为仍然保留,这与用通用元素重建交互是两回事。
可以用一个 wrapper 提供装饰性箭头,同时让 select 继续作为交互元素: <label for="region">Region</label> <span class="select-wrap"> <select id="region" name="region">
<option>Europe</option> <option>Asia</option> </select> </span> .select-wrap { position: relative; display: inline-block; .select-wrap select { appearance: none; font: inherit; color: inherit; background: transparent; border: 1px solid currentColor; border-radius: 0.5rem; padding: 0.75rem 2.5rem 0.75rem 1rem; .select-wrap::after { content: ""; position: absolute; inset-inline-end: 1rem; top: 50%; width: 0.4rem; height: 0.4rem; border-inline-end: 2px solid currentColor; border-block-end: 2px solid currentColor; transform: translateY(-70%) rotate(45deg); pointer-events: none; 额外的 inline-end padding 为箭头留出了空间。wrapper 的伪元素只是装饰,指针事件仍然会到达它下方的 select。这是一个针对普通水平文本的小样式示例,并不是适用于所有书写模式的完整 select 设计。在复用这个 wrapper 之前,请用更长的选项标签检查箭头和 padding。
有样式的 select 仍然需要 label、用于提交的有意义 name,以及合理的选项。select 元素参考文档描述了这些 HTML 职责。
移除 appearance 并不会移除这份约定。它也不能保证剩下的外观是可理解的:一个可以用键盘操作的控件仍然可能难以辨认。
这一区别对 checkbox 和 radio button 尤其重要。去掉原生外观后,如果没有替代样式提供选中指示,选中状态就可能消失。一条通用的 input { appearance: none; } reset 影响的范围远不止一个 select 箭头。
当需要的改动只是强调色时,原生 checkbox 可以保留它的外观: input[type="checkbox"], input[type="radio"] { accent-color: #4d5cff; accent-color 提供了这种更窄的控制。它不需要从头重建选中和未选中的视觉效果。
自定义形状或许能成为接管更多样式的理由。仅仅为了一个紫色对勾,并不足以成为丢弃原生对勾的充分理由。
button 通常可以直接接受普通的字体、边框和 padding 样式,而不需要 appearance reset: button { font: inherit; border: 1px solid currentColor; border-radius: 0.5rem; padding: 0.75rem 1rem; 搜索输入框可能带有浏览器特有的装饰,比如清除控件。请检查搜索输入框的原生外观,而不是假定所有 input 都有相同的细节。
针对外观需要改变的那个特定控件。一条本意用于搜索框的规则,不应该悄悄变成整个表单的样式策略。
原生外观因浏览器和操作系统而异。移除它会暴露出不同的剩余默认样式,因此在一个平台上的结果并不能证明其他地方也一致。
保持键盘焦点可见,并用真实标签检查禁用和选中状态。当控件依赖装饰性边框或箭头时,高对比度设置也应纳入检查范围。
(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。