📝

How to Create a Form in HTML

HTML/CSSBeginner

Build interactive forms with validation.

Code Example

HTML/CSS
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: sans-serif; max-width: 400px; margin: 40px auto; padding: 20px; }
  h2 { text-align: center; color: #1f2937; }
  .form-group { margin-bottom: 15px; }
  label { display: block; margin-bottom: 5px; font-weight: 500; color: #374151; }
  input, select { width: 100%; padding: 10px; border: 1px solid #d1d5db; border-radius: 6px; box-sizing: border-box; }
  input:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59,130,246,0.1); }
  button { width: 100%; padding: 12px; background: #3b82f6; color: white; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; }
  button:hover { background: #2563eb; }
</style>
</head>
<body>
  <h2>Sign Up</h2>
  <form onsubmit="event.preventDefault(); alert('Form submitted!');">
    <div class="form-group">
      <label>Full Name</label>
      <input type="text" placeholder="John Doe" required>
    </div>
    <div class="form-group">
      <label>Email</label>
      <input type="email" placeholder="john@example.com" required>
    </div>
    <div class="form-group">
      <label>Password</label>
      <input type="password" placeholder="••••••••" minlength="8" required>
    </div>
    <button type="submit">Create Account</button>
  </form>
</body>
</html>

Explanation

Forms use <form> with input types like text, email, password. Add required, minlength for validation. Handle submit with onsubmit. 💡 Note: If the preview doesn't load properly, right-click and select "Open in New Tab" to see the full interactive form.

Related Tutorials