Lesson 21 of 55
HTMLStyling Forms – Layout with Grid & Flexbox
9 min
Course progress38%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 21 of 55
HTML9 min
Keep these ideas in mind as you work through the lesson.
<form class='flex-form'>
<label for='name'>Name</label><input id='name' type='text' />
<label for='email'>Email</label><input id='email' type='email' />
<button type='submit'>Send</button>
</form>
.flex-form {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.flex-form label { flex-basis: 100%; }
.flex-form input { flex: 1 1 200px; }
Flexbox keeps label‑input pairs on the same line on larger screens but wraps on mobile.
<form class='grid-form'>
<label for='msg'>Message</label>
<textarea id='msg'></textarea>
<button type='submit'>Submit</button>
</form>
.grid-form {
display: grid;
grid-template-columns: 1fr 2fr; /* label column, control column */
gap: 0.5rem;
}
.grid-form button { grid-column: 2; }
Grid makes it easy to line up labels and controls in a consistent column layout.
<label> elements associated with inputs via for/id.<fieldset> and <legend> to group related controls (e.g., address fields).role='alert') so screen readers announce them immediately.for/id)? ✅display:flex or display:grid) chosen appropriately? ✅gap used for consistent spacing instead of margins? ✅<fieldset>/<legend> used for logical grouping? ✅Tip: Combine Flexbox for simple rows and Grid for more complex two‑column forms to keep the markup lean.