Skip to content

CSS :valid Pseudo Class

The :valid pseudo-class selects form controls (such as <input>, <select>, and <textarea>) with a value that validates according to the element's settings.

The :valid selector applies to form controls that meet their validation constraints, such as <input> elements with min and max attributes, email fields with a valid email address, number fields with a numeric value, url or date fields with valid data, and required fields with a non-empty value.

Version

HTML Living Standard

HTML5

Selectors Level 4

Syntax

CSS :valid syntax

css
:valid {
  css declarations;
}

Example of the :valid selector:

CSS :valid code example

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      input {
        border: 1px solid #cccccc;
        padding: 5px 10px;
      }
      input:valid {
        background-color: #eeeeee;
      }
    </style>
  </head>
  <body>
    <h2>:valid selector example</h2>
    <form>
      <input type="email" value="[email protected]" />
    </form>
  </body>
</html>

In the given example, the styling disappears when an invalid email address is entered.

Practice

What is the usage and function of the 'valid' CSS pseudo-class?

Dual-run preview — compare with live Symfony routes.