input不支持::before伪元素,因其属替换元素,CSS规范明确禁止;必须用容器包裹并设position: relative,再对容器应用::before,同时input需预留padding-left。
直接用 ::before 给 <input> 加图标行不通——它不支持伪元素,必须套一层容器。
浏览器规范明确禁止对替换元素(如 input、textarea、img)使用 ::before 或 ::after。你写上去不会报错,但内容完全不渲染,调试器里也看不到节点。
input::before { content: "?"; } 什么也不显示<div class="input-wrapper"><input></div> 包裹,再对 wrapper 应用 ::before
核心三步:父容器设 position: relative,伪元素绝对定位,input 补 padding-left 避免文字遮挡。
<div class="input-with-icon"><input type="text" placeholder="搜索..."></div>
.input-with-icon {position: relative;}.input-with-icon input {padding-left: 36px; /* 至少大于图标宽度 */width: 100%;}.input-with-icon::before {content: "1F50D"; /* ? Unicode */position: absolute;left: 12px;top: 50%;transform: translateY(-50%);font-size: 16px;color: #999;}
font-family 和 font-weight 缺一不可,否则显示成方块当图标需要缩放、着色灵活或 SVG 矢量保真时,background-image 比 content 更可控,尤其适合带圆角/描边的输入框。
.input-with-icon::before {content: "";position: absolute;left: 12px;top: 50%;transform: translateY(-50%);width: 16px;height: 16px;background: url("search.svg") no-repeat center;background-size: contain;}
background 方式无法用 color 改颜色,要换色得改图或切多份 SVG真正容易被忽略的是可访问性——::before 插入的图标对屏幕阅读器不可见,如果它是功能标识(比如“密码可见”开关),必须额外加 aria-label 到 input 上,不能只靠视觉提示。
tlwdr7650路由器没有wps按钮(tlwdr7650路由器没有wps按钮怎么办)
tlwdr7632扩展器电脑怎么设置(tlwdr7632扩展器电脑设置方法)
tlwda6332re安装教程(tlwda6332re如何安装)
tlwdr7632扩展器手机怎么设置(tlwdr7632扩展器手机设置方法)
tlxdr3010怎么设置网速快(tlxdr3010网速快设置方法)
tlwdr5620易展版怎么克隆(tlwdr5620易展版克隆方法)