:valid 6q4815
:valid is a CSS pseudo-class used to select and style form <input> elements whose values validate according to their type specified in the type attribute. 3g236d
For example, email inputs (<input type="email">
) whose values match a valid email address pattern.
When a number input type (<input type="number">
) is in the range of permitted values specified using the min
and max
attributes, then the :valid
pseudo-class matches, and also the :in-range
styles come after the :valid
styles in the style sheet.
Just like other pseudo-class selectors, the :valid
selector can be chained with other selectors such as :focus
, among others. For example, the following rule will provide focus styles for the element that is in an valid state, adding a soft green glow around it:
input:valid:focus { outline: 0; border: none; box-shadow: 0 0 3px 6px rgba(0, 200, 0, 0.3); }
You can see a live demo of this in the live demo section below.
Examples 3e5p5z
input[type="number"]:valid { background-color: lightgreen; } input:valid { box-shadow: 0 0 3px 5px rgba(0, 200, 0, .2); }
Live Demo 2x4j2k
The following demo shows different input types that will get a light green background color as long as the value entered in them is valid. The inputs will also get a subtle green box shadow when they are focused.
The demo also uses the :invalid
pseudo-class selector to style the inputs when the value inside them does not validate.
Browser k5t66
The :valid
pseudo-class is ed in Chrome 10+, Firefox 4+, Safari 5+, Opera 10+, Internet Explorer 10+ and on iOS.