Use CSS to modify the HTML5 input placeholder color (reproduced), html5placeholder

Source: Internet
Author: User

Use CSS to modify the HTML5 input placeholder color (reproduced), html5placeholder

Problem: Chrome supports the input = [type = text] placeholder text attribute, but the following CSS styles do not work:

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

HTML code:

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

The running result value is still gray, and Color: red does not work. Is there any way to modify the color of placeholder text? I installed the jQuery placeholder text plug-in my browser, but it is still useless.

Answer:

There are three implementation methods: pseudo-elements, pseudo-classes, and Notihing.

WebKit and Blink (Safari, Google Chrome, Opera15 +) use pseudo elements.

::-webkit-input-placeholder

Mozilla Firefox 4-18 uses pseudo classes:

:-moz-placeholder

Mozilla Firefox 19 + uses pseudo elements:

::-moz-placeholder

IE10 uses pseudo classes:

:-ms-input-placeholder

CSS Selector

Because the CSS selectors of Each browser are different, you need to set them separately for each browser.

::-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;}

The code of the textareas (text box extensible) style is as follows:

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

The font color of input and Textarea is red. All styles are determined by different selectors. Do not package them for overall processing, because one of them has a problem and the others will become invalid.

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

In Firefox and IE, the normal input text color overwrites the placeholder color:

::-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; }

There is also a good way:

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;}

The last one is from the Internet:

$('[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('');            }        })    });

The rule for calling this code is to first load Javascript and then modify the placeholder attributes with CSS.

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

In addition, CSS and placeholder text are not used to achieve the same effect.

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

Source: http://html5.9tech.cn/news/2013/1118/38691.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.