:read-write

Baseline 已廣泛支援

此特性已經十分成熟,可在許多裝置和瀏覽器版本上使用。自 2020 年 7 月以來,它已在各大瀏覽器中可用。

:read-write CSS 偽類表示可由使用者編輯的元素(例如 inputtextarea)。

試一試

label,
input[type="submit"] {
  display: block;
  margin-top: 1em;
}

*:read-write {
  background-color: ivory;
  border: 2px solid darkorange;
  border-radius: 5px;
}
<p>Please fill in 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-write {
  /* ... */
}

示例

使用只讀控制元件確認表單詳細資訊

當您希望使用者驗證他們之前輸入的資訊,並希望將其與可讀寫控制元件中的新資料一起提交時,可以使用 readonly 表單控制元件。在下面的示例中,:read-only 偽類用於使 <textarea>(使用者的地址)看起來像一個普通段落。:read-write 偽類提供了一種突出顯示可編輯的 <textarea>(送貨說明)的方法。

css
textarea:read-only {
  border: 0;
}

textarea:read-write {
  box-shadow: inset 1px 1px 3px #cccccc;
  border-radius: 5px;
}
html
<form>
  <fieldset>
    <legend>Confirm details</legend>
    <div>
      <label for="address">Address:</label>
      <textarea id="address" name="address" readonly>
123 Choco Mountain,
Awesome Ridge,
CA</textarea
      >
    </div>
    <div>
      <label for="instructions">Delivery instructions</label>
      <textarea id="instructions" name="instructions"></textarea>
    </div>
  </fieldset>
  <button type="submit">Confirm</button>
</form>

樣式化可讀寫非表單控制元件

此選擇器不僅選擇 <input>/<textarea> 元素 — 它將選擇任何可由使用者編輯的元素,例如設定了 contenteditable<p> 元素。

html
<p contenteditable>This paragraph is editable; it is read-write.</p>

<p>This paragraph is not editable; it is read-only.</p>
css
body {
  font-family: sans-serif;
}

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-write
選擇器 Level 4
# read-write-pseudo

瀏覽器相容性

另見