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.

A table is built from rows and cellscaptionthead(tr > th)tbody(tr > td)tfoottable
A table is built from rows and cells

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.

AttributeMeaning
border (old)draw borders
widthCommon attribute
Tip:Style tables with CSS: border-collapse, padding and border.
Example: <table>Try it Yourself

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.

<caption> is a table part. It goes inside: table. It can contain: text.<caption ...>content(text)</caption>
<caption> is a table part. It goes inside: table. It can contain: text.

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.

Example: <caption>Try it Yourself

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.

<thead> is a table part. It goes inside: table. It can contain: tr.<thead ...>content(tr)</thead>
<thead> is a table part. It goes inside: table. It can contain: tr.

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.

Example: <thead>Try it Yourself

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.

<tbody> is a table part. It goes inside: table. It can contain: tr.<tbody ...>content(tr)</tbody>
<tbody> is a table part. It goes inside: table. It can contain: tr.

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.

Example: <tbody>Try it Yourself

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).

<tfoot> is a table part. It goes inside: table. It can contain: tr.<tfoot ...>content(tr)</tfoot>
<tfoot> is a table part. It goes inside: table. It can contain: tr.

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.

Example: <tfoot>Try it Yourself

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.

A row holds cellsthtdtdtr
A row holds 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.

Example: <tr>Try it Yourself

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.

<th> is a table cell. It goes inside: tr. It can contain: text, inline, block.<th ...>content(text, inline, block)</th>
<th> is a table cell. It goes inside: tr. It can contain: text, inline, block.

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.

AttributeMeaning
scopecol or row
colspan, rowspanjoin cells
headersCommon attribute
Tip:Use th for headings, not for normal data.
Example: <th>Try it Yourself

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.

<td> is a table cell. It goes inside: tr. It can contain: text, inline, block.<td ...>content(text, inline, block)</td>
<td> is a table cell. It goes inside: tr. It can contain: text, inline, block.

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.

AttributeMeaning
colspanjoin columns
rowspanjoin rows
headersCommon attribute
Example: <td>Try it Yourself

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.

<colgroup> is a table part. It goes inside: table. It can contain: col.<colgroup ...>content(col)</colgroup>
<colgroup> is a table part. It goes inside: table. It can contain: col.

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.

AttributeMeaning
spanhow many columns it covers
Example: <colgroup>Try it Yourself

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.

<col> is a void (no end tag). It goes inside: colgroup. It can contain: nothing.<col ...>no contentno end tag
<col> is a void (no end tag). It goes inside: colgroup. It can contain: nothing.

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.

AttributeMeaning
spanhow many columns it covers
Example: <col>Try it Yourself

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.

How a form worksUser fillsinputsClicks thesubmit buttonBrowser sendsname=value pairsServer or scriptreceives them
How a form works

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.

AttributeMeaning
actionwhere to send the data
methodget or post
enctypemultipart/form-data for files
targetCommon attribute
autocompleteon or off
novalidateskip browser checks
Tip:Search forms usually use get; forms with private data use post.
Example: <form>Try it Yourself

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.

Two ways to connect a labelfor + id<label for="e"><input id="e">Wrapping<label>Email<input></label>
Two ways to connect a 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.

AttributeMeaning
forthe id of the control
Tip:Labels make forms usable for screen readers and easier to tap on phones.
Example: <label>Try it Yourself

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.

<input> is a void inline (control). It goes inside: form, div, label.... It can contain: nothing.<input ...>no contentno end tag
<input> is a void inline (control). It goes inside: form, div, label.... It can contain: nothing.

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.

AttributeMeaning
typekind of control
namekey sent to the server
valuecurrent value
placeholderhint
requiredmust be filled
minCommon attribute
maxCommon attribute
stepCommon attribute
minlengthCommon attribute
maxlengthCommon attribute
patterna rule
disabledCommon attribute
readonlyCommon attribute
autocompleteCommon attribute
checked (checkboxCommon attribute
radio)Common attribute
Tip:The browser can check many types by itself: email, url, number, date.
Example: <input>Try it Yourself

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.

<textarea> is a inline (control). It goes inside: form, div.... It can contain: plain text.<textarea ...>content(plain text)</textarea>
<textarea> is a inline (control). It goes inside: form, div.... It can contain: plain text.

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.

AttributeMeaning
rowsCommon attribute
colsCommon attribute
nameCommon attribute
placeholderCommon attribute
requiredCommon attribute
maxlengthCommon attribute
readonlyCommon attribute
disabledCommon attribute
Tip:Remove the resize handle with CSS resize:none.
Example: <textarea>Try it Yourself

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.

Button typestype="submit"Sends the formDefault inside a formtype="button"Does nothing aloneUsed with JavaScript
Button types

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.

AttributeMeaning
typesubmit, button, reset
disabledCommon attribute
nameCommon attribute
valueCommon attribute
formthe form it belongs to
formactionCommon attribute
Tip:A button inside a form submits it by default. Add type="button" if you do not want that.
Example: <button>Try it Yourself

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.

A dropdownoptionoptionoptgroupoptionselect
A dropdown

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.

AttributeMeaning
nameCommon attribute
multipleallow more than one
sizerows shown
requiredCommon attribute
disabledCommon attribute
Example: <select>Try it Yourself

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.

<option> is a control part. It goes inside: select, optgroup, datalist. It can contain: text.<option ...>content(text)</option>
<option> is a control part. It goes inside: select, optgroup, datalist. It can contain: text.

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".

AttributeMeaning
valueCommon attribute
selectedCommon attribute
disabledCommon attribute
labelCommon attribute
Example: <option>Try it Yourself

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.

<optgroup> is a control part. It goes inside: select. It can contain: option.<optgroup ...>content(option)</optgroup>
<optgroup> is a control part. It goes inside: select. It can contain: option.

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.

AttributeMeaning
labelgroup name
disabledCommon attribute
Example: <optgroup>Try it Yourself

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.

<datalist> is a hidden control. It goes inside: body, form.... It can contain: option.<datalist ...>content(option)</datalist>
<datalist> is a hidden control. It goes inside: body, form.... It can contain: option.

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.

AttributeMeaning
idCommon attribute
Example: <datalist>Try it Yourself

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.

A fieldset with a legendlegendlabel + inputlabel + inputfieldset
A fieldset with a legend

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.

AttributeMeaning
disableddisables every control inside
nameCommon attribute
Example: <fieldset>Try it Yourself

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.

<legend> is a block. It goes inside: fieldset. It can contain: text.<legend ...>content(text)</legend>
<legend> is a block. It goes inside: fieldset. It can contain: text.

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.

Example: <legend>Try it Yourself

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.

<output> is a inline. It goes inside: form, div.... It can contain: text.<output ...>content(text)</output>
<output> is a inline. It goes inside: form, div.... It can contain: text.

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.

AttributeMeaning
forids of the inputs
formCommon attribute
nameCommon attribute
Example: <output>Try it Yourself

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.

<progress> is a inline (control). It goes inside: text, div.... It can contain: fallback text.<progress ...>content(fallback text)</progress>
<progress> is a inline (control). It goes inside: text, div.... It can contain: fallback text.

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.

AttributeMeaning
valuecurrent
maxtotal
Example: <progress>Try it Yourself

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.

<meter> is a inline (control). It goes inside: text, div.... It can contain: fallback text.<meter ...>content(fallback text)</meter>
<meter> is a inline (control). It goes inside: text, div.... It can contain: fallback text.

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.

AttributeMeaning
valueCommon attribute
minCommon attribute
maxCommon attribute
lowCommon attribute
highCommon attribute
optimumCommon attribute
Example: <meter>Try it Yourself

A measurement within a range.

Quick quiz

1. <tfoot> is the

2. <datalist> gives

3. <output> shows

My notes
Ask a question about this lesson