HTML Forms With CSS: 5 Styled Forms to Copy

Updated 2026-10-28. 5 forms.

A good HTML form pairs every field with a visible label, groups related choices in a fieldset, ties each error message to its field with aria-describedby and styles states with :focus-visible and :user-invalid. The five forms below follow that pattern in plain HTML and CSS, with a React and Tailwind version of each.

Choose by the job the form does. A contact form needs few fields and a friendly tone, so the floating label version keeps it compact without hiding what each box is for. A signup form has rules to explain, so its version shows a message under each field the moment a rule is broken and not before the person has finished typing.

Longer forms such as bookings read faster in two columns on a wide screen, as long as related fields sit side by side and the layout drops to one column on a phone. The inline newsletter form fits in a footer or the end of an article. The feedback form uses a row of radio buttons styled as a rating scale, which keeps real form behaviour while looking like a modern control.

Each plain version works with no script for its basic job: required fields, email checks and date limits come from HTML attributes the browser already understands. The short scripts only add the nicer touches, like custom error text and the thank-you message, so the form still submits if the script fails to load.

Compare them

VariantBuilt withSize (gzipped)Running costBrowser support
Contact Form With Floating Labels in HTML and CSSCSS only2 KBA 150 ms label transition on focus; nothing runs while the form sits idle.All current browsers; :user-invalid in Chrome 119+, Safari 16.5+, Firefox 88+
Feedback Form With Radio Rating ScaleCSS only1.7 KBPure CSS selection states; the script only handles the thank-you line.All current browsers
Inline Newsletter Form in HTML and CSSCSS only1.5 KBA single input and button; the script runs only when the form is sent.All current browsers; :has() styling in browsers from 2023 on
Signup Form With Inline Validation MessagesCSS and a small script2.3 KBThree small regular expressions run on each keystroke in the password field.All current browsers
Two Column Booking Form With Select and DateCSS only2 KBNative selects and a date input; one change handler recomputes a price.All current browsers

Contact Form With Floating Labels in HTML and CSS

A contact section for a joinery workshop: business details on the left, a white form card on the right. Each label sits inside its box and slides up to the top edge when the field gets focus or text, so the form stays compact without losing its labels. Errors appear only after someone leaves a field, and on phones the two columns stack with the details first.

<section class="cff">
  <div class="cff-grid">
    <div>
      <p class="cff-kicker">Saltmarsh Joinery</p>
      <h2 class="cff-title">Tell us about your project</h2>
      <p class="cff-lead">Kitchens, built-in shelving and staircases across the county. We reply within two working days.</p>
      <dl class="cff-dl">
        <div><dt>Workshop:</dt> <dd>Unit 4, Quay Lane</dd></div>
        <div><dt>Phone:</dt> <dd>01632 960 418</dd></div>
      </dl>
    </div>
    <form class="cff-form" action="#" method="post">
      <div class="cff-field">
        <input id="cff-name" name="name" type="text" autocomplete="name" required placeholder="Your name" aria-describedby="cff-name-err">
        <label for="cff-name">Your name</label>
        <p id="cff-name-err" class="cff-err">Please tell us your name.</p>
      </div>
      <div class="cff-field">
        <input id="cff-email" name="email" type="email" autocomplete="email" required placeholder="Email address" aria-describedby="cff-email-err">
        <label for="cff-email">Email address</label>
        <p id="cff-email-err" class="cff-err">Enter an email like [email protected].</p>
      </div>
      <div class="cff-field">
        <textarea id="cff-message" name="message" rows="4" required minlength="10" placeholder="What would you like built?" aria-describedby="cff-message-err"></textarea>
        <label for="cff-message">What would you like built?</label>
        <p id="cff-message-err" class="cff-err">A sentence or two helps us quote, at least 10 characters.</p>
      </div>
      <button type="submit" class="cff-btn">Send message</button>
      <p class="cff-done" role="status"></p>
    </form>
  </div>
</section>
<style>
  .cff { --cff-accent: #115e59; --cff-bad: #e11d48; background: #eef3f1; padding: 72px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
  .cff-grid { display: grid; gap: 40px; max-width: 56rem; margin: 0 auto; }
  @media (min-width: 768px) { .cff-grid { grid-template-columns: 1fr 1.3fr; } }
  .cff-kicker { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .2em; text-transform: uppercase; color: var(--cff-accent); }
  .cff-title { margin: 12px 0 0; font-size: 30px; font-weight: 600; letter-spacing: -.02em; line-height: 1.2; }
  .cff-lead { margin: 12px 0 0; color: #334155; line-height: 1.6; }
  .cff-dl { margin: 24px 0 0; font-size: 14px; color: #334155; }
  .cff-dl div { margin-top: 8px; }
  .cff-dl dt { display: inline; font-weight: 500; color: #0f172a; }
  .cff-dl dd { display: inline; margin: 0; }
  .cff-form { display: grid; gap: 20px; padding: 28px; background: #fff; border-radius: 16px; box-shadow: 0 0 0 1px #e2e8f0, 0 1px 2px rgba(0,0,0,.05); }
  .cff-field { position: relative; }
  .cff-field input, .cff-field textarea { box-sizing: border-box; display: block; width: 100%; padding: 24px 16px 8px; border: 1px solid #cbd5e1; border-radius: 12px; background: #fff; color: #0f172a; font: inherit; font-size: 15px; resize: vertical; }
  .cff-field ::placeholder { color: transparent; }
  .cff-field label { position: absolute; left: 17px; top: 8px; font-size: 12px; font-weight: 500; color: #475569; pointer-events: none; transition: top .15s, font-size .15s, color .15s; }
  /* the label sits inside the box while the field is empty and unfocused */
  .cff-field :placeholder-shown:not(:focus) + label { top: 16px; font-size: 15px; }
  .cff-field input:focus, .cff-field textarea:focus { outline: 0; border-color: var(--cff-accent); box-shadow: 0 0 0 4px #ccfbf1; }
  .cff-field :focus + label { color: var(--cff-accent); }
  .cff-field :focus-visible { outline: 0; }
  /* errors appear only after someone has used the field */
  .cff-err { display: none; margin: 6px 0 0; font-size: 14px; color: #be123c; }
  .cff-field :user-invalid { border-color: var(--cff-bad); }
  .cff-field :user-invalid:focus { box-shadow: 0 0 0 4px #ffe4e6; }
  .cff-field :user-invalid ~ .cff-err, .cff-field [aria-invalid="true"] ~ .cff-err { display: block; }
  .cff-field [aria-invalid="true"] { border-color: var(--cff-bad); }
  .cff-btn { height: 48px; border: 0; border-radius: 12px; background: var(--cff-accent); color: #fff; font: 600 15px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .cff-btn:hover { background: #134e4a; }
  .cff-btn:focus-visible { outline: 2px solid var(--cff-accent); outline-offset: 3px; }
  .cff-done { margin: 0; font-weight: 500; color: #115e59; }
  .cff-done:empty { display: none; }
  @media (prefers-reduced-motion: reduce) { .cff-field label { transition: none; } }
</style>
<script>
  (function () {
    var form = document.querySelector(".cff-form");
    if (!form) return;
    /* the form works without this script; it adds aria-invalid and a thank-you line */
    var fields = form.querySelectorAll("input, textarea");
    fields.forEach(function (f) {
      f.addEventListener("blur", function () { f.setAttribute("aria-invalid", String(!f.validity.valid)); });
      f.addEventListener("input", function () { if (f.validity.valid) f.setAttribute("aria-invalid", "false"); });
    });
    form.setAttribute("novalidate", "");
    form.addEventListener("submit", function (e) {
      e.preventDefault();
      fields.forEach(function (f) { f.setAttribute("aria-invalid", String(!f.validity.valid)); });
      var first = form.querySelector(":invalid");
      if (first) { first.focus(); return; }
      form.querySelector(".cff-done").textContent = "Thanks, your message is in. Mara will get back to you by Thursday.";
      form.reset();
    });
  })();
</script>

With reduced motion: Labels jump to their new place instead of sliding.

Running cost: A 150 ms label transition on focus; nothing runs while the form sits idle.

  • Edit the `FIELDS` array to add a phone field; give it `type: "tel"` and `auto: "tel"` so phones show the number pad.
  • Change the accent by replacing `teal-800` and `teal-100` in React, or `--cff-accent` in the CSS version.
  • Set the form `action` to your form handler address and keep `method="post"` in the plain version.
  • Adjust `minLength={10}` on the message box, or remove it if a short note is fine.

Full page for Contact Form With Floating Labels in HTML and CSS

Feedback Form With Radio Rating Scale

A post-order survey for a meal kit company. Five numbered tiles from Poor to Superb sit in a grey tray and the chosen one fills rose; under it, pill checkboxes ask what stood out, and a note box takes a comment. Every tile is a real radio button, so arrow keys move the choice and the value posts with the form.

<section class="ffr">
  <form class="ffr-card" action="#" method="post">
    <p class="ffr-kicker">Order #4471, Pellmell Kitchen</p>
    <h2 class="ffr-title">How was your dinner box?</h2>
    <fieldset class="ffr-set">
      <legend>Overall</legend>
      <div class="ffr-scale">
        <label><input type="radio" name="rating" value="1"><span><b>1</b>Poor</span></label>
        <label><input type="radio" name="rating" value="2"><span><b>2</b>Fair</span></label>
        <label><input type="radio" name="rating" value="3"><span><b>3</b>Good</span></label>
        <label><input type="radio" name="rating" value="4" checked><span><b>4</b>Great</span></label>
        <label><input type="radio" name="rating" value="5"><span><b>5</b>Superb</span></label>
      </div>
    </fieldset>
    <fieldset class="ffr-set">
      <legend>What stood out? <small>Pick any</small></legend>
      <div class="ffr-chips">
        <label><input type="checkbox" name="topics" value="delivery"><span>Delivery time</span></label>
        <label><input type="checkbox" name="topics" value="packaging"><span>Packaging</span></label>
        <label><input type="checkbox" name="topics" value="taste" checked><span>Taste</span></label>
        <label><input type="checkbox" name="topics" value="price"><span>Price</span></label>
      </div>
    </fieldset>
    <label for="ffr-note" class="ffr-label">Anything else?</label>
    <textarea id="ffr-note" name="note" rows="3">The miso aubergine was the best thing we've had all week.</textarea>
    <button type="submit" class="ffr-btn">Send feedback</button>
    <p class="ffr-done" role="status"></p>
  </form>
</section>
<style>
  .ffr { --ffr-accent: #be123c; background: #fff1f2; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #1c1917; }
  .ffr-card { max-width: 32rem; margin: 0 auto; padding: 28px; background: #fff; border-radius: 24px; box-shadow: 0 0 0 1px #ffe4e6; }
  .ffr-kicker { margin: 0; font-size: 14px; font-weight: 500; color: #9f1239; }
  .ffr-title { margin: 4px 0 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
  .ffr-set { margin: 24px 0 0; padding: 0; border: 0; min-width: 0; }
  .ffr-set legend, .ffr-label { padding: 0; font-size: 14px; font-weight: 500; color: #292524; }
  .ffr-set small { font-size: 14px; font-weight: 400; color: #57534e; }
  /* real radio buttons, visually hidden; the span after each one is the visible tile */
  .ffr input[type=radio], .ffr input[type=checkbox] { position: absolute; opacity: 0; width: 1px; height: 1px; }
  .ffr-scale { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 12px; padding: 6px; border-radius: 16px; background: #f5f5f4; }
  .ffr-scale label { position: relative; cursor: pointer; }
  .ffr-scale span { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 64px; border-radius: 12px; font-size: 11px; font-weight: 500; color: #44403c; transition: background-color .15s, color .15s; }
  .ffr-scale b { font-size: 18px; font-variant-numeric: tabular-nums; }
  .ffr-scale span:hover { background: #fff; }
  .ffr-scale :checked + span { background: var(--ffr-accent); color: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.1); }
  .ffr :focus-visible + span { outline: 2px solid var(--ffr-accent); outline-offset: 2px; }
  .ffr-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
  .ffr-chips label { position: relative; cursor: pointer; }
  .ffr-chips span { display: flex; align-items: center; height: 40px; padding: 0 16px; border: 1px solid #d6d3d1; border-radius: 999px; font-size: 14px; font-weight: 500; color: #44403c; }
  .ffr-chips :checked + span { border-color: var(--ffr-accent); background: #fff1f2; color: #881337; }
  .ffr-label { display: block; margin-top: 24px; }
  .ffr textarea { box-sizing: border-box; display: block; width: 100%; margin-top: 6px; padding: 10px 12px; border: 1px solid #d6d3d1; border-radius: 12px; font: inherit; font-size: 14px; color: #1c1917; resize: vertical; }
  .ffr textarea:focus { outline: 0; border-color: var(--ffr-accent); box-shadow: 0 0 0 3px #fecdd3; }
  .ffr-btn { width: 100%; height: 44px; margin-top: 24px; border: 0; border-radius: 999px; background: #1c1917; color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .ffr-btn:focus-visible { outline: 2px solid var(--ffr-accent); outline-offset: 2px; }
  .ffr-done { margin: 12px 0 0; text-align: center; font-size: 14px; font-weight: 500; color: #9f1239; }
  .ffr-done:empty { display: none; }
  @media (min-width: 640px) { .ffr-scale span { font-size: 12px; } }
  @media (prefers-reduced-motion: reduce) { .ffr-scale span { transition: none; } }
</style>
<script>
  (function () {
    var form = document.querySelector(".ffr-card");
    if (!form) return;
    /* the form posts without this; it only swaps in a thank-you line */
    form.addEventListener("submit", function (e) {
      e.preventDefault();
      form.querySelector(".ffr-done").textContent = "Thank you, Sam. Your note goes straight to the kitchen team.";
    });
  })();
</script>

With reduced motion: The selected tile changes colour at once, without the short fade.

Running cost: Pure CSS selection states; the script only handles the thank-you line.

  • Edit `SCALE` for a different number of steps or words; in the plain version add or remove label blocks and change `repeat(5, 1fr)`.
  • Swap the topic pills in `TOPICS` for questions that matter to your service.
  • Recolour the selected tile with `bg-rose-700`, or `--ffr-accent` in the CSS.

Inline Newsletter Form in HTML and CSS

A slim signup band for an outdoors publication on deep navy. The email field and a lime Subscribe button share one pill on wider screens and stack inside a rounded box on phones. A line underneath carries a privacy promise at rest, turns rose with a short fix when the address is wrong, and confirms the next send date once it is right.

<section class="inl">
  <div class="inl-wrap">
    <div class="inl-copy">
      <h2 class="inl-title">The Field Notes letter</h2>
      <p class="inl-lead">One email on the first Sunday of each month: new trail guides, kit we have stopped using, and a map worth printing.</p>
    </div>
    <form class="inl-form" action="#" method="post">
      <label for="inl-email" class="inl-sr">Email address</label>
      <div class="inl-pill">
        <input id="inl-email" name="email" type="email" autocomplete="email" required placeholder="[email protected]" aria-describedby="inl-msg">
        <button type="submit">Subscribe</button>
      </div>
      <p id="inl-msg" class="inl-msg" role="status">No tracking pixels. Leave with one click.</p>
    </form>
  </div>
</section>
<style>
  .inl { --inl-accent: #bef264; background: #10172a; padding: 56px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #fff; }
  .inl-wrap { display: flex; flex-direction: column; gap: 32px; max-width: 64rem; margin: 0 auto; }
  @media (min-width: 1024px) { .inl-wrap { flex-direction: row; align-items: center; justify-content: space-between; } .inl-form { max-width: 28rem; } }
  .inl-copy { max-width: 28rem; }
  .inl-title { margin: 0; font-size: 24px; font-weight: 600; letter-spacing: -.02em; }
  .inl-lead { margin: 8px 0 0; color: #cbd5e1; line-height: 1.6; }
  .inl-form { width: 100%; }
  .inl-pill { display: flex; flex-direction: column; gap: 8px; padding: 6px; border-radius: 16px; background: rgba(255,255,255,.05); box-shadow: inset 0 0 0 1px rgba(255,255,255,.15); }
  @media (min-width: 640px) { .inl-pill { flex-direction: row; border-radius: 999px; } }
  .inl-pill:focus-within { box-shadow: inset 0 0 0 2px var(--inl-accent); }
  .inl-pill:has(:user-invalid), .inl-form[data-bad] .inl-pill { box-shadow: inset 0 0 0 1px #fb7185; }
  .inl-pill input { flex: 1; min-width: 0; height: 44px; padding: 0 16px; border: 0; border-radius: 999px; background: transparent; color: #fff; font: inherit; font-size: 15px; }
  .inl-pill input::placeholder { color: #94a3b8; }
  .inl-pill input:focus { outline: 0; }
  .inl-pill button { height: 44px; padding: 0 24px; border: 0; border-radius: 999px; background: var(--inl-accent); color: #0f172a; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; flex-shrink: 0; }
  .inl-pill button:hover { background: #d9f99d; }
  .inl-pill button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
  .inl-msg { margin: 10px 0 0; padding: 0 16px; font-size: 14px; color: #94a3b8; }
  .inl-form[data-bad] .inl-msg { color: #fda4af; }
  .inl-form[data-done] .inl-msg { color: #d9f99d; }
  .inl-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
  (function () {
    var form = document.querySelector(".inl-form");
    if (!form) return;
    var input = form.querySelector("input"), msg = form.querySelector(".inl-msg"), btn = form.querySelector("button");
    form.setAttribute("novalidate", "");
    form.addEventListener("submit", function (e) {
      e.preventDefault();
      var ok = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(input.value);
      form.toggleAttribute("data-bad", !ok);
      form.toggleAttribute("data-done", ok);
      input.setAttribute("aria-invalid", !ok);
      msg.textContent = ok ? "Done. The next letter goes out on Sunday." : "Check the address, it needs an @ and a domain.";
      if (ok) btn.textContent = "Subscribed"; else input.focus();
    });
    input.addEventListener("input", function () { form.removeAttribute("data-bad"); input.removeAttribute("aria-invalid"); });
  })();
</script>

With reduced motion: Nothing moves; only the outline colour of the pill changes.

Running cost: A single input and button; the script runs only when the form is sent.

  • Change `--inl-accent` or the `lime-300` classes to your brand colour; keep dark text on light buttons.
  • Point the form `action` at your mailing list provider's subscribe address and match its field name for the email.
  • Replace the helper line with your own promise, for example how often you send.

Signup Form With Inline Validation Messages

An account signup card for a project tool, opened in the middle of filling it in: the email has lost its ending and shows a red border with a plain message, and the password checklist ticks off each rule in green as it is met. Errors only appear after a field is left, and the submit button sends focus to the first problem.

<section class="sfv">
  <form class="sfv-card" action="#" method="post" novalidate>
    <h2 class="sfv-title">Create your Ortolan account</h2>
    <p class="sfv-sub">Free for teams of up to five. No card needed.</p>
    <div class="sfv-f">
      <label for="sfv-email">Work email</label>
      <input id="sfv-email" name="email" type="email" autocomplete="email" required value="dev@ortolan" aria-describedby="sfv-email-msg">
      <p id="sfv-email-msg" class="sfv-msg"></p>
    </div>
    <div class="sfv-f">
      <label for="sfv-pass">Password</label>
      <input id="sfv-pass" name="password" type="password" autocomplete="new-password" required minlength="10" value="orchard7" aria-describedby="sfv-rules">
      <ul id="sfv-rules" class="sfv-rules" aria-label="Password rules">
        <li data-rule="len">At least 10 characters<span class="sfv-sr"></span></li>
        <li data-rule="num">Contains a number<span class="sfv-sr"></span></li>
        <li data-rule="case">Upper and lower case letters<span class="sfv-sr"></span></li>
      </ul>
    </div>
    <div class="sfv-f">
      <div class="sfv-check">
        <input id="sfv-agree" name="agree" type="checkbox" required aria-describedby="sfv-agree-msg">
        <label for="sfv-agree">I agree to the <a href="#">terms</a> and <a href="#">privacy notice</a>.</label>
      </div>
      <p id="sfv-agree-msg" class="sfv-msg"></p>
    </div>
    <button type="submit" class="sfv-btn">Create account</button>
    <p class="sfv-done" role="status"></p>
  </form>
</section>
<style>
  .sfv { --sfv-accent: #6d28d9; --sfv-bad: #f43f5e; background: #f5f3ff; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
  .sfv-card { max-width: 28rem; margin: 0 auto; padding: 28px; background: #fff; border-radius: 16px; box-shadow: 0 0 0 1px #ede9fe, 0 24px 48px -28px rgba(76,29,149,.45); }
  .sfv-title { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
  .sfv-sub { margin: 4px 0 0; font-size: 14px; color: #475569; }
  .sfv-f { margin-top: 20px; }
  .sfv-f > label { font-size: 14px; font-weight: 500; color: #1e293b; }
  .sfv-f input[type=email], .sfv-f input[type=password] { box-sizing: border-box; display: block; width: 100%; height: 44px; margin-top: 6px; padding: 0 12px; border: 1px solid #cbd5e1; border-radius: 8px; font: inherit; font-size: 14px; color: #0f172a; }
  .sfv-f input:focus { outline: 0; border-color: var(--sfv-accent); box-shadow: 0 0 0 3px #ddd6fe; }
  .sfv-f input:user-invalid, .sfv-f input[aria-invalid="true"] { border-color: var(--sfv-bad); }
  .sfv-f input:user-invalid:focus, .sfv-f input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px #fecdd3; }
  .sfv-msg { min-height: 20px; margin: 6px 0 0; font-size: 14px; color: #be123c; }
  .sfv-rules { margin: 10px 0 0; padding: 0; list-style: none; }
  .sfv-rules li { position: relative; margin-top: 6px; padding-left: 26px; font-size: 14px; color: #475569; }
  .sfv-rules li::before { content: ""; position: absolute; left: 0; top: 2px; width: 14px; height: 14px; border: 1px solid #94a3b8; border-radius: 50%; }
  .sfv-rules li[data-ok] { color: #047857; }
  .sfv-rules li[data-ok]::before { border-color: #059669; background: #059669 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.2l2.2 2.2 4.8-4.9' fill='none' stroke='white' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E") center / 10px no-repeat; }
  .sfv-check { display: flex; gap: 12px; align-items: flex-start; font-size: 14px; color: #334155; }
  .sfv-check input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--sfv-accent); }
  .sfv-check input:focus-visible { outline: 2px solid var(--sfv-accent); outline-offset: 2px; }
  .sfv-check a { color: #5b21b6; font-weight: 500; }
  .sfv-btn { width: 100%; height: 44px; margin-top: 8px; border: 0; border-radius: 8px; background: var(--sfv-accent); color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .sfv-btn:hover { background: #5b21b6; }
  .sfv-btn:focus-visible { outline: 2px solid var(--sfv-accent); outline-offset: 2px; }
  .sfv-done { min-height: 20px; margin: 12px 0 0; text-align: center; font-size: 14px; font-weight: 500; color: #047857; }
  .sfv-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
  (function () {
    var form = document.querySelector(".sfv-card");
    if (!form) return;
    var EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
    var RULES = { len: function (p) { return p.length >= 10; }, num: function (p) { return /\d/.test(p); }, "case": function (p) { return /[a-z]/.test(p) && /[A-Z]/.test(p); } };
    var email = form.querySelector("#sfv-email"), pass = form.querySelector("#sfv-pass"), agree = form.querySelector("#sfv-agree");
    var emailMsg = form.querySelector("#sfv-email-msg"), agreeMsg = form.querySelector("#sfv-agree-msg");

    function checkEmail() {
      var bad = !EMAIL.test(email.value);
      email.setAttribute("aria-invalid", bad);
      emailMsg.textContent = bad ? "That address is missing its ending, such as .com or .io." : "";
      return !bad;
    }
    function checkPass(mark) {
      var all = true;
      form.querySelectorAll("[data-rule]").forEach(function (li) {
        var ok = RULES[li.getAttribute("data-rule")](pass.value);
        li.toggleAttribute("data-ok", ok);
        li.querySelector(".sfv-sr").textContent = ok ? ", done" : ", not yet";
        if (!ok) all = false;
      });
      if (mark) pass.setAttribute("aria-invalid", !all);
      return all;
    }
    function checkAgree() {
      agree.setAttribute("aria-invalid", !agree.checked);
      agreeMsg.textContent = agree.checked ? "" : "Please accept the terms to continue.";
      return agree.checked;
    }
    email.addEventListener("blur", checkEmail);
    email.addEventListener("input", function () { if (email.getAttribute("aria-invalid") === "true") checkEmail(); });
    pass.addEventListener("input", function () { checkPass(pass.hasAttribute("aria-invalid")); });
    pass.addEventListener("blur", function () { checkPass(true); });
    agree.addEventListener("change", checkAgree);
    form.addEventListener("submit", function (e) {
      e.preventDefault();
      var ok = [checkEmail(), checkPass(true), checkAgree()];
      if (ok.indexOf(false) > -1) { form.querySelector("[aria-invalid=true]").focus(); return; }
      form.querySelector(".sfv-done").textContent = "Account created. Check your inbox to confirm.";
    });
    checkEmail(); checkPass(true);
  })();
</script>

With reduced motion: Nothing animates; rule ticks and red borders simply switch on.

Running cost: Three small regular expressions run on each keystroke in the password field.

  • Edit the `RULES` array to match your real password policy; each rule is a label and a test function.
  • Swap the violet accent through `violet-700` and `violet-200`, or `--sfv-accent` in the plain CSS.
  • Replace the email message with your own wording for missing at signs and missing domain endings.

Two Column Booking Form With Select and Date

A holiday cottage request form with a tinted header, a two column grid of native selects, a date picker, name and phone, and a full width notes box. A footer bar keeps a running line such as 3 nights from Fri 13 Nov / £495, so guests see the cost before sending. Below 640 pixels every field drops into one column.

<section class="tcb">
  <form class="tcb-card" action="#" method="post">
    <div class="tcb-head">
      <h2 class="tcb-title">Book a stay at Brackenfold Farm</h2>
      <p class="tcb-sub">Three stone cottages in the hills. Arrivals from 4pm, Monday and Friday changeovers.</p>
    </div>
    <div class="tcb-grid">
      <fieldset class="tcb-set">
        <legend class="tcb-sr">Your stay</legend>
        <div class="tcb-wide">
          <label for="tcb-cottage">Cottage</label>
          <select id="tcb-cottage" name="cottage">
            <option data-rate="120">The Byre, sleeps 2</option>
            <option data-rate="165" selected>Hayloft, sleeps 4</option>
            <option data-rate="240">Mill House, sleeps 6</option>
          </select>
        </div>
        <div>
          <label for="tcb-arrive">Arrival date</label>
          <input id="tcb-arrive" name="arrive" type="date" required min="2026-10-30" value="2026-11-13">
        </div>
        <div>
          <label for="tcb-nights">Nights</label>
          <select id="tcb-nights" name="nights">
            <option value="2">2 nights</option>
            <option value="3" selected>3 nights</option>
            <option value="4">4 nights</option>
            <option value="7">7 nights</option>
          </select>
        </div>
      </fieldset>
      <fieldset class="tcb-set">
        <legend class="tcb-sr">Lead guest</legend>
        <div>
          <label for="tcb-name">Full name</label>
          <input id="tcb-name" name="name" required autocomplete="name" value="Hannah Rowe">
        </div>
        <div>
          <label for="tcb-phone">Phone <span>(for arrival day)</span></label>
          <input id="tcb-phone" name="phone" type="tel" autocomplete="tel" placeholder="07700 900 123">
        </div>
        <div class="tcb-wide">
          <label for="tcb-notes">Anything we should know?</label>
          <textarea id="tcb-notes" name="notes" rows="3" placeholder="Dogs, cots, late arrival"></textarea>
        </div>
      </fieldset>
    </div>
    <div class="tcb-foot">
      <p class="tcb-sum" aria-live="polite"></p>
      <button type="submit" class="tcb-btn">Request booking</button>
    </div>
  </form>
</section>
<style>
  .tcb { --tcb-accent: #9a3412; background: #f5f5f4; padding: 64px 16px; font-family: ui-sans-serif, system-ui, sans-serif; color: #1c1917; }
  .tcb-card { max-width: 48rem; margin: 0 auto; overflow: hidden; background: #fff; border-radius: 16px; box-shadow: 0 0 0 1px #e7e5e4; }
  .tcb-head { padding: 20px 24px; border-bottom: 1px solid #e7e5e4; background: #fff7ed; }
  .tcb-title { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
  .tcb-sub { margin: 4px 0 0; font-size: 14px; color: #44403c; }
  /* one column on phones, two from 640px; wide fields span both */
  .tcb-grid, .tcb-set { display: grid; gap: 20px; }
  .tcb-grid { padding: 24px; }
  .tcb-set { margin: 0; padding: 0; border: 0; min-width: 0; }
  @media (min-width: 640px) {
    .tcb-grid, .tcb-set { grid-template-columns: 1fr 1fr; }
    .tcb-set { grid-column: 1 / -1; }
    .tcb-wide { grid-column: 1 / -1; }
    .tcb-grid { padding: 24px 32px; }
  }
  .tcb label { font-size: 14px; font-weight: 500; color: #292524; }
  .tcb label span { font-weight: 400; color: #57534e; }
  .tcb input, .tcb select, .tcb textarea { box-sizing: border-box; display: block; width: 100%; height: 44px; margin-top: 6px; padding: 0 12px; border: 1px solid #d6d3d1; border-radius: 6px; background: #fff; color: #1c1917; font: inherit; font-size: 14px; }
  .tcb textarea { height: auto; padding: 10px 12px; resize: vertical; }
  .tcb ::placeholder { color: #78716c; }
  .tcb input:focus, .tcb select:focus, .tcb textarea:focus { outline: 0; border-color: #c2410c; box-shadow: 0 0 0 3px #fed7aa; }
  .tcb input:user-invalid { border-color: #e11d48; }
  .tcb-foot { display: flex; flex-wrap: wrap; gap: 16px; align-items: center; justify-content: space-between; padding: 20px 24px; border-top: 1px solid #e7e5e4; background: #fafaf9; }
  .tcb-sum { margin: 0; font-size: 14px; color: #44403c; }
  .tcb-sum b { color: #1c1917; }
  .tcb-btn { height: 44px; padding: 0 24px; border: 0; border-radius: 6px; background: var(--tcb-accent); color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
  .tcb-btn:hover { background: #7c2d12; }
  .tcb-btn:focus-visible { outline: 2px solid var(--tcb-accent); outline-offset: 2px; }
  .tcb-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
</style>
<script>
  (function () {
    var form = document.querySelector(".tcb-card");
    if (!form) return;
    var cottage = form.querySelector("#tcb-cottage"), arrive = form.querySelector("#tcb-arrive"), nights = form.querySelector("#tcb-nights"), sum = form.querySelector(".tcb-sum");
    function update() {
      var rate = Number(cottage.selectedOptions[0].getAttribute("data-rate")), n = Number(nights.value);
      var d = arrive.value ? new Date(arrive.value + "T12:00:00").toLocaleDateString("en-GB", { weekday: "short", day: "numeric", month: "short" }) : "your arrival";
      while (sum.firstChild) sum.removeChild(sum.firstChild);
      var b = document.createElement("b");
      b.textContent = "£" + rate * n;
      sum.appendChild(document.createTextNode(n + " nights from " + d + " / "));
      sum.appendChild(b);
    }
    [cottage, arrive, nights].forEach(function (el) { el.addEventListener("change", update); });
    form.addEventListener("submit", function (e) {
      if (!form.checkValidity()) return;
      e.preventDefault();
      form.querySelector(".tcb-btn").textContent = "Request sent";
    });
    update();
  })();
</script>

With reduced motion: There is no animation; the price line updates in place as choices change.

Running cost: Native selects and a date input; one change handler recomputes a price.

  • Change the cottages and nightly prices in `COTTAGES` and `RATE`, or the `data-rate` values on each option.
  • Set the `min` on the date input to today on the server so past dates cannot be chosen.
  • Make any field full width by adding `sm:col-span-2` in React or the `tcb-wide` class in the plain version.

Performance notes

Styled forms are one of the lightest things on a page. The CSS here is a few hundred lines at most across all five, uses no web fonts and no images, and the floating label effect is a transform and a colour change on focus. None of it runs while the person is not typing.

The :user-invalid selector lets the browser decide when to show an error, which saves a script that listens to every blur and input. Where a browser does not support it yet, the fields still validate on submit through the built-in checks. Keep error text in the markup and toggle it, rather than creating new nodes, so assistive technology finds a stable element to announce.

Questions

How do I style an HTML form with CSS?

Give inputs a consistent height, padding, border and radius, set font: inherit so they match the page, and add clear :focus-visible and invalid states. Lay fields out with flex or grid and a fixed gap rather than margins on each element.

What is the difference between :invalid and :user-invalid?

The :invalid selector matches a required field as soon as the page loads, so empty forms show errors before anyone types. The :user-invalid selector only matches after the person has interacted with the field, which is the behaviour most designers want.

How do I connect an error message to its input?

Give the message an id and add that id to the input's aria-describedby attribute. Screen readers then read the message along with the label when the field gets focus. Set aria-invalid to true on the input while the error shows.

Do floating labels hurt accessibility?

They are fine when the label is a real label element that stays visible after it moves up, and when the text never shrinks below a readable size. They become a problem when the placeholder is used as the only label.

How do I make a form two columns on desktop and one on mobile?

Use CSS grid with one column by default and a media query that switches to two columns from about 640 pixels wide. Let a field span both columns with grid-column: 1 / -1 when it needs the full width, like a message box.

Also in the gallery: HTML Form Examples with Every Input Type, Copyable as HTML + CSS or Tailwind, Photoreal Login Page HTML CSS with Show Password, Remember Me and a Cut Glass Door Knob in Window Light.

More collections: OTP Input Field in HTML and CSS: 5 Verification Code Inputs, React Tag Input: 5 Chip Inputs With Real Keyboard Support, Tailwind Progress Bars: 5 Progress Bars in React and HTML. All forms. Build your site free to have a full site built around them.

Product

Who It's For

Features

Resources

Company

View Sitemap