: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

浏览器兼容性

另见