Form state pseudo-classes
Definition and purpose
Form state selectors express conditions that HTML already tracks. :invalid matches a control failing constraints, even before editing; :user-invalid depends on user interaction as defined by the browser. CSS can show feedback but cannot validate server data.
Empty required input
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a:invalid { border: 3px solid red; } #a:valid { border: 3px solid green; } </style> </head> <body> <label for="a">Email (required)</label> <input id="a" type="email" required> <button id="b" disabled>Unavailable</button> </body></html>The border changes after entering a@example.com.
Syntax and mental model
Use :valid, :invalid, :disabled, :checked and :focus-visible for separate conditions. Required email input is invalid when empty and becomes valid with a syntactically valid address. Keep a text description of the requirement and provide meaningful errors rather than relying only on red.
Unavailable action
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b:disabled { opacity: 0.6; cursor: not-allowed; } </style> </head> <body> <label for="a">Email (required)</label> <input id="a" type="email" required> <button id="b" disabled>Unavailable</button> </body></html>The disabled attribute controls activation; opacity only changes appearance.
Common mistakes
A valid email format does not prove that the address exists. Disabled controls are not a substitute for explaining why an action is unavailable.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Native inputs and buttons. Next lesson: Placeholder and file controls.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Form state pseudo-classes — knowledge check
Five questions on form state pseudo-classes.
When does :invalid match an empty required email?
Which selector is interaction-sensitive?
What makes a button disabled?
What does a valid email syntax establish?
What supplements red error styling?