CSS :required Selector

Selector :required chọn phần tử có thuộc tính required và thiết lập CSS. Thuộc tính required của các phần tử Form được định nghĩa là bắt buộc.

Note: Selector này dùng với các phần tử form như <input>, <select>, và <textarea>.

Syntax:

:required {
// CSS Property
}

Example: Trong ví dụ này, chúng ta sử dụng CSS selector :required.

html
<!DOCTYPE html>
<html>

<head>
    <title>:required Selector</title>
    <style>
        h1 {
            color: green;
        }

        body {
            text-align: center;
        }

        input:required {
            background-color: green;
            color: white;
        }

        form div {
            padding: 10px;
        }

        label {
            display: block;
            font-size: 18px;
        }

        input {
            border: 1px solid black;
            padding: 9px;
            font-size: 18px;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>:required selector</h2>
    <form>
        <div>
            <label for="name">Name</label>
            <input type="text" name="name" id="name" />
        </div>
        <div>
            <label for="email">Email</label>
            <input type="email" name="email" id="email" required />
        </div>
        <div>
            <input type="submit" value="Submit" />
        </div>
    </form>
</body>

</html>

Output:

css-required-selector

Supported Browsers: Các trình duyệt hỗ trợ :required Selector được liệt kê dưới đây:

  • Apple Safari 5.0
  • Google Chrome 10.0
  • Edge 12.0
  • Firefox 4.0
  • Opera 10.0

Last Updated : 21/07/2025