
CSS Topics
- CSS Intro
- CSS How To
- CSS Editors
- CSS Properties
- CSS Selectors
- .class
- .class1.class2
- .class1 .class2
- #id
- * (all)
- element
- element.class
- element,element
- element element
- element>element
- element+element
- element1~element2
- [attribute]
- [attribute=value]
- [attribute~=value]
- [attribute|=value]
- [attribute^=value]
- [attribute$=value]
- [attribute*=value]
- :active
- ::after
- ::before
- :checked
- :default
- :disabled
- :empty
- :enabled
- :first-child
- ::first-letter
- ::first-line
- :first-of-type
- :focus
- :fullscreen
- :has()
- :hover
- :in-range
- :indeterminate
- :invalid
- :lang()
- :last-child
- :last-of-type
- :link
- ::marker
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-of-type
- :only-child
- :optional
- :out-of-range
- ::placeholder
- :read-only
- :read-write
- :required
- :root
- ::selection
- :target
- :valid
- :visited
- CSS Comments
- CSS Length
- CSS Image Sprites
- CSS Grid Layout
- CSS Grid Flexbox
- CSS @charset Rule
- CSS @font-face Rule
- CSS @import Rule
- CSS @keyframes Rule
- CSS @media Rule
CSS :enabled Selector

Photo Credit to CodeToFun
🙋 Introduction
The :enabled
selector in CSS is used to select form elements that are currently enabled and can be interacted with. This pseudo-class is particularly useful for applying styles to form elements based on their state, enhancing user experience and accessibility.
💡 Syntax
The signature of the :enabled
Selector is as follows:
:enabled {
/* CSS properties */
}
The :enabled
pseudo-class can be used with any form control element that supports the enabled/disabled states, such as <input>
, <button>
, <select>
, and <textarea>
.
📝 Example
Here is an example of how to use the :enabled
selector in CSS:
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :enabled Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<input type="text" id="name" placeholder="Enter your name">
<button type="submit">Submit</button>
<input type="checkbox" id="agree">
<label for="agree">I agree to the terms</label>
</form>
</body>
</html>
🎨 CSS
/* Style for enabled elements */
:enabled {
border: 2px solid green;
background-color: #e0ffe0;
}
/* Style for disabled elements */
:disabled {
border: 2px solid gray;
background-color: #f5f5f5;
}
In this example:
- Enabled form controls have a green border and a light green background.
- Disabled form controls have a gray border and a light gray background.
💬 Usage Tips
- The
:enabled
selector only targets elements that are enabled and can be interacted with. It does not apply to elements that are disabled. - Combine
:enabled
with other selectors to target specific elements within a form. For instance, you can style only enabled buttons using button:enabled.
⚠️ Common Pitfalls
- Ensure that you use
:enabled
with form elements that support the disabled attribute. It won't have any effect on elements that don't. - Test across different browsers to ensure consistent styling, as some older browsers might not fully support all CSS pseudo-classes.
🎉 Conclusion
The :enabled
selector is a powerful tool for enhancing the visual presentation of interactive form elements.
By leveraging this pseudo-class, you can provide clear visual feedback to users about which elements are active and can be interacted with. Using :enabled
effectively contributes to a more intuitive and user-friendly web experience.
👨💻 Join our Community:
Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :enabled Selector), please comment here. I will help you immediately.