03. Forms

Forms are used to handle user input, and allows the user to submit some information.

Forms are to be enclosed within a <form> tag, with an attribute "action", which points to the script that should be run when a form is submitted.

We don't yet have the tools to process the forms, but in later tutorials, we'll use a back-end server language to do so. :-)

Labels

Firstly, we need to make labels for our input fields, which we can with the <label> tag. We specify which form it's for with the "for" attribute, which is to match with the "id" in our <input> tag. Now when users click on the label, the focus is placed on the corresponding input.

Input Elements

HTML offers a variety of input elements that we can use to gather data from the user. We use the <input> element with a "type" attribute.

Additionally, we use the "name" attribute so that we can retrieve the input data once the form is handled. For a standard text box:

<input type="text" name="textbox">
Password box:
<input type="password" name="password">
Checkbox:
<input type="checkbox" checked>
Radio buttons:
<input type="radio">

After adding labels for each input field, your form should look somthing like this:

<form>
  <label for="firstName">First Name</label>
  <input type="text" name="firstName" id="firstName">
</form>

Text Area

For a bigger submission box, such as one where you put an "about me", we use the <textarea> tag. It's basically a large, multi-line textbox. The two attributes are "rows" and "cols", specifying the textbox dimensions.

Select Boxes

To make select boxes, use the <select> tag.

<select>
  <option>Banana</option>
  <option>Kiwi</option>
  <option>Applie</option>
  <option>Strawberry</option>
</select>

Select your favorite fruit from the following:

Grouping related objects within a select box

To group related objects within a drop-down menu, use <optgroup>

<select>
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="bmw">BMW</option>
  </optgroup>
</select>

Select a car:

Submit

And the most important input button, the submit!

<input type="submit">

After this button is clicked, the page is redirected to wherever the "action" attribute points to.

Build modern and responsive webpages

HTML & CSS

Build modern and responsive webpages Try Front-end

This book teaches HTML as if it's for anyone - hobbyists, students, and professionals - and it's full-color throughout. It utilizes information graphics and lifestyle photography to explain the topics in a simple way that is engaging. You can progress through the chapters from beginning to end or just dip into topics of particular interest at your leisure.

$ Check price
29.9929.99Amazon 4.5 logo(926+ reviews)

More Front-end resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad