</>HTML & CSS

HTML & CSS: Forms – code examples

Contents

Form Validation with :invalid

Forms#validation#invalid#required#formCompare in all languages
Use case: Show users immediately which inputs are invalid, without 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="Enter email">
  <p class="hinweis">Please enter a valid email address.</p>
  <input type="text" required minlength="3" pattern="[A-Za-z]+" placeholder="Name (letters only, min. 3)">
  <p class="hinweis">At least 3 letters, no digits.</p>
  <button>Submit</button>
</form>

HTML forms validate themselves (required, type, pattern, minlength). The pseudo-classes :valid/:invalid make this visible; :not(:placeholder-shown) prevents empty fields from being red right away. Show an error message via the sibling selector +.

Form Layout with Grid

Forms#form#grid#label
Use case: Tidy contact or registration forms that also work on mobile.
<!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>First name <input name="v" autocomplete="given-name" required></label>
  <label>Last name <input name="n" autocomplete="family-name" required></label></div>
  <label>Email <input type="email" name="m" autocomplete="email" required></label>
  <label>Topic <select><option>Question</option><option>Feedback</option></select></label>
  <label>Message <textarea rows="3"></textarea></label>
  <button>Send</button>
</form>

Labels wrap the fields (clickable, accessible), Grid provides spacing and a two-column layout that automatically becomes single-column on narrow screens. autocomplete enables browser autofill.

Toggle Switch with CSS Only

Forms#checkbox#toggle#switch
Use case: Present settings (dark mode, notifications) as a switch instead of a checkbox.
<!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> Notifications</label>

The real checkbox stays in the DOM (invisible) and keeps its keyboard and screen reader behavior; the sibling selector input:checked + .spur styles the switch. role="switch" provides the right role.