Chrome支持input[type=text]元素的占位符属性(其他元素可能也支持)。
但以下CSS对占位符的值没有任何作用:
输入[占位符]、[占位符],*[占位符]{颜色:红色!重要的}<input-type=“text”placeholder=“Value”>
但Value仍将保持灰色而不是红色。
是否有方法更改占位符文本的颜色?
Chrome支持input[type=text]元素的占位符属性(其他元素可能也支持)。
但以下CSS对占位符的值没有任何作用:
输入[占位符]、[占位符],*[占位符]{颜色:红色!重要的}<input-type=“text”placeholder=“Value”>
但Value仍将保持灰色而不是红色。
是否有方法更改占位符文本的颜色?
当前回答
尝试此css
input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
color: #ddd;
}
input::-moz-placeholder { /* Firefox 19+ */
color: #ddd;
}
input:-ms-input-placeholder { /* IE 10+ */
color: #ddd;
}
input:-moz-placeholder { /* Firefox 18- */
color: #ddd;
}
其他回答
<style>
::-webkit-input-placeholder {
color:red;
}
::-moz-placeholder {
color:red;
} /* firefox 22+ */
:-ms-input-placeholder {
color:red;
} /* ie10,11 */
input:-moz-placeholder {
color:red;
}
</style>
对于使用Bourbon的SASS/SCSS用户,它具有内置功能。
//main.scss
@import 'bourbon';
input {
width: 300px;
@include placeholder {
color: red;
}
}
CSS输出,您也可以抓取这部分并粘贴到代码中。
//main.css
input {
width: 300px;
}
input::-webkit-input-placeholder {
color: red;
}
input:-moz-placeholder {
color: red;
}
input::-moz-placeholder {
color: red;
}
input:-ms-input-placeholder {
color: red;
}
除了toscho的回答,我还注意到Chrome 9-10和Safari 5在支持CSS财产方面的一些webkit不一致,值得注意。
具体来说,Chrome 9和10在设置占位符样式时不支持背景色、边框、文本装饰和文本转换。
这里是完整的跨浏览器比较。
<input type="text" class="input-control" placeholder="My Input">
在头部添加以下CSS。
<style type="text/css">
.input-control::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
color: red !important;
opacity: 1; /* Firefox */
}
.input-control:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: red !important;
}
.input-control::-ms-input-placeholder { /* Microsoft Edge */
color: red !important;
}
</style>
以下是参考链接。https://www.w3schools.com/howto/howto_css_placeholder.asp
最简单的方法是:
#yourInput::placeholder {
color: red;/*As an example*/
}
/* if that would not work, you can always try styling the attribute itself: */
#myInput[placeholder] {
color: red;
}