DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
:valid
is a pseudo-class selector that enables styling on valid form elements. :valid
can be added to any form element, such as <form>
, <fieldset>
, or <input>
, to enhance styling based on the validity of an element, or a contained child element.
form:valid {
outline: 3px dashed green;
}
Syntax
:valid {
/* ... */
}
Though syntactically similar for all form elements, the way validation differs between <form>
, <fieldset>
, and <input>
influences how it works. Let’s cover those individually to see how.
Forms
The :valid
pseudo-class enables condition styling on <form>
elements based on the validity of child form elements, such as <fieldset>
or <input>
. If all child form elements are valid, then the <form>
will also be considered valid, and :valid
conditional styling is applied.
form:valid {
outline: 3px dashed green;
}
Fieldsets
Similar to <form>
, :valid
applies styling to <fieldset>
elements conditionally based on the validity of child <input>
elements, but only direct children, like radio buttons or checkboxes.
fiedset:valid {
outline: 3px dashed green;
}
Inputs
:valid
applies conditional styling to <input>
elements by validating the value
of the <input>
.
Specifically, validation checks if the input value is required with the required
attribute, or if the input must match a pattern described by the pattern
attribute. Adding :valid
to <input>
will apply the same styles to all valid input types. Pairing the input with the [type]
attribute selector enables styling specific input types.
/* All input types */
input:valid {
outline: 3px dashed green;
}
/* Specfic input types */
input[type="text"]:valid {
outline: 3px dashed green;
}
Accessibility considerations
In case you are considering using :valid
to enhance styling for form elements, this may not be enough to successfully translate the validity of inputs in an accessible way. For example, please consider including text describing the successful validation for assistive devices, like screen readers.
Testing for validity
To enable validation testing on an input, the input must be set as a required field by applying the required
attribute to the field.
<input type="text" id="name" name="name" required />;
Inputs with the required
attribute will match the :valid
pseudo-class selector if input has been provided a value. Browsers will provide additional validation testing for some input types. For example, the <input type="email">
input will generally require an @
to be included in the inputs value to be considered valid.
Validity testing may be enhanced by providing an input with a pattern that the input value should match to be considered valid. This can be accomplished by including the pattern
attribute on an input. The pattern
attribute must be supplied a pattern, typically in a regular expression format, to test the provided value.
<input type="text" id="zipcode" name="zipcode" pattern="[0-9]{5}" inputmode="numeric" required />
Chaining pseudo-classes
The :valid
pseudo-class may be chained to other pseudo-class selectors, such as :focus
or :hover
, to adapt styling further.
.input:focus:valid {
outline: 3px dashed green;
}
:has()
Using with Since the functional pseudo-class selector :has()
is widely available in browsers, it can be used in conjunction with validity pseudo-class selectors to enable other elements to benefit from conditional styling form elements.
For example, we can style a parent element that contains an :valid
input
div:has(input:valid) {
outline: 3px solid green;
}
We could even scope this to the <form>
element itself so that styling is applied to it if it contains a :valid
element anywhere:
form:has(:valid) {
/* If :valid is present anywhere in the form. */
}
Specification
:valid
is defined in the CSS Selectors Level 4 specification as a “validity pseudo-class”, meaning it is used to style interactive elements based on an evaluation of user input.
Browser support
Related articles
Form Validation Part 1: Constraint Validation in HTML
Form Validation UX in HTML and CSS
Happier HTML5 Form Validation
Form Validation Styling on Input Focus
Additional resources
- Mozilla Developer Network – :valid
- W3C – Selectors Level 4
- HTML Specification – :valid
- W3C Use of Color, Understanding Success Criterion 1.4.1
My attempt to explain the :valid selector: The :valid selector is basically a pseudo-class which can work together with the input element. Let’s say you use the input element to check if a user types something correct or wrong. Now with the :valid selector you can edit let’s say the color of an input field so the user knows if it is correct or wrong. If a word is typed correctly, the input box becomes green:
Browser compatibility: Chrome 10, Firefox 4, Internet explorer 10, Opera 10, Safari 5. Firefox mobile 4, Opera mobile 10, Safari 5. Here’s a very nice demo from the mozilla website: https://developer.mozilla.org/samples/cssref/input-validation.html
hello
The :valid and :invalid selector respectively aren’t actually parts of the Selectors Level 3 spec, but of Level 4.
After much research and experimenting I found out that a selector with input[value=””] will match when using the “:valid” pseudo class.
http://codepen.io/jnkrois/pen/rVvqjB
which is the valid css selectors?
div
_div
.element
#element
So how about this:
input[type=”text”][value]:not([value=””])
By testing for ‘value’ (but not ’empty’) you won’t have to test for ‘valid’ or combine ‘required’.
Jip, It doesn’t work – as attribute selector refers to xml markup and not the actual — in memory – value of the input that the user has changed