zoukankan      html  css  js  c++  java
  • 使用CSS修改HTML5 input placeholder颜色

    Chrome支持input=[type=text]占位文本属性,但下列CSS样式却不起作用: CSS

    input[placeholder], [placeholder], *[placeholder] {
       color:red !important;
    }
    

    HTML

    <input type="text" placeholder="Value" />
    

    运行结果值还是灰色,Color:red没有作用。有什么方法可以修改占位文本的颜色吗?我在浏览器里安装了jQuery占位文本插件,但仍然无用。

    原问题:Change an input's HTML5 placeholder color with CSS

     

    4个回答

    12

    回答:
    toscho:有三种实现方式:伪元素(pseudo-elements)、伪类( pseudo-classes)和Notihing。
    WebKit和Blink(Safari,Google Chrome, Opera15+)使用伪元素

    ::-webkit-input-placeholder
    

    Mozilla Firefox 4-18使用伪类

    :-moz-placeholder
    

    Mozilla Firefox 19+ 使用伪元素

    ::-moz-placeholder
    

    IE10使用伪类

    :-ms-input-placeholder
    

    IE9和Opera12以下版本的CSS选择器均不支持占位文本。需要注意的是伪元素在Shadow DOM里会起到元素的真实作用。
    CSS选择器
    因为每个浏览器的CSS选择器都有所差异,所以需要针对每个浏览器做单独的设定。

    ::-webkit-input-placeholder { /* WebKit browsers */
        color:    #999;
    }
    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
        color:    #999;
    }
    ::-moz-placeholder { /* Mozilla Firefox 19+ */
        color:    #999;
    }
    :-ms-input-placeholder { /* Internet Explorer 10+ */
        color:    #999;
    }
    

    Matt:textareas(文本框可拉伸)风格样式的代码,如下:

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
      color: #636363;
    }
    input:-moz-placeholder, textarea:-moz-placeholder {
      color: #636363;
    }
    

    brillout.com:input和Textarea的字体颜色均为红色。所有样式都要针对不同的选择器而定,不要打包整体处理,因为其中一个出问题,其他的都会失效。

    *::-webkit-input-placeholder {
        color: red;
    }
    
    *:-moz-placeholder {
        color: red;
    }
    
    *:-ms-input-placeholder {
        /* IE10+ */
        color: red;
    }
    

    James Donnelly:在Firefox和IE里,正常input文本颜色覆盖占位符颜色的方法:

    ::-webkit-input-placeholder { 
        color: red; text-overflow: ellipsis; 
    }
    :-moz-placeholder { 
        color: #acacac !important; text-overflow: ellipsis; 
    }
    ::-moz-placeholder { 
        color: #acacac !important; text-overflow: ellipsis; 
    } /* for the future */
    :-ms-input-placeholder { 
        color: #acacac !important; text-overflow: ellipsis; 
    }
    

    还有一种好办法:

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { 
        color:    #666;
    }
    input:-moz-placeholder, textarea:-moz-placeholder { 
        color:    #666;
    }
    input::-moz-placeholder, textarea::-moz-placeholder { 
        color:    #666;
    }
    input:-ms-input-placeholder, textarea:-ms-input-placeholder { 
        color:    #666;
    }
    

    最后一种是从网上找的:

    $('[placeholder]').focus(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function() {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();
        $('[placeholder]').parents('form').submit(function() {
            $(this).find('[placeholder]').each(function() {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });
    

    这个代码调用的规则是,先加载Javascript再用CSS修改占位符属性。

    form .placeholder {
       color: #222;
       font-size: 25px;
       /* etc */
    }
    

    user1729061:不用CSS和占位文本,同样能得到相同效果。

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

    好像只能分开来写不能像
    .样式名 .样式名::-webkit-input-placeholder,
    .样式名 .样式名::-moz-input-placeholder {}
    这种是没有效果的

    0

    CSS代码:
    input:-moz-placeholder { color: #369; }
    ::-webkit-input-placeholder { color:#369; }
    HTML代码:
    邮箱:
    <input id="email" type="email" placeholder="zhangxinxu@zhangxinxu.com" size="26" />

    0

    在原文看了看,搬了搬.
    很多人都会选择这种解决:
    user1729061:不用CSS和占位文本,同样能得到相同效果。

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

    作者的完善:

     <input type="text" value="placeholder text" onfocus="if(!this.haswriting){this.style.color='#000'; this.value='';}" onblur="if(!this.value){this.style.color='#f00'; this.value='placeholder text'; this.haswriting=false;}else{this.haswriting=true};" style="color: #f00;"/>
    

    虽然完善了部分,但用户提交时会将placeholder text跟随表单一同提交到服务器! 我选了这个:

    ::-webkit-input-placeholder { /* WebKit browsers */
        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+ */
       color:    #909;
    }
    
    <input placeholder="Stack Snippets are awesome!">
    

    opacity: 1;是为了修改FF的默认透明度导致颜色偏差;其它注意的是文本大小的设置和 input types (email, search). These might affect the rendering in unexpected ways. Use the properties -webkit-appearance and -moz-appearance to change that. 例子:

    [type="search"] {
            -moz-appearance:    textfield;
            -webkit-appearance: textfield;
            appearance: textfield;
        }
  • 相关阅读:
    Comparable内部比较器 和 Comparator外部比较器
    java——包装类数据缓存 ==号详解
    java——包装类中的equals方法
    Eclipse怎么改变@author 姓名
    非常简约学生管理系统——HashSet进行编写
    TreeMap——实现comparable接口并重写CompareTo方法
    flume收集日志直接sink到oracle数据库
    大文件多个服务器复制拷贝
    oracle创建用户表空间
    缓存策略:redis缓存之springCache
  • 原文地址:https://www.cnblogs.com/ysx215/p/6877065.html
Copyright © 2011-2022 走看看