Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS :disabled Selector

Posted in CSS Tutorial
Updated on Sep 16, 2024
By Mari Selvan
👁️ 4 - Views
⏳ 4 mins
💬 0
CSS :disabled Selector

Photo Credit to CodeToFun

🙋 Introduction

The :disabled selector in CSS is used to select form elements that are currently disabled, meaning they cannot be interacted with or modified by the user.

This pseudo-class is particularly useful for providing visual feedback on form controls that are temporarily or permanently unavailable.

💡 Syntax

The signature of the :disabled Selector is as follows:

Syntax
Copied
Copy To Clipboard
:disabled {
    /* CSS properties */
}

The :disabled pseudo-class can be applied to form elements like <input>, <button>, <select>, <textarea>, and other elements that can be disabled using the disabled attribute.

📝 Example

Here is an example demonstrating how to use the :disabled selector in CSS:

☠️ HTML

HTML
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS :disabled Selector Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <form>
        <input type="text" id="username" placeholder="Username" disabled>
        <input type="password" id="password" placeholder="Password">
        <button type="submit">Login</button>
    </form>
</body>
</html>

🎨 CSS

CSS
Copied
Copy To Clipboard
/* Style for disabled elements */
:disabled {
    background-color: #f5f5f5;
    border: 2px solid gray;
    color: #aaa;
    cursor: not-allowed;
}

/* Style for enabled elements */
:enabled {
    background-color: #e0ffe0;
    border: 2px solid green;
}

In this example:

  • Disabled elements, like the username field, have a gray background, a gray border, and text with reduced visibility.
  • Enabled elements, like the password field and button, have a green border and a lighter background color.

💬 Usage Tips

  • The :disabled selector is often used alongside :enabled to create distinct styles for both states.
  • You can disable form elements using the disabled attribute in HTML. This will automatically trigger the styles defined in your :disabled selector.
  • Combine the :disabled selector with other form element selectors (e.g., input:disabled, button:disabled) for more specific targeting.

⚠️ Common Pitfalls

  • The :disabled selector will only work on elements that support the disabled attribute, such as form controls. It does not apply to elements like <div> or <span>, which cannot be disabled.
  • Make sure to provide a visual cue for disabled elements to inform users that they are not interactive. A common mistake is using only subtle changes in styling, which may not be noticeable to all users.

🎉 Conclusion

The :disabled selector is essential for controlling the appearance of form elements that cannot be interacted with.

By applying appropriate styles, you can ensure users are aware of which controls are disabled and prevent confusion during form submission. Proper use of the :disabled selector enhances both user experience and accessibility on your website.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy