Component Library

← Back to gallery

Two-step sign-up in HTML, CSS and vanilla JS

A copy-paste Two-step sign-up in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Authentication · Tags: sign-up, steps, registration, account

Live preview

Source (copy-ready)

<section class="dcl-sign-up-steps" data-dcl-scheme="light" aria-label="Two-step sign-up example">
<form>
  <div data-row><span data-chip data-step>Step 1 of 2</span><h2>Create your profile</h2></div>
  <div data-stack data-first><label>Email address<input type="email" autocomplete="email" required></label></div>
  <div data-stack data-second hidden><label>Display name<input type="text" autocomplete="name" maxlength="80" required disabled></label><div><button type="button" data-back>Back to email</button></div></div>
  <div><button type="submit" data-primary>Continue</button></div><p role="status">Enter sample details only.</p>
</form>
<script>
(() => {
  const root = document.currentScript.closest(".dcl-sign-up-steps");
  const preference = matchMedia("(prefers-color-scheme: dark)");
  const setTheme = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
  preference.addEventListener("change", setTheme);
  setTheme();
  const find = (selector) => root.querySelector(selector);
  function onSubmit(form, action) {
    const submit = form.querySelector('[type="submit"]');
    // A sandboxed preview blocks form submission, but still dispatches the submit button's click.
    submit.addEventListener("click", (event) => {
      event.preventDefault();
      if (form.reportValidity()) action();
    });
    form.addEventListener("submit", (event) => {
      event.preventDefault();
      action();
    });
  }
  const email = find('input[type="email"]');
  const name = find('input[type="text"]');
  const next = find('button[type="submit"]');
  const result = find('[role="status"]');
  function showProfile(profile) {
    find("[data-first]").hidden = profile;
    find("[data-second]").hidden = !profile;
    email.disabled = profile;
    name.disabled = !profile;
    find("[data-step]").textContent = profile ? "Step 2 of 2" : "Step 1 of 2";
    next.textContent = profile ? "Preview completion" : "Continue";
    result.textContent = "";
    (profile ? name : email).focus();
  }
  onSubmit(find("form"), () => {
    if (!email.disabled) showProfile(true);
    else result.textContent = "Both steps complete in this demo. No account was created.";
  });
  find("[data-back]").addEventListener("click", () => showProfile(false));
})();
</script>
</section>
<style>
.dcl-sign-up-steps, .dcl-sign-up-steps * { box-sizing: border-box; }
.dcl-sign-up-steps { font: 1rem/1.5 system-ui, sans-serif; max-inline-size: 34rem; padding: clamp(0.75rem, 3vw, 1.25rem); border: 1px solid; border-radius: 0.75rem; overflow-wrap: anywhere; }
.dcl-sign-up-steps :where(h2, h3, p, ol, ul, dl, dd) { margin: 0; }
.dcl-sign-up-steps h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-sign-up-steps h3 { font-size: 1rem; }
.dcl-sign-up-steps p + p { margin-block-start: 0.5rem; }
.dcl-sign-up-steps [data-stack], .dcl-sign-up-steps form { display: grid; gap: 0.75rem; }
.dcl-sign-up-steps [data-row] { display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; align-items: center; }
.dcl-sign-up-steps [data-split] { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); gap: 0.75rem 1.25rem; align-items: start; }
.dcl-sign-up-steps [data-spread] { justify-content: space-between; }
.dcl-sign-up-steps [hidden] { display: none !important; }
.dcl-sign-up-steps [data-well] { padding: 0.75rem; border-radius: 0.375rem; }
.dcl-sign-up-steps [data-chip] { display: inline-block; width: fit-content; padding: 0.125rem 0.55rem; border-radius: 99rem; font-size: 0.875rem; font-weight: 700; }
.dcl-sign-up-steps :where(button, summary, input, select) { font: inherit; }
.dcl-sign-up-steps :where(button, input, select) { min-block-size: 44px; min-inline-size: 0; max-inline-size: 100%; }
.dcl-sign-up-steps :where(button, input, select) { border: 1px solid; border-radius: 0.375rem; padding: 0.45rem 0.7rem; }
.dcl-sign-up-steps :where(button, summary) { cursor: pointer; font-weight: 650; }
.dcl-sign-up-steps :where(button, summary):active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-sign-up-steps summary:hover { text-decoration: underline; }
.dcl-sign-up-steps :where(button, input, select):disabled, .dcl-sign-up-steps [aria-disabled=true] { cursor: not-allowed; border-style: dashed; }
.dcl-sign-up-steps label { display: grid; gap: 0.25rem; }
.dcl-sign-up-steps :where(input, select) { inline-size: 100%; }
.dcl-sign-up-steps label[data-choice] { display: flex; gap: 0.5rem; align-items: center; min-block-size: 44px; }
.dcl-sign-up-steps label[data-choice] input { inline-size: 1.25rem; block-size: 1.25rem; min-block-size: 0; flex-shrink: 0; }
.dcl-sign-up-steps fieldset { min-inline-size: 0; margin: 0; border-radius: 0.375rem; padding: 0.5rem 0.75rem; }
.dcl-sign-up-steps :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-sign-up-steps { color-scheme: light; border-color: #697f84; }
.dcl-sign-up-steps { color: #11292f; background-color: #ffffff; }
.dcl-sign-up-steps [data-well], .dcl-sign-up-steps [data-chip] { color: #11292f; background-color: #e4edef; }
.dcl-sign-up-steps button, .dcl-sign-up-steps input, .dcl-sign-up-steps select { color: #11292f; background-color: #e4edef; }
.dcl-sign-up-steps button[data-primary] { color: #ffffff; background-color: #00535a; }
.dcl-sign-up-steps button:hover { color: #ffffff; background-color: #003c42; }
.dcl-sign-up-steps button[disabled], .dcl-sign-up-steps button[aria-disabled=true] { color: #405a60; background-color: #e4edef; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] { color-scheme: dark; border-color: #839fa5; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] [data-well], .dcl-sign-up-steps[data-dcl-scheme="dark"] [data-chip] { color: #eff8f9; background-color: #1c3c44; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] button, .dcl-sign-up-steps[data-dcl-scheme="dark"] input, .dcl-sign-up-steps[data-dcl-scheme="dark"] select { color: #eff8f9; background-color: #1c3c44; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] button[data-primary] { color: #11292f; background-color: #8fe1da; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #b2f2ec; }
.dcl-sign-up-steps[data-dcl-scheme="dark"] button[disabled], .dcl-sign-up-steps[data-dcl-scheme="dark"] button[aria-disabled=true] { color: #b0c9ce; background-color: #1c3c44; }
</style>

When to use

Split creation into email and profile steps when the second step has its own purpose. Preserve the email when going back.

Avoid

Do not send or claim to verify an email just because the person advanced to step two.

Accessibility

The step is visible. Advancing and going back focus the relevant field, and hidden fields do not block validation. Text contrast is at least 6.1:1 in light and 6.7:1 in dark.

Was this useful? Send feedback.