CSS Input field text color of inputted text
Asked Answered
P

5

91

I have an input field, and the color of the text in it is black. (I'm using jquery.placeholder) Let's say the text in there is "E-Mail"

When you click on this field, the black placeholding text dissapears (thanks to jquery.placeholder).

Any text inputted into this field, I want it to turn red, and when you deselect this field, I want it to stay red.

At the moment, the text saying "E-Mail" is black, and anything I type into is red, which is great, but as soon as i deselect, it goes back to black. Can anyone help me? I want the text to stay red, even after it's deselected. Here is my code

textarea:focus, input:focus {
    color: #ff0000;
}

input, select, textarea{
    color: #000;
}
Palmation answered 12/7, 2011 at 19:22 Comment(0)
W
108

Change your second style to this:

input, select, textarea{
    color: #ff0000;
}

At the moment, you are telling the form to change the text to black once the focus is off. The above remedies that.

Also, it is a good idea to place the normal state styles ahead of the :focus and :hover styles in your stylesheet. That helps prevent this problem. So

input, select, textarea{
    color: #ff0000;
}

textarea:focus, input:focus {
    color: #ff0000;
}
Waylen answered 12/7, 2011 at 19:29 Comment(3)
Ok I Understand, But if I change my second style to red, then the original placeholding text is also changed to red (rather than black). Does that make sense?Palmation
Yes. If you need the text to be black first. Set it in placeholder plugin. Or you can use some additional scripting to accomplish this.Waylen
Thanks mate, I was totally missing declaring the placeholders style, which was seperate to the input boxes style!Palmation
S
34

If you want the placeholder text to be red you need to target it specifically in CSS.

Write:

input::placeholder{
  color: #f00;
}
Streamlined answered 2/11, 2018 at 17:57 Comment(0)
S
6

I always do input prompts, like this:

    <input style="color: #C0C0C0;" value="[email protected]" 
    onfocus="this.value=''; this.style.color='#000000'">

Of course, if your user fills in the field, changes focus and comes back to the field, the field will once again be cleared. If you do it like that, be sure that's what you want. You can make it a one time thing by setting a semaphore, like this:

    <script language = "text/Javascript"> 
    cleared[0] = cleared[1] = cleared[2] = 0; //set a cleared flag for each field
    function clearField(t){                   //declaring the array outside of the
    if(! cleared[t.id]){                      // function makes it static and global
        cleared[t.id] = 1;  // you could use true and false, but that's more typing
        t.value='';         // with more chance of typos
        t.style.color='#000000';
        }
    }
    </script>

Your <input> field then looks like this:

    <input id = 0; style="color: #C0C0C0;" value="[email protected]" 
    onfocus=clearField(this)>
Superhighway answered 22/6, 2014 at 19:17 Comment(0)
E
3

To add color to an input, Use the following css code:

input{
     color: black;
}
Ermaermanno answered 29/3, 2018 at 8:37 Comment(0)
B
2

replace:

input, select, textarea{
    color: #000;
}

with:

input, select, textarea{
    color: #f00;
}

or color: #ff0000;

Bischoff answered 12/7, 2011 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.