修改uniapp中 input 的 placeholder 样式

使用placeholder-class增加类名(这个可以

html 复制代码
<input type="text" class="search-input" placeholder-class="search-input-placeholder" placeholder="输入关键词搜索" />

<style scoped>
/deep/ .search-input-placeholder {
	color: red;
}
</style>

但是有限制和注意点

  1. 只能使用font-size、font-weight、color这几个属性
  2. 如果style中有scoped时,需要在类名前加上/deep/

使用placeholder-style直接写行内样式(这个我没生效

html 复制代码
<input type="text" class="search-input" placeholder-style="color: red" placeholder="输入关键词搜索" />

使用原生css方法修改(这个我没生效

css 复制代码
input::placeholder {
  // some code
}
<!-- 其他浏览器兼容 -->
/* - Chrome ≤56,
- Safari 5-10.0
- iOS Safari 4.2-10.2
- Opera 15-43
- Opera Mobile >12
- Android Browser 2.1-4.4.4
- Samsung Internet
- UC Browser for Android
- QQ Browser */
::-webkit-input-placeholder {
  color: #ccc;
  font-weight: 400;
}

/* Firefox 4-18 */
:-moz-placeholder {
  color: #ccc;
  font-weight: 400;
}

/* Firefox 19-50 */
::-moz-placeholder {
  color: #ccc;
  font-weight: 400;
}

/* - Internet Explorer 10--11
- Internet Explorer Mobile 10-11 */
:-ms-input-placeholder {
  color: #ccc !important;
  font-weight: 400 !important;
}

/* Edge (also supports ::-webkit-input-placeholder) */
::-ms-input-placeholder {
  color: #ccc;
  font-weight: 400;
}

/* CSS Working Draft */
::placeholder {
  color: #ccc;
  font-weight: 400;
}

有些样式是生效到input元素的属性中的

比如我这边本次就是要修改padding-left,最后暂时放弃先去修改输入值的属性的时候无意中发现的

css 复制代码
.search-input {
	background-color: #fff;
	height: 70rpx;
	width: 460rpx;
	border-radius: 250rpx;
	padding-left: 64rpx;
}
相关推荐
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_9:(信件语义标记)
前端·css·笔记·ui·html
摇滚侠1 天前
外边距问题 塌陷问题 HTML CSS
css
W.A委员会1 天前
CSS中的单位
css·css3·html5
nbwenren1 天前
2026实测:Gemini 3 镜像站视觉能力实践——拍照原型图,一键生成 HTML+CSS 代码
前端·css·html
之歆1 天前
Day07_CSS盒子模型 · 样式继承 · 用户代理样式
前端·css
爱上好庆祝1 天前
学习js的第五天
前端·css·学习·html·css3·js
EnCi Zheng2 天前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python
前端老石人2 天前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
用户059540174462 天前
把多级缓存一致性验证从手工测试换成 Pytest 参数化,Bug 排查时间缩短 90%
前端·css
用户059540174462 天前
把 Redis 持久化测试从 800 行 Shell 换成 30 行 pytest,排错效率翻了 10 倍
前端·css