optional

CSS :optional Pseudo Class

The :optional selector selects optional elements.

Form elements are optional when attribute is not required. Form elements that can be selected using :optional are <input>, <select> and <textarea> without a required attribute.

The :optional selector can be linked with other selectors (e.g. :hover) and with pseudo-elements (e.g. ::after).

Version

Version

CSS Basic User Interface Module

CSS2 Universal Selector

Syntax

Syntax

:optional {
  css declarations;
}

Example of the :optional selector:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</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>

In the given example both the :optional and the :required pseudo-class selectors are used.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部