程序go 2010-04-09 19:54 采纳率: 100%
浏览 504
已采纳

使用 CSS 改变 HTML5输入的占位颜色

Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).

But the following CSS doesn't do anything to the placeholder's value:

input[placeholder], [placeholder], *[placeholder] {
    color: red !important;
}
<input type="text" placeholder="Value">

Value will still remain grey instead of red.

Is there a way to change the color of the placeholder text?

</div>

转载于:https://stackoverflow.com/questions/2610497/change-an-html5-inputs-placeholder-color-with-css

  • 写回答

22条回答 默认 最新

  • 10.24 2010-04-09 20:36
    关注

    Implementation

    There are three different implementations: pseudo-elements, pseudo-classes, and nothing.

    • WebKit, Blink (Safari, Google Chrome, Opera 15+) and Microsoft Edge are using a pseudo-element: ::-webkit-input-placeholder. [Ref]
    • Mozilla Firefox 4 to 18 is using a pseudo-class: :-moz-placeholder (one colon). [Ref]
    • Mozilla Firefox 19+ is using a pseudo-element: ::-moz-placeholder, but the old selector will still work for a while. [Ref]
    • Internet Explorer 10 and 11 are using a pseudo-class: :-ms-input-placeholder. [Ref]
    • April 2017: Most modern browsers support the simple pseudo-element ::placeholder [Ref]

    Internet Explorer 9 and lower does not support the placeholder attribute at all, while Opera 12 and lower do not support any CSS selector for placeholders.

    The discussion about the best implementation is still going on. Note the pseudo-elements act like real elements in the Shadow DOM. A padding on an input will not get the same background color as the pseudo-element.

    CSS selectors

    User agents are required to ignore a rule with an unknown selector. See Selectors Level 3:

    a group of selectors containing an invalid selector is invalid.

    So we need separate rules for each browser. Otherwise the whole group would be ignored by all browsers.

    ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
        color:    #909;
    }
    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
       color:    #909;
       opacity:  1;
    }
    ::-moz-placeholder { /* Mozilla Firefox 19+ */
       color:    #909;
       opacity:  1;
    }
    :-ms-input-placeholder { /* Internet Explorer 10-11 */
       color:    #909;
    }
    ::-ms-input-placeholder { /* Microsoft Edge */
       color:    #909;
    }
    
    ::placeholder { /* Most modern browsers support this now. */
       color:    #909;
    }
    <input placeholder="Stack Snippets are awesome!">

    Usage notes

    • Be careful to avoid bad contrasts. Firefox's placeholder appears to be defaulting with a reduced opacity, so needs to use opacity: 1 here.
    • Note that placeholder text is just cut off if it doesn’t fit – size your input elements in em and test them with big minimum font size settings. Don’t forget translations: some languages need more room for the same word.
    • Browsers with HTML support for placeholder but without CSS support for that (like Opera) should be tested too.
    • Some browsers use additional default CSS for some input types (email, search). These might affect the rendering in unexpected ways. Use the properties -webkit-appearance and -moz-appearance to change that. Example:
        [type="search"] {
            -moz-appearance:    textfield;
            -webkit-appearance: textfield;
            appearance: textfield;
        }
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败