:read-only
:read-only CSS 伪类选择用户无法编辑的元素(例如某些 <input> 类型和 <textarea>)。HTML 属性 readonly 对其无效的元素(例如 <input type="radio">、<input type="checkbox"> 以及所有其他非表单元素)也由 :read-only 伪类选择。实际上,:read-only 匹配任何 :read-write 不匹配的元素,使其等同于 :not(:read-write)。
试一试
label,
input[type="submit"] {
display: block;
margin-top: 1em;
}
*:read-only {
font-weight: bold;
color: indigo;
}
<p>Please fill your details:</p>
<form>
<label for="email">Email Address:</label>
<input id="email" name="email" type="email" value="test@example.com" />
<label for="note">Short note about yourself:</label>
<textarea id="note" name="note">Don't be shy</textarea>
<label for="pic">Your picture:</label>
<input id="pic" name="pic" type="file" />
<input type="submit" value="Submit form" />
</form>
语法
css
:read-only {
/* ... */
}
示例
使用只读或读写控件确认表单信息
只读表单控件的一个用途是允许用户检查和验证他们可能在早期表单中输入的信息(例如,发货详情),同时仍能够将信息与表单的其余部分一起提交。我们在下面的示例中就是这样做的。
:read-only 伪类用于删除所有使输入看起来像可点击字段的样式,使它们看起来更像只读段落。另一方面,:read-write 伪类用于为可编辑的 <textarea> 提供一些更好的样式。
css
input:read-only,
textarea:read-only {
border: 0;
box-shadow: none;
background-color: #dddddd;
}
textarea:read-write {
outline: 1px dashed red;
outline-offset: 2px;
border-radius: 5px;
}
样式化只读非表单控件
此选择器不仅选择 <input>/<textarea> 元素——它将选择用户无法编辑的任何元素。
html
<p contenteditable>This paragraph is editable; it is read-write.</p>
<p>This paragraph is not editable; it is read-only.</p>
css
p {
font-size: 150%;
padding: 5px;
border-radius: 5px;
}
p:read-only {
background-color: red;
color: white;
}
p:read-write {
background-color: lime;
}
规范
| 规范 |
|---|
| HTML # selector-read-only |
| 选择器 Level 4 # read-only-pseudo |
浏览器兼容性
加载中…
另见
:read-write- HTML
contenteditable属性