Home / Learn HTML / Elements: Tables and Forms
Elements: Tables and Forms
Rows, columns and form controls. These are the elements that collect and show data.
About this lesson
Tables show data in rows and columns. Forms collect data from visitors. Both have many small elements that work together, so each one is explained with its job and its attributes.
The <table> element
The table element shows data in rows and columns.
When to use it: Use it only for real tabular data such as prices or results, never for page layout. Build it from tr, th and td.
Why we use it
It shows data in rows and columns. Tables are for data, not for page layout.
How it works
The browser lines up cells in a grid. th cells act as headers for screen readers.
Where it is used
- Price lists
- Schedules
- Reports and statistics
Common mistakes
Do not use tables for layout. Add a caption and th with scope.
| Attribute | Meaning |
|---|---|
border (old) | draw borders |
width | Common attribute |
border-collapse, padding and border.A table.
The <caption> element
The caption element is the title of a table.
When to use it: Place it as the first child of table.
Why we use it
It is the title of a table.
How it works
It must be the first child of table. Screen readers announce it first.
Where it is used
- Naming a table such as "Monthly sales"
Common mistakes
Do not replace it with a heading far away from the table.
The table title.
The <thead> element
The thead element groups the header rows of a table.
When to use it: Place it before tbody. It lets browsers repeat the header on every printed page.
Why we use it
It groups the header rows of a table.
How it works
Browsers can repeat it on every printed page.
Where it is used
- Column titles
Common mistakes
Use it with tbody.
Header rows group.
The <tbody> element
The tbody element groups the main body rows of a table.
When to use it: Use it after thead. Browsers add it automatically if you leave it out.
Why we use it
It groups the body rows of a table.
How it works
It separates data from header and footer so you can scroll or style it separately.
Where it is used
- Main data rows
Common mistakes
The browser adds it automatically if you forget.
Body rows group.
The <tfoot> element
The tfoot element groups the footer rows of a table, such as totals.
When to use it: Place it after tbody (or before it, for older rules).
Why we use it
It groups the footer rows such as totals.
How it works
It is drawn at the bottom of the table.
Where it is used
- Total rows
- Notes under a table
Common mistakes
Place it after thead or after tbody in modern HTML.
Footer rows group.
The <tr> element
The tr element is one row of a table.
When to use it: Place it inside table, thead, tbody or tfoot. It holds th and td cells.
Why we use it
It is one row of cells.
How it works
Each tr creates a row. Cells inside line up in columns.
Where it is used
- Each row of a table
Common mistakes
Every row should have the same number of cells unless you use colspan.
A table row.
The <th> element
The th element is a header cell. It is bold and centered by default.
When to use it: Use it for column or row titles. Set scope so screen readers know which cells it labels.
Why we use it
It is a header cell that labels a column or row.
How it works
It is bold and centered by default. The scope attribute tells screen readers which cells it labels.
Where it is used
- Column and row headings
Common mistakes
Do not use it only to make text bold.
| Attribute | Meaning |
|---|---|
scope | col or row |
colspan, rowspan | join cells |
headers | Common attribute |
th for headings, not for normal data.A heading cell.
The <td> element
The td element is a data cell in a table row.
When to use it: Use it for the values of a table.
Why we use it
It is a data cell in a row.
How it works
The colspan and rowspan attributes let it span several columns or rows.
Where it is used
- Table values
Common mistakes
Do not put layout in cells.
| Attribute | Meaning |
|---|---|
colspan | join columns |
rowspan | join rows |
headers | Common attribute |
A data cell.
The <colgroup> element
The colgroup element groups columns so you can style them together.
When to use it: Place it right after caption, before the rows.
Why we use it
It groups columns so you can style or set width for them together.
How it works
It must come before the rows. Each col inside sets the style for one column.
Where it is used
- Highlighting a column
- Setting widths
Common mistakes
Only a few CSS properties work on col.
| Attribute | Meaning |
|---|---|
span | how many columns it covers |
Groups columns for styling.
The <col> element
The col element defines the style of a column inside colgroup. It is an empty element.
When to use it: Use it to set width or background for a whole column.
Why we use it
It sets style or width for one column.
How it works
Its span attribute lets it cover several columns.
Where it is used
- Column widths
Common mistakes
Few styles apply, mostly width, background and border.
| Attribute | Meaning |
|---|---|
span | how many columns it covers |
Styles a column.
The <form> element
The form element wraps controls that collect information from the visitor and sends it to a server.
When to use it: Put inputs, labels and a submit button inside it.
Why we use it
It collects user data and sends it to a server or script.
How it works
The action attribute is where to send it and method is GET or POST. The submit button sends all named controls.
Where it is used
- Login and signup
- Contact forms
- Search boxes
- Checkout
Common mistakes
Always add labels and a submit button. Validate again on the server.
| Attribute | Meaning |
|---|---|
action | where to send the data |
method | get or post |
enctype | multipart/form-data for files |
target | Common attribute |
autocomplete | on or off |
novalidate | skip browser checks |
get; forms with private data use post.A form.
The <label> element
The label element is the visible name of a form control. Clicking it focuses the control.
When to use it: Always give every control a label. Connect them with for and the control's id, or wrap the control inside the label.
Why we use it
It names a form control. Clicking the label focuses the control.
How it works
The for attribute matches the id of the input. A label can also wrap the input.
Where it is used
- Every input, checkbox and radio
Common mistakes
Never use placeholder as a replacement for a label.
| Attribute | Meaning |
|---|---|
for | the id of the control |
A name for a form control.
The <input> element
The input element is the main form control. Its type decides what it is: text box, checkbox, radio button, date picker, file chooser and many more. It is an empty element.
When to use it: Choose the right type and always add name and a label.
Why we use it
It is the main form control. Its type decides what it is: text, email, password, checkbox, date, file and more.
How it works
The browser shows the right control and keyboard for the type and validates it using attributes like required and pattern.
Where it is used
- Text fields
- Checkboxes and radios
- Date and colour pickers
- File uploads
Common mistakes
Always set a type, name and a label. Use the right type for mobile keyboards.
| Attribute | Meaning |
|---|---|
type | kind of control |
name | key sent to the server |
value | current value |
placeholder | hint |
required | must be filled |
min | Common attribute |
max | Common attribute |
step | Common attribute |
minlength | Common attribute |
maxlength | Common attribute |
pattern | a rule |
disabled | Common attribute |
readonly | Common attribute |
autocomplete | Common attribute |
checked (checkbox | Common attribute |
radio) | Common attribute |
A field to type or choose.
The <textarea> element
The textarea element is a multi line text box for long messages.
When to use it: Set its size with rows and cols or CSS. The starting text goes between the tags.
Why we use it
It is a multi line text box.
How it works
The browser shows a resizable box. rows and cols set the starting size.
Where it is used
- Messages and comments
- Feedback forms
Common mistakes
Its value is the text between tags. Do not set a value attribute.
| Attribute | Meaning |
|---|---|
rows | Common attribute |
cols | Common attribute |
name | Common attribute |
placeholder | Common attribute |
required | Common attribute |
maxlength | Common attribute |
readonly | Common attribute |
disabled | Common attribute |
resize:none.A multi line text box.
The <button> element
The button element is a clickable button. It can hold text, icons and images.
When to use it: Use type="submit" to send a form, type="button" for actions in JavaScript and type="reset" to clear a form.
Why we use it
It is a clickable button that can submit a form or run JavaScript.
How it works
Its type is submit by default inside a form. Use type="button" to avoid sending the form.
Where it is used
- Submit buttons
- Open menus
- Toggle panels
Common mistakes
Do not use div or a for actions. A real button works with the keyboard.
| Attribute | Meaning |
|---|---|
type | submit, button, reset |
disabled | Common attribute |
name | Common attribute |
value | Common attribute |
form | the form it belongs to |
formaction | Common attribute |
type="button" if you do not want that.A button.
The <select> element
The select element is a drop down list of choices.
When to use it: Fill it with option elements. Group them with optgroup.
Why we use it
It is a dropdown list for choosing one or more options.
How it works
The browser shows a list of option elements. The multiple attribute allows several choices.
Where it is used
- Country and city pickers
- Sort order lists
Common mistakes
For fewer than 5 choices radios may be easier to use.
| Attribute | Meaning |
|---|---|
name | Common attribute |
multiple | allow more than one |
size | rows shown |
required | Common attribute |
disabled | Common attribute |
A drop down.
The <option> element
The option element is one choice inside a select or datalist.
When to use it: The text between the tags is shown, and value is sent.
Why we use it
It is one choice in a select or datalist.
How it works
The value attribute is sent to the server and the text is what the user sees.
Where it is used
- Each dropdown item
Common mistakes
Add a first option with an empty value as a prompt such as "Choose".
| Attribute | Meaning |
|---|---|
value | Common attribute |
selected | Common attribute |
disabled | Common attribute |
label | Common attribute |
One choice in select or datalist.
The <optgroup> element
The optgroup element groups related options under a heading in a select.
When to use it: Use label to name the group.
Why we use it
It groups related options with a label.
How it works
The label attribute is shown as a bold heading inside the list.
Where it is used
- Cities grouped by country
Common mistakes
The label cannot be selected itself.
| Attribute | Meaning |
|---|---|
label | group name |
disabled | Common attribute |
A named group of options.
The <datalist> element
The datalist element gives a list of suggestions for an input. The visitor can still type something else.
When to use it: Connect it with the input's list attribute and the datalist's id.
Why we use it
It gives suggestions for an input while still allowing free text.
How it works
An input points to it with the list attribute. The browser shows matching suggestions.
Where it is used
- Search boxes with suggestions
- Country input with hints
Common mistakes
It is only a hint. The user can still type something else.
| Attribute | Meaning |
|---|---|
id | Common attribute |
Suggestions for an input.
The <fieldset> element
The fieldset element groups related controls in a form and draws a box around them.
When to use it: Use it with a legend for sets of radio buttons or sections of a long form.
Why we use it
It groups related controls in a form.
How it works
The browser draws a box. Screen readers announce the legend with each control.
Where it is used
- Radio button groups
- Address sections
Common mistakes
Always add a legend.
| Attribute | Meaning |
|---|---|
disabled | disables every control inside |
name | Common attribute |
Groups form controls.
The <legend> element
The legend element is the caption of a fieldset.
When to use it: Place it as the first child of fieldset.
Why we use it
It is the caption of a fieldset.
How it works
It must be the first child of fieldset.
Where it is used
- The title of a group of options
Common mistakes
Use it only inside fieldset.
The title of a fieldset.
The <output> element
The output element shows the result of a calculation or a user action.
When to use it: Use it with a form or JavaScript. for lists the controls that feed the result.
Why we use it
It shows the result of a calculation or user action.
How it works
The for attribute lists the ids of the inputs that feed the result. Screen readers announce changes.
Where it is used
- Calculators
- Range slider values
Common mistakes
Update it with JavaScript.
| Attribute | Meaning |
|---|---|
for | ids of the inputs |
form | Common attribute |
name | Common attribute |
The result of a calculation.
The <progress> element
The progress element shows how far a task has come, such as an upload.
When to use it: Use value and max. Without a value it shows a busy animation.
Why we use it
It shows how far a task has progressed.
How it works
The value and max attributes draw a bar. Without a value it shows an unknown loading state.
Where it is used
- Uploads and downloads
- Steps in a form
Common mistakes
For a measurement that is not progress use meter.
| Attribute | Meaning |
|---|---|
value | current |
max | total |
Progress of a task.
The <meter> element
The meter element shows a measurement within a known range, such as disk space or a score. It is not for progress.
When to use it: Set value, min and max. low, high and optimum colour the bar.
Why we use it
It shows a value within a known range, like a gauge.
How it works
The min, max, low, high and optimum attributes colour it.
Where it is used
- Disk space used
- Password strength
- Ratings out of 5
Common mistakes
Not for tasks that move forward. Use progress.
| Attribute | Meaning |
|---|---|
value | Common attribute |
min | Common attribute |
max | Common attribute |
low | Common attribute |
high | Common attribute |
optimum | Common attribute |
A measurement within a range.
Quick quiz
1. <tfoot> is the
Summary rows go here.
2. <datalist> gives
Used with input list.
3. <output> shows
It displays results.