Forms let visitors type something in: a search, a login, a message. A form is a <form> element with fields inside it:
<form>
<label for="name">Your name</label>
<input type="text" id="name" name="name">
<button type="submit">Send</button>
</form>
Inputs
<input> is an empty element. Its type attribute decides what kind of field it is:
| Type | What you get |
|---|---|
text | A one-line text box |
email | A text box that checks for an email address |
number | A box for numbers, with small up and down arrows |
password | A text box that hides what you type |
checkbox | A box to tick |
The name attribute is the label the data is sent under. Add required to make the browser refuse an empty field.
Labels
Every field needs a <label> that says what to type. Connect the two by giving the input an id and putting the same value in the label's for attribute. Then clicking the label puts the cursor in the field, and screen readers read the label aloud.
Buttons
<button type="submit"> sends the form. On a real site, the form's action attribute says which address receives the data. This preview does not send anything anywhere.
Your Task
- Inside the form, add a label and a text input for a name. Connect them with
forandid. - Add a label and an input of type
emailfor an email address, connected the same way. - Make the email input
required. - Add a submit button with the text Sign Up.
- The form has a text input with a connected label
- The form has an input of type
emailwith a connected label - The email input is
required - The form has a submit button that says Sign Up
- The two inputs have different
idvalues
Lesson complete!
Next Lesson: Project: A Profile Page