The HTML required attribute define that the HTML element must be filled out before submitting the form in an HTML document. It can be applied on input, select, and textarea HTML element.
Syntax
Following is the syntax −
<tagname required></tagname>
Example
Let us see an example of HTML required Attribute −
<!DOCTYPE html> <html> <style> body { color: #000; height: 100vh; background-color: #8BC6EC; background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%); text-align: center; } .btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; } </style> <body> <h1>HTML required Attribute Demo</h1> <p>Select your favourite subject:</p> <form method="post"> <select required> <option value="">None</option> <option value="Physics">Physics</option> <option value="Chemistry">Chemistry</option> <option value="Biology">Biology</option> </select> <input type="submit" value="Submit" class="btn"> </form> </body> </html>
Output
Click on “Submit” button without selecting any subject from the drop down list.