CSS Form and Input Styling
CSS Form and Input Styling
Form styling should improve scanning and input, not hide labels or prevent keyboard users from completing fields.
What you’ll learn
- Style labels and controls as one form system.
- Use :focus-visible and :invalid states sensibly.
- Avoid replacing native input behavior with decorative markup.
Understand the concept
Associate each <label for="id"> with its control. Use consistent spacing and box-sizing:border-box for full-width fields. :focus-visible can enhance keyboard focus while :invalid reflects native validation state.
Do not depend on placeholder text as the only label. Validation requires appropriate HTML constraints and often application logic; CSS only displays states.
What the browser evaluates
A label provides an accessible name. Form CSS controls layout and feedback while the browser retains input, validation, and keyboard behavior.
Simple contact fields
Use a stacked label and input.
HTML
<form class="mini-form" action="#"><label for="demo-email-one">Email address</label><input id="demo-email-one" type="email" placeholder="you@example.com" required><button type="button">Continue</button></form>CSS
.mini-form { display: grid; gap: 10px; max-width: 340px; font-family: Arial, sans-serif; } .mini-form label { font-weight: 700; } .mini-form input { width: 100%; box-sizing: border-box; padding: 11px; border: 1px solid #94a3b8; border-radius: 7px; } .mini-form button { width: max-content; border: 0; padding: 11px 15px; background: #1d4ed8; color: white; border-radius: 7px; }Rendered output
Observe: Each field is labeled, and the input fills the form width.
Add states and hierarchy
Introduce a second field and a clear focus ring.
HTML
<form class="mini-form" action="#"><label for="demo-name-two">Full name</label><input id="demo-name-two" type="text" placeholder="Asha Rao"><label for="demo-email-two">Email address</label><input id="demo-email-two" type="email" placeholder="you@example.com" required><button type="button">Create account</button></form>CSS
.mini-form { display: grid; gap: 9px; max-width: 350px; padding: 18px; border-radius: 10px; background: #f1f5f9; } .mini-form label { font-weight: 600; } .mini-form input { width: 100%; box-sizing: border-box; padding: 12px; border: 1px solid #94a3b8; border-radius: 7px; background: white; } .mini-form input:focus-visible { outline: 3px solid #60a5fa; outline-offset: 1px; } .mini-form button { margin-top: 6px; padding: 12px; border: 0; background: #0f766e; color: white; font-weight: 700; border-radius: 7px; cursor: pointer; }Rendered output
Observe: The form has consistent field spacing, labels, and keyboard feedback.
Explain the result
A label provides an accessible name. Form CSS controls layout and feedback while the browser retains input, validation, and keyboard behavior.
Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.
Common mistakes to avoid
A placeholder disappears as a user types, so it should not replace a label.
A visually red border does not explain how to correct an invalid field.
Try it yourself
Tab between controls and add a small helper text under the email field using an ID and aria-describedby.
Before changing the code, predict what will change. Then verify by checking the rendered result.
Check your understanding
What you should remember
Use real form controls with readable labels, focus and validation messaging.
Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.
Reaching the recap and completing the quizzes are separate milestones.