CSS pseudo-class - :required



CSS :required pseudo-class select form elements which has the required attribute.

This attribute makes the element a required field, which means that the user must enter a value for it before the form can be submitted.

Syntax

:required {
   /* ... */
}

CSS :required Example

Here is an example that demostrates a simple login form with required fields so that users know which information they need to enter to submit the form −

<html>
<head>
<style>
   .form-container {
      width: 300px;
      margin: 0 auto;
   }
   input:required:invalid {
      border: 2px solid red;
   }
   label {
      display: block;
      margin-top: 10px;
   }
   .submit-conatiner {
      margin-top: 10px;
   }
   button {
      background-color: violet;
      padding: 10px;
      border: none;
   }
</style>
</head>
<body>
   <div class="form-container">
      <form>
      <label for="name">Name:</label>
      <input type="text" id="name" name="name" >

      <label for="email">Email:</label>
      <input type="email" id="email" name="email" >

      <label for="password">Password:</label>
      <input type="password" id="password" name="password" required>

      <div class="submit-conatiner"><button type="submit">Submit</div>
      </form>
   </div>
</body>
</html>
Advertisements