Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Tạo kiểu cho input không có required với CSS

Bộ chọn :optional

:optional chọn <input>, <select>, <textarea> **không có** thuộc tính required. Ngược lại với :required.

Ví dụ minh họa

<!DOCTYPE html>
<html>
<head>
  <style>
    input:optional {
      background-color: blue;
      color: white;
    }
  </style>
</head>
<body>
  <form>
    Môn học: <input type="text" name="subject"><br> <!-- optional -->
    Sinh viên: <input type="text" name="student" required><br> <!-- required -->
  </form>
</body>
</html>

Ô "Môn học" không có required → nền xanh. Ô "Sinh viên" có required → không khớp.