You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在input输入框的边框上添加自定义文字?

input输入框边框嵌入*Email文字实现方案

直接通过父容器嵌套+标签定位即可实现,无需JS,兼容性好,以下是可直接运行的代码:

HTML结构

<div class="input-group">
  <span class="input-tag">*Email</span>
  <input type="email" name="email" />
</div>

CSS样式

/* 外层容器模拟输入框边框 */
.input-group {
  position: relative;
  margin: 16px 0;
  padding: 0 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
/* 文字标签定位到边框位置 */
.input-tag {
  position: absolute;
  top: -7px;
  left: 12px;
  padding: 0 4px;
  background-color: #ffffff;
  font-size: 12px;
  color: #f56c6c;
}
/* 清除输入框默认边框 */
.input-group input {
  width: 100%;
  height: 38px;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  box-sizing: border-box;
}
/* 可选:聚焦状态样式 */
.input-group:focus-within {
  border-color: #409eff;
}
.input-group:focus-within .input-tag {
  color: #409eff;
}

注意事项

  • 标签的背景色需要和输入框所在的页面背景色保持一致,才能完全遮盖住下方的容器边框
  • 可根据设计需求自行调整标签的颜色、字体大小、左右偏移量
  • 不需要额外引入第三方依赖,所有主流浏览器均兼容该写法

内容的提问来源于stack exchange,提问作者xxanit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 17:33:00