BeginnerLesson 3 of 15
Forms & Input Fields – HTML & CSS
Input types, labels, select fields and built-in validation.
You collect user input with <form>. Every field needs a <label> linked via for to the field's id – this way you can click the label and screen readers read it aloud. The name attribute determines the key of the submitted data. The type attribute of <input> selects the kind of field (keyboard on mobile, validation, control element).
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
form{display:grid;gap:10px;max-width:340px}
label{display:grid;gap:4px;font-weight:600}
input,select,textarea{padding:6px;font:inherit}
fieldset{border:1px solid #bbb}
</style>
<form onsubmit="event.preventDefault();out.textContent='Sent: '+new FormData(this).get('name')+' ('+new FormData(this).get('level')+')'">
<label>Name <input type="text" name="name" value="Ada" required></label>
<label>Email <input type="email" name="mail" placeholder="ada@example.com"></label>
<label>Age <input type="number" name="alter" min="1" max="120" value="36"></label>
<label>Date <input type="date" name="datum"></label>
<label>Level
<select name="level"><option>Beginner</option><option>Pro</option></select>
</label>
<fieldset><legend>Newsletter?</legend>
<label><input type="radio" name="nl" value="ja" checked> Yes</label>
<label><input type="radio" name="nl" value="nein"> No</label>
</fieldset>
<label><input type="checkbox" name="agb"> I accept the terms</label>
<label>Message <textarea name="text" rows="3"></textarea></label>
<button type="submit">Submit</button>
<output id="out"></output>
</form>Built-in validation without JavaScript: required (mandatory field), minlength/maxlength, min/max, pattern (regular expression) and the type itself (email, url). With the pseudo-classes :valid and :invalid you color fields accordingly. The browser blocks submission and shows messages.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
input{padding:6px;border:2px solid #bbb;border-radius:4px}
input:invalid:not(:placeholder-shown){border-color:#d33;background:#fee}
input:valid:not(:placeholder-shown){border-color:#2a2}
</style>
<form onsubmit="event.preventDefault();this.querySelector('output').textContent='All inputs valid!'">
<p><label>Username (min. 3 characters)<br>
<input name="u" required minlength="3" placeholder="ada"></label></p>
<p><label>ZIP code (5 digits)<br>
<input name="plz" required pattern="[0-9]{5}" placeholder="10115" inputmode="numeric"></label></p>
<p><label>Website<br><input type="url" name="w" placeholder="https://example.com"></label></p>
<button>Check & submit</button> <output></output>
</form>WarningValidation in the browser never replaces checking on the server – users can bypass it. And:
placeholder is no substitute for a <label> (it disappears while typing and has low contrast).TipUse fitting types:
type="tel", type="email" and inputmode="numeric" open the right keyboard on a smartphone. autocomplete="email", "name", "postal-code" … lets browsers fill in fields automatically – a big gain in convenience.