</>HTML & CSS

HTML & CSS: Formulare – Code-Beispiele

Inhalt

Formularvalidierung mit :invalid

Formulare#validierung#invalid#required#formularIn allen Sprachen vergleichen
Anwendungsfall: Nutzern sofort zeigen, welche Eingaben ungültig sind, ohne JavaScript.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
input { padding: 8px; border: 2px solid #bbb; border-radius: 6px; margin-bottom: 4px; }
input:not(:placeholder-shown):invalid { border-color: #c62828; background: #fdecea; }
input:not(:placeholder-shown):valid { border-color: #2e7d32; }
.hinweis { display: none; color: #c62828; font-size: .85rem; margin: 0 0 8px; }
input:not(:placeholder-shown):invalid + .hinweis { display: block; }
</style>
<form onsubmit="event.preventDefault()">
  <input type="email" required placeholder="E-Mail eingeben">
  <p class="hinweis">Bitte eine gültige E-Mail-Adresse angeben.</p>
  <input type="text" required minlength="3" pattern="[A-Za-z]+" placeholder="Name (nur Buchstaben, min. 3)">
  <p class="hinweis">Mindestens 3 Buchstaben, keine Ziffern.</p>
  <button>Absenden</button>
</form>

HTML-Formulare validieren selbst (required, type, pattern, minlength). Die Pseudoklassen :valid/:invalid machen das sichtbar; :not(:placeholder-shown) verhindert, dass leere Felder sofort rot sind. Fehlermeldung anzeigen per Geschwister-Selektor +.

Formular-Layout mit Grid

Formulare#formular#grid#label
Anwendungsfall: Aufgeräumte Kontakt- oder Registrierungsformulare, die auch mobil funktionieren.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
form { display: grid; gap: 12px; max-width: 420px; }
.reihe { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
label { display: grid; gap: 4px; font-weight: 600; font-size: .9rem; }
input, textarea, select { font: inherit; padding: 8px 10px; border: 1.5px solid #9ca3af; border-radius: 8px; }
input:focus, textarea:focus, select:focus { outline: 3px solid #93c5fd; border-color: #2563eb; }
button { justify-self: start; font: inherit; font-weight: 700; padding: 10px 20px; border: 0; border-radius: 8px; background: #2563eb; color: #fff; }
</style>
<form onsubmit="event.preventDefault()">
  <div class="reihe"><label>Vorname <input name="v" autocomplete="given-name" required></label>
  <label>Nachname <input name="n" autocomplete="family-name" required></label></div>
  <label>E-Mail <input type="email" name="m" autocomplete="email" required></label>
  <label>Thema <select><option>Frage</option><option>Feedback</option></select></label>
  <label>Nachricht <textarea rows="3"></textarea></label>
  <button>Senden</button>
</form>

Labels umschließen die Felder (klickbar, zugänglich), Grid liefert Abstände und ein Zwei-Spalten-Layout, das auf schmalen Bildschirmen automatisch einspaltig wird. autocomplete ermöglicht Browser-Autofill.

Toggle-Schalter nur mit CSS

Formulare#checkbox#toggle#switch
Anwendungsfall: Einstellungen (Dark Mode, Benachrichtigungen) als Schalter statt Checkbox darstellen.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.schalter { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.schalter input { position: absolute; opacity: 0; }
.spur { width: 46px; height: 26px; background: #9ca3af; border-radius: 99px; position: relative; transition: background .2s; }
.spur::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; background: #fff; border-radius: 50%; transition: transform .2s; }
input:checked + .spur { background: #16a34a; }
input:checked + .spur::after { transform: translateX(20px); }
input:focus-visible + .spur { outline: 3px solid #f59e0b; outline-offset: 2px; }
</style>
<label class="schalter"><input type="checkbox" role="switch"><span class="spur"></span> Benachrichtigungen</label>

Die echte Checkbox bleibt (unsichtbar) im DOM und behält Tastatur- und Screenreader-Verhalten; der Nachbar-Selektor input:checked + .spur gestaltet den Schalter. role="switch" gibt die richtige Rolle.