CSS修改placeholder样式的方法介绍代码示例

首先来看一下chrome默认的input样式 (placeholder)(input style)可以发现,placeholder和input的默认颜色是有点区别的。现在我们来修改input 的颜色 (placeholder)(input)不难发现color属性只能改变输入值的颜色,placeholder的颜色没人任何变化。so,如何来改变placeholder的颜色。要改变placeholder的颜色就要使...
CSS修改placeholder样式的方法介绍代码示例
本篇文章给大家带来的内容是CSS修改placeholder样式的方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
项目用经常遇到修改input的placeholder的颜色的需求,这里来看一下placeholder如何用css设置:
首先来看一下chrome默认的input样式
<input type="text" placeholder="hello world">
(placeholder)
(input style)
可以发现,placeholder和input的默认颜色是有点区别的。现在我们来修改input 的颜色
<input type="text" placeholder="hello world" style="color: red">
(placeholder)
(input)
不难发现color属性只能改变输入值的颜色,placeholder的颜色没人任何变化。so,如何来改变placeholder的颜色。
要改变placeholder的颜色就要使用到伪类:placeholder
<style> input:placeholder { color: green; }</style><input type="text" placeholder="hello world" style="color: red;">
(placeholder)
(input)
需要注意的是:palceholder伪类的兼容性,以上是在chrome浏览器的运行结果,同样的代码在IE11中就成了这样
(placeholder - ie11)
(input - ie11)
IE解决方案:
首先IE9及以下不支持placeholder。IE10需要用:-ms-input-placeholder,并且属性需要加上!important提高优先级。
<style> input:-ms-input-placeholder { color: green !important; }</style><input type="text" placeholder="hello world" style="color: red;">
(placeholder ie11)
(input ie11)
之后给出其他浏览器的适配方案/* - 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;}2024-07-14
mengvlog 阅读 10 次 更新于 2025-07-20 21:48:45 我来答关注问题0
  • 首先IE9及以下不支持placeholder。IE10需要用:-ms-input-placeholder,并且属性需要加上!important提高优先级。 input:-ms-input-placeholder { color: green !important; } (placeholder ie11)(input ie11)之后给出其他浏览器的适配方案/* - Chrome ≤56, - Safari 5-10.0 - iOS Safari 4.2-10...

  •  文暄生活科普 图文详解placeholder属性的使用方法以及如何修改placeholder的默认样式

    举例:修改placeholder样式,将input提示框中文字的颜色设为红色,字体设为20px,让文字在输入框中水平居中显示。代码如下 HTML部分: CSS部分:input{width: 200px;height: 40px;}#input:-webkit-input-placeholder { color: red; font-size: 20px; text-align: center;}#input:-moz-placeholder {...

  • css3设置placeholder的样式代码示例:input::-webkit-input-placeholder { color: palevioletred;

  •  书香学编程 怎么修改placeholder字体的css样式

    ::-moz-placeholder { /* Mozilla Firefox 19+ */ color:#999;} :-ms-input-placeholder { /* Internet Explorer 10+ */ color:#999;} 如何设置css3中placeholder的字体颜色 还可以写成下面这样:input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #666;} input...

  •  翡希信息咨询 怎么设置input中placeholder的大小

    使用::placeholder伪元素选择器:在CSS中,可以使用::placeholder伪元素选择器来选中元素中的placeholder文本,并为其设置样式。例如,要设置placeholder文本的字体大小为14px,可以这样写:cssinput::webkitinputplaceholder {fontsize: 14px;}input::mozplaceholder {fontsize: 14px;}input:msinputplaceholder...

檬味博客在线解答立即免费咨询

CSS相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部