Skip to content

Forms

Learn how to create interactive forms that collect user input - essential for login pages, contact forms, surveys, and more.

What are Forms?

Forms allow users to submit data to a server. They're used for: - Login and registration - Contact forms - Search boxes - Surveys and polls - E-commerce checkout - And much more

Basic Form Structure

All forms start with the <form> tag:

<form>
    <!-- Form elements go here -->
</form>

Form Attributes

<form action="/submit" method="post">
    <!-- Form content -->
</form>
  • action - Where to send the form data (URL)
  • method - How to send data (get or post)

!!! note "Backend Required" Forms typically need a backend server to process the data. For now, we'll focus on the HTML structure.

Text Input

The most common form element for single-line text:

<form>
    <label for="username">Username:</label>
    <input type="text" id="username" name="username">
</form>

Input Attributes

<input type="text" 
       id="username" 
       name="username" 
       placeholder="Enter your username"
       required
       maxlength="20"
       value="default">
  • type - Type of input (text, email, password, etc.)
  • id - Unique identifier (used with <label>)
  • name - Name sent to server (required for submission)
  • placeholder - Hint text shown in empty field
  • required - Field must be filled
  • maxlength - Maximum characters allowed
  • value - Default value

Labels

Labels make forms accessible and user-friendly:

<!-- Method 1: Using 'for' attribute -->
<label for="email">Email:</label>
<input type="email" id="email" name="email">

<!-- Method 2: Wrapping input -->
<label>
    Email:
    <input type="email" name="email">
</label>

!!! tip "Always Use Labels" Labels improve accessibility and make forms easier to use. Clicking a label focuses its input field.

Input Types

HTML5 provides many input types for different data:

Text Inputs

<!-- Single-line text -->
<input type="text" name="name" placeholder="Your name">

<!-- Password (hidden characters) -->
<input type="password" name="password" placeholder="Password">

<!-- Email (validates email format) -->
<input type="email" name="email" placeholder="your@email.com">

<!-- Number -->
<input type="number" name="age" min="0" max="120">

<!-- URL -->
<input type="url" name="website" placeholder="https://example.com">

<!-- Telephone -->
<input type="tel" name="phone" placeholder="123-456-7890">

<!-- Search -->
<input type="search" name="query" placeholder="Search...">

Date and Time

<!-- Date -->
<input type="date" name="birthday">

<!-- Time -->
<input type="time" name="appointment">

<!-- Date and Time -->
<input type="datetime-local" name="event">

<!-- Month -->
<input type="month" name="month">

<!-- Week -->
<input type="week" name="week">

Other Input Types

<!-- Color picker -->
<input type="color" name="favorite-color">

<!-- File upload -->
<input type="file" name="document">

<!-- Range slider -->
<input type="range" name="volume" min="0" max="100" value="50">

<!-- Hidden field (not visible to user) -->
<input type="hidden" name="user-id" value="123">

Textarea

For multi-line text input:

<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50" placeholder="Enter your message"></textarea>

Attributes: - rows - Number of visible rows - cols - Number of visible columns - placeholder - Hint text - required - Field must be filled

Select Dropdown

For choosing from a list of options:

<label for="country">Country:</label>
<select id="country" name="country">
    <option value="">Select a country</option>
    <option value="us">United States</option>
    <option value="uk">United Kingdom</option>
    <option value="ca">Canada</option>
</select>

Multiple Selection

<label for="languages">Languages (hold Ctrl to select multiple):</label>
<select id="languages" name="languages" multiple>
    <option value="html">HTML</option>
    <option value="css">CSS</option>
    <option value="js">JavaScript</option>
</select>

Option Groups

<select name="car">
    <optgroup label="European">
        <option value="bmw">BMW</option>
        <option value="mercedes">Mercedes</option>
    </optgroup>
    <optgroup label="American">
        <option value="ford">Ford</option>
        <option value="chevrolet">Chevrolet</option>
    </optgroup>
</select>

Checkboxes

For multiple selections (yes/no options):

<fieldset>
    <legend>Select your interests:</legend>

    <input type="checkbox" id="coding" name="interests" value="coding">
    <label for="coding">Coding</label><br>

    <input type="checkbox" id="design" name="interests" value="design">
    <label for="design">Design</label><br>

    <input type="checkbox" id="music" name="interests" value="music">
    <label for="music">Music</label>
</fieldset>

Checked by Default

<input type="checkbox" id="newsletter" name="newsletter" value="yes" checked>
<label for="newsletter">Subscribe to newsletter</label>

Radio Buttons

For single selection from multiple options:

<fieldset>
    <legend>Choose your plan:</legend>

    <input type="radio" id="basic" name="plan" value="basic">
    <label for="basic">Basic</label><br>

    <input type="radio" id="pro" name="plan" value="pro">
    <label for="pro">Pro</label><br>

    <input type="radio" id="enterprise" name="plan" value="enterprise">
    <label for="enterprise">Enterprise</label>
</fieldset>

!!! tip "Same 'name' for Radio Buttons" Radio buttons with the same name are grouped together - only one can be selected.

Fieldset and Legend

Group related form fields:

<fieldset>
    <legend>Personal Information</legend>

    <label for="first-name">First Name:</label>
    <input type="text" id="first-name" name="first-name"><br><br>

    <label for="last-name">Last Name:</label>
    <input type="text" id="last-name" name="last-name">
</fieldset>

<fieldset>
    <legend>Contact Information</legend>

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

    <label for="phone">Phone:</label>
    <input type="tel" id="phone" name="phone">
</fieldset>

Buttons

Submit Button

<button type="submit">Submit</button>
<!-- OR -->
<input type="submit" value="Submit">

Reset Button

<button type="reset">Reset Form</button>
<!-- OR -->
<input type="reset" value="Reset">

Regular Button

<button type="button">Click Me</button>

!!! tip "Use