- HTML attribute: disabled
- Try it
- Overview
- Attribute interactions
- Usability
- Constraint validation
- Examples
- Specifications
- Browser compatibility
- html.elements.button.disabled
- html.elements.fieldset.disabled
- html.elements.input.disabled
- html.elements.optgroup.disabled
- html.elements.option.disabled
- html.elements.select.disabled
- html.elements.textarea.disabled
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- :disabled
- Примеры
- Пример селекторов
- Пример использования
- Спецификации
- Поддержка браузерами
- Смотрите также
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- How to Disable an Input Field Using CSS?
- How to Disable an Input Field Using CSS?
- What is “pointer-events” CSS Property?
- Example 1: Adding an Input Field Using CSS
- Example 2: Disabling an Input Field Using CSS
- Conclusion
- About the author
- Sharqa Hameed
- CSS :disabled Pseudo Class
- Version
- Syntax
- Example of setting a background color for a disabled element:
- Example of setting a background color for disabled elements:
- Example of a disabled element:
- Browser support
HTML attribute: disabled
The Boolean disabled attribute, when present, makes the element not mutable, focusable, or even submitted with the form. The user can neither edit nor focus on the control, nor its form control descendants.
Try it
Overview
If the disabled attribute is specified on a form control, the element and its form control descendants do not participate in constraint validation. Often browsers grey out such controls and it won’t receive any browsing events, like mouse clicks or focus-related ones.
The disabled attribute is supported by , , , , , and .
This Boolean disabled attribute indicates that the user cannot interact with the control or its descendant controls. If this attribute is not specified, the control inherits its setting from the containing element, for example fieldset ; if there is no containing element with the disabled attribute set, and the control itself does not have the attribute, then the control is enabled. If declared on an , the select is still interactive (unless otherwise disabled), but none of the items in the option group are selectable.
When a supporting element has the disabled attribute applied, the :disabled pseudo-class also applies to it. Conversely, elements that support the disabled attribute but don’t have the attribute set match the :enabled pseudo-class.
This Boolean attribute prevents the user from interacting with the button. If this attribute isn’t set, the button can still be disabled from a containing element, for example ; if there is no containing element with the disabled attribute set, then the button is enabled.
Firefox will, unlike other browsers, persist the dynamic disabled state of a across page loads. Use the autocomplete attribute to control this feature.
Attribute interactions
The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled.
Because a disabled field cannot have its value changed, required does not have any effect on inputs with the disabled attribute also specified. Additionally, since the elements become immutable, most other attributes, such as pattern , have no effect, until the control is enabled.
Note: The required attribute is not permitted on inputs with the disabled attribute specified.
Usability
Browsers display disabled form controls greyed as disabled form controls are immutable, won’t receive focus or any browsing events, like mouse clicks or focus-related ones, and aren’t submitted with the form.
If present on a supporting elements, the :disabled pseudo class will match. If the attribute is not included, the :enabled pseudo class will match. If the element doesn’t support the disabled attribute, the attribute will have no effect, including not leading to being matched by the :disabled and :enabled pseudo classes.
Constraint validation
If the element is disabled , then the element’s value can not receive focus and cannot be updated by the user, and does not participate in constraint validation.
Examples
fieldset> legend>Checkboxeslegend> p> label> input type="checkbox" name="chbox" value="regular" /> Regular label> p> p> label> input type="checkbox" name="chbox" value="disabled" disabled /> disabled label> p> fieldset> fieldset> legend>Radio buttonslegend> p> label> input type="radio" name="radio" value="regular" /> Regular label> p> p> label> input type="radio" name="radio" value="disabled" disabled /> disabled label> p> fieldset> p> label >Select an option: select> optgroup label="Group 1"> option>Option 1.1option> optgroup> optgroup label="Group 2"> option>Option 2.1option> option disabled>Option 2.2option> option>Option 2.3option> optgroup> optgroup label="Group 3" disabled> option>Disabled 3.1option> option>Disabled 3.2option> option>Disabled 3.3option> optgroup> select> label> p> fieldset disabled> legend>Disabled fieldsetlegend> p> label> Name: input type="name" name="radio" value="regular" /> Regular label> p> p> label>Number: input type="number" />label> p> fieldset>
Specifications
Browser compatibility
html.elements.button.disabled
BCD tables only load in the browser
html.elements.fieldset.disabled
BCD tables only load in the browser
html.elements.input.disabled
BCD tables only load in the browser
html.elements.optgroup.disabled
BCD tables only load in the browser
html.elements.option.disabled
BCD tables only load in the browser
html.elements.select.disabled
BCD tables only load in the browser
html.elements.textarea.disabled
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on May 22, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
:disabled
CSS псевдокласс :disabled находит любой отключённый элемент. Элемент отключён, если не может быть активирован (например, его нельзя выбрать, нажать на него или ввести текст) или получить фокус. У элемента также есть включённое состояние, когда его можно активировать или сфокусировать.
Примеры
Пример селекторов
Выберет все отключённые поля ввода
Найдёт все отключённые select элементы с классом country
Пример использования
input[type="text"]:disabled background: #ccc; >
Применим к этому HTML5 фрагменту:
form action="#"> fieldset> legend>Адрес доставкиlegend> input type="text" placeholder="Имя"> input type="text" placeholder="Адрес"> input type="text" placeholder="Почтовый индекс"> fieldset> fieldset id="billing"> legend>Адрес оплатыlegend> label for="billing_is_shipping">Такой же как адрес доставки:label> input type="checkbox" onchange="javascript:toggleBilling()" checked> br /> input type="text" placeholder="Имя" disabled> input type="text" placeholder="Адрес" disabled> input type="text" placeholder="Почтовый индекс" disabled> fieldset> form>
Добавим немного javascript:
function toggleBilling() var billingItems = document.querySelectorAll('#billing input[type="text"]'); for (var i = 0; i billingItems.length; i++) billingItems[i].disabled = !billingItems[i].disabled; > >
Результатом будет отключение всех полей в группе адреса оплаты и окраска их в серый цвет.
Спецификации
Поддержка браузерами
BCD tables only load in the browser
Смотрите также
Found a content problem with this page?
This page was last modified on 14 февр. 2023 г. by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
How to Disable an Input Field Using CSS?
The input field is used to make forms and take input from the user. Users can fill the input field according to the input type. But sometimes, you must disable the input field to fulfil any precondition, such as selecting a checkbox. In that situation, you need to disable the input field.
In this guide, we will gain an understanding of how to disable the input field using CSS. So, Let’s begin!
How to Disable an Input Field Using CSS?
In CSS, events are disabled by using the “pointer-events” property. So, first, learn about the pointer-events property.
What is “pointer-events” CSS Property?
The “pointer-events” control how the HTML elements respond or behave to the touch event, such as click or tap events, active or hover states, and whether the cursor is visible or not.
Syntax
The syntax of pointer-events is given as follows:
The above mention property takes two values, such as “auto” and “none”:
- auto: It is used to perform default events.
- none: It is utilized to disable events.
Head towards the given example.
Example 1: Adding an Input Field Using CSS
In this example, firstly, we will create a div and add a heading and input field to it. Then, set the input type as “text” and set its value as “Enter Your Name”.
After that, move to the CSS and style the div by setting its background color as “rgb(184, 146, 99)” and height as “150px”.
The output of the above-described code is given below. Here, we can see that our input field is currently active and is accepting the input from the user:
Now, move to the next part in which we use the value of the “pointer-events” property as “none”.
Example 2: Disabling an Input Field Using CSS
We will now use “input” to access the element added in the HTML file and set the value of pointer-events as “none”:
Once you implement the above-stated property “pointer-events” with “none” value, the text of the input field will be non-editable which indicates that our input field is disabled:
That’s it! We have explained the method of disabling the input field using CSS.
Conclusion
To disable an input field in an HTML, the “pointer-events” property of the CSS is used. To do so, add an input field, and set the value of pointer-events as “none” to disable the input field. In this guide, we explain the method of disabling an input field using CSS and provide an example of it.
About the author
Sharqa Hameed
I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.
CSS :disabled Pseudo Class
The :disabled pseudo-class selects and styles elements that are disabled.
These elements are usually form elements, such as buttons ( ), select menus ( ), input types ( ), and textareas ( ).
Disabled elements do not accept clicks, text input, or focus.
Version
Syntax
Example of setting a background color for a disabled element:
html> html> head> title>Title of the document title> style> input < padding: 2px 5px; margin-bottom: 10px; border: 1px solid #ccc; > input[type=text]:enabled < background: #eee; > input[type=text]:disabled < background: #ccc; > style> head> body> h2>:disabled selector example h2> form action=""> label for="name">First name: label> input type="text" value="John" id="name"> br> label for="lastname">Last name: label> input type="text" value="Smith" id="lastname"> br> label for="country">Country: label> input type="text" disabled="disabled" value="10 High Street" id="conutry"> form> body> html>
Example of setting a background color for disabled elements:
html> html> head> title>Title of the document title> style> option:disabled < background: #8ebf42; > style> head> body> h2>:disabled selector example h2> select> option value="paris">Paris option> option value="london" disabled>London option> option value="moscow">Moscow option> option value="rome" disabled>Rome option> option value="berlin">Berlin option> select> body> html>
Example of a disabled element:
html> html> head> title>Title of the document title> style> input < width: 60%; margin: 0; border: none; outline: 1px solid lightgrey; outline-offset: 2px; > input:disabled < background: #cccccc; cursor: not-allowed; > form < background: #67a6ec; padding: 1.5em; max-width: 400px; width: 100%; outline: 10px solid rgba(17, 58, 103, 0.6); > hr < visibility: hidden; > label < margin-right: 3%; text-align: left; display: inline-block; width: 35%; > style> head> body> h2>:disabled selector example h2> form action="#"> label for="name">Enabled Input: label> input type="text" autofocus> hr> label for="name">Disabled Input: label> input type="text" disabled> form> body> html>