: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屬性