语法

:optional {
  css declarations;
}

:optional 选择器的例子:

<!DOCTYPE html>
<html>
  <head>
    <title>文档的标题</title>
    <style>
      .example {
        margin: 40px auto;
        max-width: 400px;
      }
      label,
      button {
        display: block;
        width: 100%;
        margin-bottom: 1.5em;
      }
      input,
      select,
      button {
        padding: .4em 1em;
      }
      input,
      select {
        border: 1px solid #666666;
      }
      input:optional,
      select:optional {
        background-color: #eeeeee;
        color: #666666;
        opacity: 0.5;
        transition: .3s;
      }
      input:optional:hover,
      select:optional:hover {
        opacity: 1;
      }
      input:required,
      textarea:required {
        border-bottom: 3px solid #1c87c9;
      }
    </style>
  </head>
  <body>
    <h2>:optional selector example</h2>
    <div class="example">
      <form action="#">
        <label>
          <input type="name" required>Name *
        </label>
        <label>
          <input type="email" required>Email *
        </label>
        <label>
          <input type="phone">Phone (optional)
        </label>
        <label>
          <input type="url">Address (optional)
        </label>
      </form>
    </div>
  </body>
</html>

在给定的示例中, :optional 和 :required 伪类选择器都被使用。

CSS : 可选的伪类

:optional 选择器选择可选元素。

当不需要属性时,表单元素是可选的。

可以使用 :optional 选择的表单元素是 <input>、<select> 和 <textarea>,没有必需的属性。

:optional 选择器可以与其他选择器(例如 :hover)和伪元素(例如 ::after)。

日期:2020-06-02 22:14:41 来源:oir作者:oir