With pseudo-classes, we can target specific states of form elements, such as when a user focuses on a field, enters invalid data, or when a field is required.
The most common pseudo-classes include <:focus>, which applies when the field is active, <:required> for required fields, and <:disabled> for disabled fields.
The following simple example shows a basic form with required fields styled with pseudo-classes.
The <:invalid> pseudo-class is particularly useful when a user enters incorrect data, such as an invalid email address. <:hover> or <:checked> also provide many options to make forms more interactive.
This example shows how a field changes color for invalid input and how the focused field is highlighted.
Pseudo-classes can significantly improve user experience when used wisely.
✨ Ask Lara — your AI study partner
Unlock personalized learning support. Lara can explain lessons, summarize topics, and answer your study questions — available from the Go plan and above.
Lara helps you learn faster — exclusive to ReadyTools Go, Plus, and Max members.