I\'m having a heck of a time with this particular CSS selector which does not want to work when I add :not(:empty)
to it. It seems to work fine with any combina
Since placeholder disappear on input, you can use:
input:placeholder-shown{
//rules for not empty input
}
Another pure CSS solution
.form{
position:relative;
display:inline-block;
}
.form input{
margin-top:10px;
}
.form label{
position:absolute;
left:0;
top:0;
opacity:0;
transition:all 1s ease;
}
input:not(:placeholder-shown) + label{
top:-10px;
opacity:1;
}
<div class="form">
<input type="text" id="inputFName" placeholder="Firstname">
<label class="label" for="inputFName">Firstname</label>
</div>
<div class="form">
<input type="text" id="inputLName" placeholder="Lastname">
<label class="label" for="inputLName">Lastname</label>
</div>
It is possible with inline javascript onkeyup="this.setAttribute('value', this.value);"
& input:not([value=""]):not(:focus):invalid
Demo: http://jsfiddle.net/mhsyfvv9/
input:not([value=""]):not(:focus):invalid{
background-color: tomato;
}
<input
type="email"
value=""
placeholder="valid mail"
onchange="this.setAttribute('value', this.value);" />
This should work in modern browsers:
input[value]:not([value=""])
It selects all inputs with value attribute and then select inputs with non empty value among them.
pure css solution
input::-webkit-input-placeholder {
opacity: 1;
-webkit-transition: opacity 0s;
transition: opacity 0s;
text-align: right;
}
/* Chrome <=56, Safari < 10 */
input:-moz-placeholder {
opacity: 1;
-moz-transition: opacity 0s;
transition: opacity 0s;
text-align: right;
}
/* FF 4-18 */
input::-moz-placeholder {
opacity: 1;
-moz-transition: opacity 0s;
transition: opacity 0s;
text-align: right;
}
/* FF 19-51 */
input:-ms-input-placeholder {
opacity: 1;
-ms-transition: opacity 0s;
transition: opacity 0s;
text-align: right;
}
/* IE 10+ */
input::placeholder {
opacity: 1;
transition: opacity 0s;
text-align: right;
}
/* Modern Browsers */
*:focus::-webkit-input-placeholder {
opacity: 0;
text-align: left;
}
/* Chrome <=56, Safari < 10 */
*:focus:-moz-placeholder {
opacity: 0;
text-align: left;
}
/* FF 4-18 */
*:focus::-moz-placeholder {
opacity: 0;
text-align: left;
}
/* FF 19-50 */
*:focus:-ms-input-placeholder {
opacity: 0;
text-align: left;
}
/* IE 10+ */
*:focus::placeholder {
opacity: 0;
text-align: left;
}
/* Modern Browsers */
input:focus {
text-align: left;
}