Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make input value uppercase in CSS without affecting the placeholder

Tags:

css

uppercase

Is there a way I can make an input value uppercase without making the placeholder uppercase?

Seems like I get one or the other. I'd prefer to do this cleanly just using CSS (JS last resort).

enter image description here

like image 798
kaleazy Avatar asked Aug 07 '14 10:08

kaleazy


People also ask

How do I style uppercase in CSS?

To make a block of text have all capital letters, use text-transform: uppercase in your CSS selector: text-transform: uppercase; The text-transform property accepts three possible values: uppercase: Sets each word to uppercase in a text element.

Can you change placeholder with CSS?

<input type="text" placeholder="A red placeholder text..">

How do you automatically set text box to uppercase?

Use keyup() method to trigger the keyup event when user releases the key from keyboard. Use toLocaleUpperCase() method to transform the input to uppercase and again set it to the input element.


2 Answers

Each browser engine has a different implementation to control placeholder styling. Use the following:

jsBin example.

input { 
    text-transform: uppercase;
}
::-webkit-input-placeholder { /* WebKit browsers */
    text-transform: none;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
    text-transform: none;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
    text-transform: none;
}
:-ms-input-placeholder { /* Internet Explorer 10+ */
    text-transform: none;
}
::placeholder { /* Recent browsers */
    text-transform: none;
}

Needless to say, this only works in browsers that can handle placeholders. (IE9/10+)

like image 133
Sander Koedood Avatar answered Oct 16 '22 13:10

Sander Koedood


I know you said using JS was a last resort however in this case it is probaly more effective and simpler (also if you dont use JS you the value posted on frorm submit will not be uppercase) so this might be better no aditional CSS needed:

<input oninput="this.value = this.value.toUpperCase()" placeholder="Enter Drivers License #" />

EDIT

Some people have been complaining that the cursor jumps to the end when editing the value, so this slightly expanded version should resolve that

<input oninput="let p = this.selectionStart; this.value = this.value.toUpperCase();this.setSelectionRange(p, p);" />
like image 8
Yehuda Schwartz Avatar answered Oct 16 '22 12:10

Yehuda Schwartz