Chrome支持input[type=text]元素的占位符属性(其他元素可能也支持)。

但以下CSS对占位符的值没有任何作用:

输入[占位符]、[占位符],*[占位符]{颜色:红色!重要的}<input-type=“text”placeholder=“Value”>

但Value仍将保持灰色而不是红色。

是否有方法更改占位符文本的颜色?


当前回答

最简单的方法是:

#yourInput::placeholder {
    color: red;/*As an example*/
}
/* if that would not work, you can always try styling the attribute itself: */
#myInput[placeholder] {
    color: red;
}

其他回答

添加一个非常简单的可能性:CSS过滤器!

它将设置所有内容的样式,包括占位符。

下面将在同一调色板上设置两个输入元素,使用色调过滤器进行颜色更改。它现在在浏览器中呈现得很好(除了…)

输入{滤镜:棕褐色(100%)饱和(400%)灰度(0)对比度(200%)色调旋转(68deg)反转(18%);}<input placeholder=“Hello world!”/><input-type=“date”/><br><输入类型=“范围”/><input-type=“color”/>

要允许用户使用输入类型颜色进行动态更改,或查找细微差别,请查看以下片段:

发件人:Codepen

函数stylElem(){stylo.dataset.hue=((parseInt(stylo.value.substring(1),16))/46666).toFixed(0)Array.from(document.querySelectorAll('输入,音频,视频')).forEach(函数(e){e.style.cssText+=“;滤镜:棕褐色(100%)饱和(400%)灰度(0)对比度(200%)色调旋转(”+styl.dataset.hue+“deg)反转(”+(styl.ddataset.hue/3.6)+“%)”out.innerText=e.style.css文本})()}stylElem()正文{背景:黑色;颜色:白色}选择颜色!<input type=“color”id=“stylo”oninput=“stylElem()”><br><div id=“out”></div><p><input placeholder=“Hello world!”/><input-type=“date”/><br><输入类型=“范围”/><br><音频控件src=“#”></audio><br><br><视频控件src=“#”></video>

Css过滤器文档:https://developer.mozilla.org/en-US/docs/Web/CSS/filter

这个怎么样

<input type=“text”value=“placeholder text”onfocus=“this.style.color='#000';this.value=“”;“style=”颜色:#f00;" />

没有CSS或占位符,但您可以获得相同的功能。

<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

您可以使用CSS更改HTML5输入的占位符颜色。如果碰巧,您的CSS冲突,此代码注释正常工作,您可以像下面这样使用(!important)。

::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color:#909 !important;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
   color:#909 !important;
   opacity:1 !important;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
   color:#909 !important;
   opacity:1 !important;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
   color:#909 !important;
}
::-ms-input-placeholder { /* Microsoft Edge */
   color:#909 !important;
}

<input placeholder="Stack Snippets are awesome!">

希望这会有所帮助。

最简单的方法是:

#yourInput::placeholder {
    color: red;/*As an example*/
}
/* if that would not work, you can always try styling the attribute itself: */
#myInput[placeholder] {
    color: red;
}