Form With Layouts
Six ways to arrange fields, then four forms assembled from them. Every demo is a real
<form> inside .form, which caps the column at 36rem — so pressing
Enter submits it, and in these docs that reloads the page with a query string.
Layout patterns
Farga styles a form from its elements, so a layout is a choice of wrapper around the field, not a class on the control.
Stacked — the default
One field per row, label above the control. Use it unless there is a reason not to: it survives long labels, long values, translation, and a 320px screen.
Example code
<form class="form">
<p>
<label for="stack-name">Full name</label>
<input id="stack-name" name="name" type="text" autocomplete="name">
</p>
<p>
<label for="stack-email">Email</label>
<input id="stack-email" name="email" type="email" autocomplete="email" aria-describedby="stack-email-hint">
<small id="stack-email-hint" class="field-hint">Only used for the receipt.</small>
</p>
</form>
Two-up row
For two short values that belong together and are read together. Both columns are the same width, so keep long values out of it — a URL beside a quantity makes the URL unreadable.
Example code
<form class="form">
<div class="grid-2">
<p>
<label for="two-first">First name</label>
<input id="two-first" name="first" type="text" autocomplete="given-name">
</p>
<p>
<label for="two-last">Last name</label>
<input id="two-last" name="last" type="text" autocomplete="family-name">
</p>
</div>
</form>
Label-left row
Compact, for settings screens and short labels. The label column is a fixed minimum width, and a hint would land in the control column where it loses its owner — so this pattern is for fields that need no explanation.
Example code
<form class="form">
<div class="field-row">
<label for="row-name">Name</label>
<input id="row-name" name="name" type="text" autocomplete="name">
</div>
<div class="field-row">
<label for="row-timezone">Timezone</label>
<select id="row-timezone" name="timezone">
<option>Asia/Bangkok</option>
<option>Europe/London</option>
<option>America/New_York</option>
</select>
</div>
</form>
Row of fields
For two or three controls of similar weight that are submitted together — a filter bar, a subscribe bar. Items divide the row equally, so do not use it when one field needs noticeably more room than the others.
Example code
<form class="form-wide">
<div class="flex-stretch gap">
<p>
<label for="inline-email">Email</label>
<input id="inline-email" name="email" type="email" autocomplete="email">
</p>
<p>
<label for="inline-topic">Topic</label>
<select id="inline-topic" name="topic">
<option>Product news</option>
<option>Release notes</option>
<option>Both</option>
</select>
</p>
<p>
<button type="submit" class="btn">Subscribe</button>
</p>
</div>
</form>
Sectioned form
When a form stops being one topic, cut it into <fieldset> groups named by a <legend>. The gap between groups has to be visibly wider than the gap between fields, or the sections read as one long column.
Example code
<form class="form">
<fieldset>
<legend>Account</legend>
<p>
<label for="sec-email">Email</label>
<input id="sec-email" name="email" type="email" autocomplete="email" required>
</p>
<p>
<label for="sec-password">Password</label>
<input id="sec-password" name="password" type="password" autocomplete="new-password" required aria-describedby="sec-password-hint">
<small id="sec-password-hint" class="field-hint">At least 12 characters.</small>
</p>
</fieldset>
<fieldset>
<legend>Email preferences</legend>
<label class="switch">
<input type="checkbox" name="weekly-digest" role="switch" checked />
Weekly digest
</label>
<label class="switch">
<input type="checkbox" name="product-news" role="switch" />
Product news
</label>
</fieldset>
</form>
Actions
Primary action last, so it is the one the eye lands on when reading the form downwards. Secondary actions sit to its left as an outline. There is no Reset: clearing a filled form throws away the user's work, which needs a confirmation step rather than a button beside Save.
Example code
<form class="form">
<p>
<label for="actions-name">Display name</label>
<input id="actions-name" name="display-name" type="text" value="jane_doe" autocomplete="nickname">
</p>
<footer class="actions">
<button type="button" class="btn-outline">Cancel</button>
<button type="submit">Save changes</button>
</footer>
</form>
Field anatomy and errors
A field is a label, a control, and — when it needs one — a message that belongs to the control through
aria-describedby. Required controls are marked by the required attribute alone: the
asterisk comes from CSS, so it never carries the meaning on its own.
Summary when validation fails
Name the problem at the top of the form, link each item straight to its control, and let the alert announce itself with role="alert". The messages below the fields repeat the detail for anyone who arrives there directly.
Example code
<form class="form">
<div class="alert danger" role="alert">
<div>
<h3>2 fields need attention</h3>
<ul>
<li><a href="#err-email">Email address is not complete</a></li>
<li><a href="#err-postcode">Postcode is required</a></li>
</ul>
</div>
</div>
<p>
<label for="err-name">Display name</label>
<input id="err-name" name="display-name" type="text" value="jane_doe" autocomplete="nickname">
<small class="field-hint">Shown on your profile.</small>
</p>
<p>
<label for="err-email">Email address</label>
<input id="err-email" name="email" type="email" value="jane@" aria-invalid="true" aria-describedby="err-email-message">
<small id="err-email-message" class="field-hint">Enter a complete address, like jane@example.com.</small>
</p>
<p>
<label for="err-postcode">Postcode</label>
<input id="err-postcode" name="postcode" type="text" aria-invalid="true" aria-describedby="err-postcode-message" autocomplete="postal-code">
<small id="err-postcode-message" class="field-hint">Postcode is required.</small>
</p>
<footer class="actions">
<button type="submit">Save changes</button>
</footer>
</form>
Required, hint and error together
The asterisk is decorative — required is what assistive tech reports — and it depends on the
label's shape, because CSS cannot follow a for attribute. The hint and the error differ by more
than colour: one explains, the other says what went wrong.
Example code
<form class="form">
<p>
<label>
Email
<input name="email" type="email" autocomplete="email" required aria-describedby="anatomy-email-hint">
</label>
<small id="anatomy-email-hint" class="field-hint">A wrapping label picks the marker up from <code>required</code>. We send a confirmation link.</small>
</p>
<p>
<label for="anatomy-phone" class="required">Phone</label>
<input id="anatomy-phone" name="phone" type="tel" autocomplete="tel" aria-describedby="anatomy-phone-hint">
<small id="anatomy-phone-hint" class="field-hint">A sibling label needs <code>class="required"</code>: there is no selector for the label whose target is required.</small>
</p>
</form>
Composed examples
Four forms built from the patterns above, with the copy and the attributes a real screen would carry.
Sign in
Two fields in a column narrow enough for them, the recovery link in the actions row so it sits on the same line as the button instead of floating under it, and the joining path kept available without competing with the primary action. autocomplete is what lets a password manager fill both fields.
Example code
<form class="form form-narrow">
<p>
<label for="signin-email">Email</label>
<input id="signin-email" name="email" type="email" autocomplete="email" required>
</p>
<p>
<label for="signin-password">Password</label>
<input id="signin-password" name="password" type="password" autocomplete="current-password" required>
</p>
<footer class="actions">
<a href="#">Forgot your password?</a>
<button type="submit">Sign in</button>
</footer>
<p>New to Farga? <a href="#">Create an account</a></p>
</form>
Contact
Stacked fields, a hint where the format matters, and the consent checkbox inside the same group as the rest of the message.
Example code
<form class="form">
<fieldset>
<legend>Contact us</legend>
<p>
<label for="contact-name">Full name</label>
<input id="contact-name" name="name" type="text" autocomplete="name" required>
</p>
<p>
<label for="contact-email">Email</label>
<input id="contact-email" name="email" type="email" autocomplete="email" required>
</p>
<p>
<label for="contact-message">Message</label>
<textarea id="contact-message" name="message" rows="5" required aria-describedby="contact-message-hint"></textarea>
<small id="contact-message-hint" class="field-hint">The order number helps us answer faster.</small>
</p>
<label>
<input type="checkbox" name="consent" value="yes" required />
You can email me about this request
</label>
</fieldset>
<footer class="actions">
<button type="submit">Send message</button>
</footer>
</form>
Create account
A two-up row for the name, sections for account and preferences, and new-password so the browser offers to generate one rather than filling an old one.
Example code
<form class="form">
<fieldset>
<legend>Your account</legend>
<div class="grid-2">
<p>
<label for="reg-first">First name</label>
<input id="reg-first" name="first-name" type="text" autocomplete="given-name" required>
</p>
<p>
<label for="reg-last">Last name</label>
<input id="reg-last" name="last-name" type="text" autocomplete="family-name" required>
</p>
</div>
<p>
<label for="reg-email">Email</label>
<input id="reg-email" name="email" type="email" autocomplete="email" required>
</p>
<p>
<label for="reg-password">Password</label>
<input id="reg-password" name="password" type="password" autocomplete="new-password" required aria-describedby="reg-password-hint">
<small id="reg-password-hint" class="field-hint">At least 12 characters.</small>
</p>
</fieldset>
<fieldset>
<legend>Preferences</legend>
<label class="switch">
<input type="checkbox" name="weekly-digest" role="switch" checked />
Weekly digest
</label>
<label>
<input type="checkbox" name="terms" value="agreed" required />
I agree to the terms of service
</label>
</fieldset>
<footer class="actions">
<button type="button" class="btn-outline">Cancel</button>
<button type="submit">Create account</button>
</footer>
</form>
Product search
Two filters in a two-up row, an actions row to run it, and clearing the filters as a tertiary link. A search bar is not one row of controls — the query carries the search, the category only narrows it, and stacking the labels keeps them readable at any width.
Example code
<form class="form-wide">
<div class="grid-2">
<p>
<label for="search-query">Search</label>
<input id="search-query" name="query" type="search" placeholder="Try “adaptive learning”">
</p>
<p>
<label for="search-category">Category</label>
<select id="search-category" name="category">
<option value="">All categories</option>
<option value="electronics">Electronics</option>
<option value="clothing">Clothing</option>
<option value="books">Books</option>
</select>
</p>
</div>
<footer class="actions">
<a href="#">Clear filters</a>
<button type="submit">Search products</button>
</footer>
</form>