A form is wrap in a form element. Which take two essential attributes:
action: the url to send the data tomethod: POST or GET<form action="example.com/path" method="post">
<label for="first_name">First Name:</label>
<input type="text" id="first_name">
</form>
Most versatile element, accepts a type attribute that determine the type of
information collected.
To add a text above it, add a label element with a for attribute value
that match the id of the input.
<input type="text" id="first_name" placeholder="your name" name="first_name">
<label for="first_name">First Name:</label>
type: type of information collectedid: must match the for attribute in the labelplaceholder: text display by defaultname: key name used when send to the backendvalue: value send to the backen (for checkbox, radio…)text: single line textemailpasswordnumberdatecheckboxradiofile: select a file to sendtel: phone numberurlrange: sliderdatetime-local, month, time, week: select a time/date.colorhidden: not visible by the user, can send info to server like the date.Let the user enter a longer text, like a comment.
<textarea id="" name="" cols="30" rows="10">Placeholder text</textarea>
Wrap it in a <select> with each option in its own <option>
You can group the option inside a <optgroup>
Add the attribute selected to an option to have it as default.
<select name="Car">
<optgroup label="big">
<option value="mercedes" selected>Mercedes</option>
<option value="tesla">Tesla</option>
<option value="volvo">Volvo</option>
</optgroup>
<optgroup label="small">
<option value="bmw">BMW</option>
<option value="mini">Mini</option>
<option value="ford">Ford</option>
</optgroup>
</select>
Box where several option can be checked. Can also have only one option to have a toggle.
It is an <input> with the type set as checkbox.
All the option must have the same name attibute.
<h1>Pizza Toppings</h1>
<div>
<input type="checkbox" id="sausage" name="topping" value="sausage" checked>
<label for="sausage">Sausage</label>
</div>
<div>
<input type="checkbox" id="onions" name="topping" value="onions">
<label for="onions">Onions</label>
</div>
Checkbox where only one option can be checked.
It is an <input> with the type set as radio.
All the option must have the same name attibute.
<ul>
<li>
<label for="soup">Soup</label>
<input type="radio" id="soup" name="meal" value="soup" checked />
</li>
<li>
<label for="pizza">Pizza</label>
<input type="radio" id="pizza" name="meal" value="pizza" />
</li>
</ul>
Three type:
submit: send the formreset: clear the value entered in the formbutton: generic button that can be whatever you want (set with js)<button type="submit">Submit</button>
<button type="reset">Submit</button>
<button type="button">Submit</button>
A <fieldset> is a container that group related form input into
a logical unit. It create a separation between group.
<fieldset>
<label for="first_name">First name</label>
<input type="text" id="first_name" name="first_name">
<label for="last_name">Last name</label>
<input type="text" id="last_name" name="last_name">
</fieldset>
It is the title of a fieldset, it should come right after this one.
<fieldset>
<legend>Contact detail</legend>
<label for="first_name">First name</label>
<input type="text" id="first_name" name="first_name">
<label for="last_name">Last name</label>
<input type="text" id="last_name" name="last_name">
</fieldset>
It is common to wrap the form elements in a list to make styling easier.
<form action="/my-handling-form-page" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="user_name" />
</li>
<li>
<label for="mail">Email:</label>
<input type="email" id="mail" name="user_email" />
</li>
</ul>
</form>