The closest pure-CSS approach requires the browser to support the :required pseudo-class. Your input would have the required attribute like so:
<input id="test1" name="test1" type="text" required />
And your CSS would look like:
input:required {
background-color: red;
}
Here is an example jsfiddle:
http://jsfiddle.net/5L5hx4bo/
However, this would set the style for the input on page load, not when the field lost focus, but this makes more sense, from a CSS/document perspective. For a field to change style based on state, it would be one of three 'states':
invalid :
Which could be handled similar to above but using
input:invalid {
background-color: red;
}
But a blank text input would not be considered "invalid" and thus would never get styled by such a rule. However, an input with a type like "email" that had a value set to "bogus#value" would get styled, since the value is not considered valid for that type.
required:
The blank text input would be considered a match if required attribute is set, however this would be a match when the form initially loads as well.
focus (negative rule) :
You could have a CSS rule for when the input does not have the state of being in focus, but this is not the same as "on blur". Similar to required, the rule would be applied when the element loads, not exclusively when focus is lost.
Since your requirements for the style are :
- When an input's state changes from in-focus to not-in-focus
- When an input is type text and value is empty
- When an input was most recent element with state of in-focus changing to not-in-focus (so style rule is removed from an input once another input loses focus and has style rule applied)
This would not be something that would be considered a "pure CSS" type state, and thus should be handled via javascript.