Is it possible to make input fields read-only through CSS?
With CSS only? This is sort of possible on text inputs by using user-select:none
:
.print {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
JSFiddle example.
It's well worth noting that this will not work in browsers which do not support CSS3 or support the user-select
property. The readonly
property should be ideally given to the input markup you wish to be made readonly, but this does work as a hacky CSS alternative.
With JavaScript:
document.getElementById("myReadonlyInput").setAttribute("readonly", "true");
Edit: The CSS method no longer works in Chrome (29). The -webkit-user-select
property now appears to be ignored on input elements.
The read-only
attribute in HTML is used to create a text input non-editable. But in case of CSS, the pointer-events
property is used to stop the pointer events.
Syntax:
pointer-events: none;
Example: This example shows two input text, in which one is non-editable.
<!DOCTYPE html>
<html>
<head>
<title>
Disable Text Input field
</title>
<style>
.inputClass {
pointer-events: none;
}
</style>
</head>
<body style = "text-align:center;">
Non-editable:<input class="inputClass" name="input" value="NonEditable">
<br><br>
Editable:<input name="input" value="Editable">
</body>
</html>