How do I apply a style to an empty input box? If the user types something in the input field, the style should no longer be applied. Is this possible in CSS? I tried this:
input[value=""]
This question is related to
css
input
css-selectors
In modern browsers you can use :placeholder-shown
to target the empty input (not to be confused with ::placeholder
).
input:placeholder-shown {
border: 1px solid red; /* Red border only if the input is empty */
}
More info and browser support: https://css-tricks.com/almanac/selectors/p/placeholder-shown/
It worked for me to add a class name to the input and then apply CSS rules to that:
<input type="text" name="product" class="product" />
<style>
input[value=""].product {
display: none;
}
</style>
If supporting legacy browsers is not needed, you could use a combination of required
, valid
, and invalid
.
The good thing about using this is the valid
and invalid
pseudo-elements work well with the type attributes of input fields. For example:
input:invalid, textarea:invalid { _x000D_
box-shadow: 0 0 5px #d45252;_x000D_
border-color: #b03535_x000D_
}_x000D_
_x000D_
input:valid, textarea:valid {_x000D_
box-shadow: 0 0 5px #5cd053;_x000D_
border-color: #28921f;_x000D_
}
_x000D_
<input type="email" name="email" placeholder="[email protected]" required />_x000D_
<input type="url" name="website" placeholder="http://johndoe.com"/>_x000D_
<input type="text" name="name" placeholder="John Doe" required/>
_x000D_
For reference, JSFiddle here: http://jsfiddle.net/0sf6m46j/
I'm wondered by answers we have clear attribute to get empty input boxes, take a look at this code
/*empty input*/
input:empty{
border-color: red;
}
/*input with value*/
input:not(:empty){
border-color: black;
}
UPDATE
input, select, textarea {
border-color: @green;
&:empty {
border-color: @red;
}
}
More over for having a great look in the validation
input, select, textarea {
&[aria-invalid="true"] {
border-color: amber !important;
}
&[aria-invalid="false"], &.valid {
border-color: green !important;
}
}
This worked for me:
For the HTML, add the required
attribute to the input element
<input class="my-input-element" type="text" placeholder="" required />
For the CSS, use the :invalid
selector to target the empty input
input.my-input-element:invalid {
}
Notes:
required
from w3Schools.com:
"When present, it specifies that an input field must be filled out before submitting the form."$('input#edit-keys-1').blur(function(){
tmpval = $(this).val();
if(tmpval == '') {
$(this).addClass('empty');
$(this).removeClass('not-empty');
} else {
$(this).addClass('not-empty');
$(this).removeClass('empty');
}
});
in jQuery. I added a class and styled with css.
.empty { background:none; }
Updating the value of a field does not update its value attribute in the DOM so that's why your selector is always matching a field, even when it's not actually empty.
Instead use the invalid
pseudo-class to achieve what you want, like so:
input:required {_x000D_
border: 1px solid green;_x000D_
}_x000D_
input:required:invalid {_x000D_
border: 1px solid red;_x000D_
}
_x000D_
<input required type="text" value="">_x000D_
_x000D_
<input required type="text" value="Value">
_x000D_
There is no selector in CSS which does this. Attribute selectors match attribute values, not computed values.
You would have to use JavaScript.
If you're happy not not supporting IE or pre-Chromium Edge (which might be fine if you are using this for progressive enhancement), you can use :placeholder-shown
as Berend has said. Note that for Chrome and Safari you actually need a non-empty placeholder for this to work, though a space works.
*,_x000D_
::after,_x000D_
::before {_x000D_
box-sizing: border-box;_x000D_
}_x000D_
_x000D_
label.floating-label {_x000D_
display: block;_x000D_
position: relative;_x000D_
height: 2.2em;_x000D_
margin-bottom: 1em;_x000D_
}_x000D_
_x000D_
label.floating-label input {_x000D_
font-size: 1em;_x000D_
height: 2.2em;_x000D_
padding-top: 0.7em;_x000D_
line-height: 1.5;_x000D_
color: #495057;_x000D_
background-color: #fff;_x000D_
background-clip: padding-box;_x000D_
border: 1px solid #ced4da;_x000D_
border-radius: 0.25rem;_x000D_
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;_x000D_
}_x000D_
_x000D_
label.floating-label input:focus {_x000D_
color: #495057;_x000D_
background-color: #fff;_x000D_
border-color: #80bdff;_x000D_
outline: 0;_x000D_
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);_x000D_
}_x000D_
_x000D_
label.floating-label input+span {_x000D_
position: absolute;_x000D_
top: 0em;_x000D_
left: 0;_x000D_
display: block;_x000D_
width: 100%;_x000D_
font-size: 0.66em;_x000D_
line-height: 1.5;_x000D_
color: #495057;_x000D_
border: 1px solid transparent;_x000D_
border-radius: 0.25rem;_x000D_
transition: font-size 0.1s ease-in-out, top 0.1s ease-in-out;_x000D_
}_x000D_
_x000D_
label.floating-label input:placeholder-shown {_x000D_
padding-top: 0;_x000D_
font-size: 1em;_x000D_
}_x000D_
_x000D_
label.floating-label input:placeholder-shown+span {_x000D_
top: 0.3em;_x000D_
font-size: 1em;_x000D_
}
_x000D_
<fieldset>_x000D_
<legend>_x000D_
Floating labels example (no-JS)_x000D_
</legend>_x000D_
<label class="floating-label">_x000D_
<input type="text" placeholder=" ">_x000D_
<span>Username</span>_x000D_
</label>_x000D_
<label class="floating-label">_x000D_
<input type="Password" placeholder=" ">_x000D_
<span>Password</span>_x000D_
</label>_x000D_
</fieldset>_x000D_
<p>_x000D_
Inspired by Bootstrap's <a href="https://getbootstrap.com/docs/4.0/examples/floating-labels/">floating labels</a>._x000D_
</p>
_x000D_
I realize this is a very old thread, but things have changed a bit since and it did help me find the right combination of things I needed to get my problem fixed. So I thought I'd share what I did.
The problem was I nedded to have the same css applied for an optional input if it was filled, as I had for a filled required. The css used the psuedo class :valid which applied the css on the optional input also when not filled.
This is how I fixed it;
HTML
<input type="text" required="required">
<input type="text" placeholder="">
CSS
input:required:valid {
....
}
input:optional::not(:placeholder-shown){
....
}
input[value=""], input:not([value])
works with:
<input type="text" />
<input type="text" value="" />
But the style will not change as soon as someone will start typing (you need JS for that).
This question might have been asked some time ago, but as I recently landed on this topic looking for client-side form validation, and as the :placeholder-shown
support is getting better, I thought the following might help others.
Using Berend idea of using this CSS4 pseudo-class, I was able to create a form validation only triggered after the user is finished filling it.
Here is ademo and explanation on CodePen: https://codepen.io/johanmouchet/pen/PKNxKQ
If only the field is required
you could go with input:valid
#foo-thing:valid + .msg { visibility: visible!important; }
_x000D_
<input type="text" id="foo-thing" required="required">_x000D_
<span class="msg" style="visibility: hidden;">Yay not empty</span>
_x000D_
See live on jsFiddle
OR negate using #foo-thing:invalid
(credit to @SamGoody)
Source: Stackoverflow.com