HTML and CSS · Lesson 3 of 12

Tables and forms

Tables show data in rows and columns; forms collect input. Both are easy to get working and easy to get subtly wrong.

Tables

A <table> has rows (<tr>) of cells: <th> for headings and <td> for data. Group the heading row in <thead> and the rest in <tbody>. Use tables for data only, never for page layout: that's what flexbox and grid are for.

Try itEdit the code: the page updates as you type

Forms, inputs and labels

A <form> wraps inputs. Every input needs a <label>, tied to it with for and id (or by wrapping the input): clicking the label then focuses the input, and screen readers read it. The input's type gives the right keyboard on phones: email, tel, number, date.

Built-in validation: required, minlength, min/max, pattern. The browser checks them before submitting; your server must check again.

Try itEdit the code: the page updates as you type

Your turn

Make a sign-up form with name, email, password (at least 8 characters) and a checkbox "I agree", all required, and a submit button.

ExerciseEdit the code: the page updates as you type
Show a solution
<form>
  <p><label for="n">Name</label><br><input id="n" required></p>
  <p><label for="e">Email</label><br><input id="e" type="email" required></p>
  <p><label for="p">Password</label><br><input id="p" type="password" minlength="8" required></p>
  <p><label><input type="checkbox" required> I agree</label></p>
  <button>Sign up</button>
</form>

In the interview

Why should every input have a label?

So screen readers can say what the field is, so clicking the label focuses the input (a bigger target on phones), and so the form still makes sense without placeholders, which disappear as you type.

Is HTML form validation enough?

No. It helps users, but anyone can send a request without your form. The server must validate every input again.

GET or POST for a form?

GET puts the fields in the URL: fine for searches and filters that can be bookmarked. POST sends them in the body: for anything that changes data or holds private information, like passwords.

Your progress stays on this device.