CSS provides powerful styling options for form elements, enhancing the user experience while making forms easier to use and visually appealing. Pseudo-classes are a particularly useful tool in form styling, as they allow developers to target specific states of form elements, such as when they are disabled, checked, or valid.
Forms
CSS Forms and Pseudo Classes
Pseudo Classes for Form Elements
:disabled
The
:disabled
pseudo-class is used to target form elements that are currently disabled. Disabled form elements are typically
unclickable and cannot be interacted with by the user. This pseudo-class can be used to style form elements
like buttons, inputs, select menus, and text areas when they are disabled.
input:disabled {
background-color: lightgray;
color: darkgray;
}
In this example, the background colour of a disabled input field is changed to light gray, and the text colour is set to dark gray, giving the user a visual indication that the field cannot be interacted with.
:enabled
The
:enabled
pseudo-class targets form elements that are enabled. This is essentially the opposite of
:disabled
and allows for styling enabled form fields. It's often used in conjunction with
:disabled
to apply different styles based on the enabled or disabled state of form elements.
input:enabled {
background-color: white;
color: black;
}
This CSS rule ensures that when the input field is enabled, it has a white background and black text, providing clear feedback to the user.
:checked
The
:checked
pseudo-class is used to style elements like checkboxes or radio buttons that are currently selected or checked.
This is particularly useful when you want to change the appearance of a form element based on whether it is
selected or not.
input:checked {
border-color: blue;
background-color: lightblue;
}
Here, when a checkbox or radio button is checked, its border colour changes to blue and its background colour changes to light blue, providing clear visual feedback that the element is selected.
:required
The
:required
pseudo-class targets form elements that are marked as required. These are fields that must be filled out before
the form can be submitted. The
:required pseudo-class allows you to apply specific styles to these fields to
draw attention to their importance.
input:required {
border: 2px solid red;
}
This CSS rule gives required input fields a red border, making them stand out so that users are aware that they must fill out these fields before submitting the form.
:optional
The
:optional
pseudo-class targets form fields that are not required. These are fields where the user has the option to leave
them blank. You can use this pseudo-class to style optional fields differently from required ones, giving users
a visual cue about the non-mandatory nature of the field.
input:optional {
border: 1px solid gray;
}
In this example, optional input fields are given a gray border, providing a subtle distinction between required and optional fields.
:valid
The
:valid
pseudo-class targets form elements that have valid input according to their constraints. For instance, an input
field that requires a specific type of data (like an email address or a number) will be considered valid if the
user enters data that conforms to these rules. This pseudo-class can be used to style fields that are correctly
filled out.
input:valid {
background-color: lightgreen;
}
Here, valid input fields are highlighted with a light green background, providing positive feedback that the entered data is correct.
:invalid
The
:invalid
pseudo-class targets form fields that have invalid input. This is useful for highlighting fields that have not
been filled out correctly or do not meet the specified validation criteria. It's commonly used with
input,
select,
and
textarea
elements.
input:invalid {
background-color: pink;
}
In this case, any input field that contains invalid data will have a pink background, drawing attention to the error and prompting the user to correct their input.
Combining Pseudo Classes With Other CSS Selectors
Pseudo-classes can also be combined with other CSS selectors to achieve more specific styling. For example, you might want to apply a style to only required text inputs, or only to checked checkboxes. You can do that by targeting them with the attribute selector. Here's an example that targets only required text input fields that are valid:
input[type="text"]:required:valid {
border-color: green;
}
This CSS rule ensures that only required text input fields that have valid input will have a green border.