UNIT 9 • STAGE 4 OF 7
Build an HTML form and style it with CSS :focus states
A form is how a web page collects information from a visitor. Every time you fill out a contact form, submit a comment, or sign up for something online, you are using an HTML form.
In this stage you will build a community recipe submission form. Visitors can share their name, their community, a traditional recipe, and how they feel about the food sovereignty movement.
Try clicking into one of the fields below. Notice the green border that appears when a field is active:
That green highlight when the input is active is the CSS :focus state. You are about to build exactly that.
<form>, the container for all form fields. <label>, names a field and links to it via the for attribute. <input type="text">, a single-line text field. <input type="email">, a text field that validates email format. <input type="radio">, a circular option where only one can be selected. <textarea>, a multi-line text area. <button type="submit">, a button that submits the form.
Find </style> in your editor and add this CSS just above it:
background: #e8f0fe on #community-form, a soft blue-indigo. The section colors now move through the page: green hero, light green pillars, warm cream recipes, blue form. Each section has its own identity while the page reads as a whole.display: block on .form-group label, by default, labels are inline elements and sit beside their input field. Setting display: block stacks the label above the field, which is much easier to read and fill in.width: 100% and box-sizing: border-box on inputs, makes every field stretch to fill the form container. Without box-sizing: border-box, the padding would add to the width and fields would overflow.border: 1px solid #d1d5db, a light gray border on the input. It outlines the field without drawing too much attention before the user clicks.:focus, a CSS pseudo-class that applies styles when a user clicks into an element. This is how the browser knows which field is currently active.outline: none on :focus, removes the browser's default blue focus ring (which looks different in every browser) so your custom styles take over completely.border-color: #2d4a1e and box-shadow: 0 0 0 3px rgba(45, 74, 30, 0.15) on :focus, the green border and the soft green outer glow together make the active field clearly visible. The box-shadow uses the same green as the hero, keeping the palette consistent.resize: vertical on textarea, allows users to drag the textarea taller but not wider. Preventing horizontal resize keeps the form from breaking its layout.Find the closing </section> of your recipes section and add this HTML right after it:
The form is built and styled. Right now the submit button reloads the page when clicked, which is the default browser behavior. In Stage 5 you will write your first JavaScript to intercept that click and show a thank-you message instead.