The :enabled
pseudo-class is the opposite of :disabled
. It selects form elements that are in their default, interactive state. This is useful for applying styles to active elements, often in contrast to disabled ones.
Example 1: Highlighting Enabled Input Fields
/* This CSS rule targets text inputs that are enabled. */
input[type="text"]:enabled {
/* The border-color property is set to a solid green line. */
border: 2px solid #4CAF50;
}
Explanation
This CSS adds a green border to all text input fields that are enabled. This can help guide the user's attention to the fields they need to fill out.
Example 2: Adding a Hover Effect to Enabled Buttons
/* This CSS rule targets an enabled button on hover. */
button:enabled:hover {
/* The background-color is darkened on hover. */
background-color: #45a049;
}
Explanation
This code enhances user interaction by changing the background color of an enabled button when the user hovers over it. This provides feedback that the button is clickable.
Example 3: Default Styling for Enabled Textareas
/* This CSS rule targets enabled textarea elements. */
textarea:enabled {
/* Sets the background color to a light yellow. */
background-color: #ffffed;
/* Sets the text color. */
color: #333;
}
Explanation
This CSS applies a light yellow background to all enabled <textarea>
elements. This can be used to create a consistent look and feel for all active text entry areas in a form.
Example 4: Pointer Cursor for Enabled Selects
/* This CSS rule targets enabled select elements. */
select:enabled {
/* The cursor property is set to a pointer. */
cursor: pointer;
}
Explanation
This code changes the mouse cursor to a pointer when it is over an enabled <select>
dropdown menu. This indicates to the user that the element is interactive.
Example 5: Transition Effect on Enabled Inputs
/* This CSS rule targets enabled text inputs. */
input[type="text"]:enabled {
/* Defines a transition effect for the border-color property. */
transition: border-color 0.3s ease-in-out;
}
/* This rule applies a different border color on focus. */
input[type="text"]:enabled:focus {
border-color: #007bff;
}
Explanation
This example adds a smooth transition effect to the border color of an enabled text input when it gains focus. This provides a polished and professional feel to form interactions.