YL

FORM & INPUT

浮动标签输入Floating Label Input

标签一开始待在占位符的位置,聚焦或输入后缩着升到边框上,一个元素干两个活。

中英对照

中文
浮动标签输入
英文
Floating Label Input
别名
浮动占位符、Material 浮动标签、升起的 label
一句话
标签一开始待在占位符的位置,聚焦或输入后缩着升到边框上,一个元素干两个活。

效果预览

适合用在哪

登录注册、设置表单、需要紧凑排版的任何输入场景。

不适合:标签文案很长、或需要额外帮助文案常驻可见时:浮动后空间不够;无障碍要求标签始终完整可见的场景,优先用外部静态 label。

和相近模式的区别

图标前后缀输入Icon Affix Input

前后缀输入解决图标/单位/域名;浮动标签解决「占位与标签合一」。两者可叠用,但不要让浮动标签和前置图标抢同一左上角。

内联校验状态Input Validation States

校验管边框色与错误文案;浮动标签只管标签位移。报错时标签仍应可读,不要只靠变红边框。

容易踩的坑

  • 浏览器自动填充时 :placeholder-shown 不触发,标签与已填内容重叠。
  • 缩小后的标签只有 10px 且为浅灰,对比度不足 4.5:1,难以辨认字段含义。
  • 用 font-size 而非 transform 做缩放动画,每帧触发重排导致文字闪烁。

设计提示词 · Prompt

浮动标签输入(Floating Label Input):input 的 placeholder 设为空格(用于 :placeholder-shown 判断),label 绝对定位在输入框内部(16px、灰色)。聚焦或有值时(input:focus + label 或 input:not(:placeholder-shown) + label)label 缩小到 10-11px、上移到边框线上(top:负值或 4px)、变品牌色,同时 input 的 padding-top 预留标签空间。缩放动画用 transform + 180ms cubic-bezier(.32,.72,0,1),不要用 font-size 直接跳变(会闪)。纯 CSS 可实现,无需 JS。
Floating Label Input: set the input's placeholder to a single space (used for :placeholder-shown detection); the label is absolutely positioned inside the input (16px, gray). When focused or filled (input:focus + label or input:not(:placeholder-shown) + label), the label shrinks to 10-11px, moves up onto the border line (top: negative value or 4px) and turns brand color, while the input's padding-top reserves space for the label. Animate the scale with transform + 180ms cubic-bezier(.32,.72,0,1); don't jump font-size directly (it flickers). Achievable in pure CSS, no JS required.

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* placeholder 空格 + :placeholder-shown 驱动上浮 */
.float-field {
  position: relative;
  width: min(360px, 100%);
}
.float-field input {
  width: 100%;
  height: 52px;
  padding: 22px 14px 8px;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  background: #fff;
  font: 15px/1.2 system-ui, sans-serif;
  outline: 0;
}
.float-field label {
  position: absolute;
  left: 14px;
  top: 50%;
  color: #6b7280;
  font: 16px/1 system-ui, sans-serif;
  pointer-events: none;
  transform: translateY(-50%);
  transform-origin: left center;
  transition: transform .18s cubic-bezier(.32, .72, 0, 1), color .18s ease;
}
.float-field input:focus {
  border-color: #4f46e5;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .12);
}
.float-field input:focus + label,
.float-field input:not(:placeholder-shown) + label {
  top: 4px;
  color: #4f46e5;
  transform: translateY(0) scale(.7);
}
@media (prefers-reduced-motion: reduce) {
  .float-field label { transition: none; }
}

演示 HTML 结构

<div class="s d-fl">
  <input placeholder=" " value="hello@nova.dev">
  <label>邮箱地址</label>
</div>

常见问题

浮动标签输入的英文是什么?

浮动标签输入的英文是 Floating Label Input。

Floating Label Input 的中文是什么?

Floating Label Input 的中文是「浮动标签输入」。常见中文别名还包括 浮动占位符、Material 浮动标签、升起的 label。标签一开始待在占位符的位置,聚焦或输入后缩着升到边框上,一个元素干两个活。

为什么用 transform 而不是改 font-size?

直接改字号会触发布局重排并闪一下;scale + translate 在 180ms cubic-bezier(.32,.72,0,1) 下更稳,也是 Material 浮动标签的常见做法。

placeholder 为什么是空格?

用来配合 :placeholder-shown:有值或聚焦时判定「非空占位」,纯 CSS 就能区分空态与填态,不必靠 JS 加 class。

常见搭配

同一页面里经常和「浮动标签输入」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

所属设计风格

「浮动标签输入」是这些整体设计风格的常见组成部分,在设计风格词典里可以看到整页效果。

标签