Is there a way to detect whether or not an input has text in it via CSS? I've tried using the :empty
pseudo-class, and I've tried using [value=""]
, neither of which worked. I can't seem to find a single solution to this.
I imagine this must be possible, considering we have pseudo-classes for :checked
, and :indeterminate
, both of which are kind of similar thing.
Please note: I'm doing this for a "Stylish" style, which can't utilize JavaScript.
Also note, that Stylish is used, client-side, on pages that the user does not control.
The valid selector will do the trick.
It is possible, with the usual CSS caveats and if the HTML code can be modified. If you add the
required
attribute to the element, then the element will match:invalid
or:valid
according to whether the value of the control is empty or not. If the element has novalue
attribute (or it hasvalue=""
), the value of the control is initially empty and becomes nonempty when any character (even a space) is entered.Example:
The pseudo-classed
:valid
and:invalid
are defined in Working Draft level CSS documents only, but support is rather widespread in browsers, except that in IE, it came with IE 10.If you would like to make “empty” include values that consist of spaces only, you can add the attribute
pattern=.*\S.*
.There is (currently) no CSS selector for detecting directly whether an input control has a nonempty value, so we need to do it indirectly, as described above.
Generally, CSS selectors refer to markup or, in some cases, to element properties as set with scripting (client-side JavaScript), rather than user actions. For example,
:empty
matches element with empty content in markup; allinput
elements are unavoidably empty in this sense. The selector[value=""]
tests whether the element has thevalue
attribute in markup and has the empty string as its value. And:checked
and:indeterminate
are similar things. They are not affected by actual user input.You can style
input[type=text]
differently depending on whether or not the input has text by styling the placeholder. This is not an official standard at this point but has wide browser support, though with different prefixes:Example: http://fiddlesalad.com/scss/input-placeholder-css
You can use the
placeholder
trick as written above w/orequired
field.The problem with
required
is that when you wrote something, then deleted it - the input will now always be red as part of the HTML5 spec - then you'll need a CSS as written above to fix/override it.You can simple do thing w/o
required
CSS
Code pen:https://codepen.io/arlevi/pen/LBgXjZ
Basically what everybody is looking for is:
LESS:
Pure CSS:
Stylish cannot do this because CSS cannot do this. CSS has no (pseudo) selectors for
<input>
value(s). See:The
:empty
selector refers only to child nodes, not input values.[value=""]
does work; but only for the initial state. This is because a node'svalue
attribute (that CSS sees), is not the same as the node'svalue
property (Changed by the user or DOM javascript, and submitted as form data).Unless you care only about the initial state, you must use a userscript or Greasemonkey script. Fortunately this is not hard. The following script will work in Chrome, or Firefox with Greasemonkey or Scriptish installed, or in any browser that supports userscripts (i.e. most browsers, except IE).
See a demo of the limits of CSS plus the javascript solution at this jsBin page.