fail to change placeholder color with Bootstrap 3

Two questions:

  • I am trying to make the placeholder text white. But it doesn't work. I am using Bootstrap 3. JSFiddle demo

  • Another question is how do I change placeholder color not globally. That is, I have multiple fields, I want only one field to have white placeholder, all the others remain in default color.

  • Thanks in advance.

    html:

    <form id="search-form" class="navbar-form navbar-left" role="search">
        <div class="">
            <div class="right-inner-addon"> <i class="icon-search search-submit"></i>
                <input type="search" class="form-control" placeholder="search" />
            </div>
        </div>
    </form>
    

    css:

    .right-inner-addon {
        position: relative;
    }
    .right-inner-addon input {
        padding-right: 30px;
        background-color:#303030;
        font-size: 13px;
        color:white;
    
    }
    .right-inner-addon i {
        position: absolute;
        right: 0px;
        padding: 10px 12px;
        /*  pointer-events: none; */
        cursor: pointer;
        color:white;
    }
    
    
    /* do not group these rules*/
    ::-webkit-input-placeholder { color: white; }
    FF 4-18 
    :-moz-placeholder           { color: white; }
     FF 19+
    ::-moz-placeholder          { color: white; }
     IE 10+
    :-ms-input-placeholder      { color: white; } 
    

    Assign the placeholder to a class selector like this:

    .form-control::-webkit-input-placeholder { color: white; }  /* WebKit, Blink, Edge */
    .form-control:-moz-placeholder { color: white; }  /* Mozilla Firefox 4 to 18 */
    .form-control::-moz-placeholder { color: white; }  /* Mozilla Firefox 19+ */
    .form-control:-ms-input-placeholder { color: white; }  /* Internet Explorer 10-11 */
    .form-control::-ms-input-placeholder { color: white; }  /* Microsoft Edge */
    

    It will work then since a stronger selector was probably overriding your global. I'm on a tablet so i cant inspect and confirm which stronger selector it was :) But it does work I tried it in your fiddle.

    This also answers your second question. By assigning it to a class or id and giving an input only that class you can control what inputs to style.


    There was an issue posted here about this: https://github.com/twbs/bootstrap/issues/14107

    The issue was solved by this commit: https://github.com/twbs/bootstrap/commit/bd292ca3b89da982abf34473318c77ace3417fb5

    The solution therefore is to override it back to #999 and not white as suggested (and also overriding all bootstraps styles, not just for webkit-styles):

    .form-control::-moz-placeholder {
      color: #999;
    }
    .form-control:-ms-input-placeholder {
      color: #999;
    }
    .form-control::-webkit-input-placeholder {
      color: #999;
    }
    

    A Possible Gotcha

    Recommended Sanity Check - Make sure to add the form-control class to your inputs.

    If you have bootstrap css loaded on your page, but your inputs don't have the
    class="form-control" then placeholder CSS selector won't apply to them.

    Example markup from the docs:

    I know this didn't apply to the OP's markup but as I missed this at first and spent a little bit of effort trying to debug it, I'm posting this answer to help others.

    链接地址: http://www.djcxy.com/p/70464.html

    上一篇: 如何更改数字输入的占位符颜色? (火狐)

    下一篇: 无法使用Bootstrap 3更改占位符颜色