Tailwind CSS Two-Factor Authentication Examples
A growing collection of two-factor authentication examples for Tailwind CSS. Copy and paste the HTML into your project and adjust it with utility classes.
<div class="flex min-h-svh flex-col items-center justify-center p-6 md:p-10">
<div class="flex w-full max-w-sm flex-col gap-6">
<a
href="#"
class="self-center text-foreground [&>svg]:size-7"
aria-label="Starting Point"
><svg
xmlns="http://www.w3.org/2000/svg"
viewBox="4 4 16 16"
fill="currentColor"
>
<path
d="M6.5 16.5 16.5 6.5"
fill="none"
stroke="currentColor"
stroke-width="4"
stroke-linecap="round"
></path>
<circle cx="17" cy="17" r="2.5"></circle></svg
></a>
<div class="grid gap-6" data-two-factor="">
<div data-two-factor-code="">
<div class="grid justify-items-center gap-2 text-center">
<h1 class="text-xl font-medium tracking-tight">
Authentication code
</h1>
<p class="text-sm text-balance text-muted-foreground">
Enter the authentication code provided by your authenticator
application.
</p>
</div>
</div>
<div data-two-factor-recovery="" hidden="">
<div class="grid justify-items-center gap-2 text-center">
<h1 class="text-xl font-medium tracking-tight">Recovery code</h1>
<p class="text-sm text-balance text-muted-foreground">
Please confirm access to your account by entering one of your
emergency recovery codes.
</p>
</div>
</div>
<form
id="simple-two-factor-authentication-page"
class="field-group"
action="/two-factor-challenge"
method="post"
>
<div data-two-factor-code="">
<div class="field items-center [&>.label]:sr-only">
<label
class="label"
for="simple-two-factor-authentication-page-code"
>Authentication code</label
><label class="input-otp input-otp-separated"
><span></span><span></span><span></span><span></span><span></span
><span></span
><input
maxlength="6"
spellcheck="false"
inputmode="numeric"
autocomplete="one-time-code"
pattern="[0-9]{6}"
required=""
autofocus=""
id="simple-two-factor-authentication-page-code"
name="code"
/></label>
</div>
</div>
<div data-two-factor-recovery="" hidden="">
<div class="field [&_label]:sr-only">
<label
class="label"
for="simple-two-factor-authentication-page-recovery_code"
>Recovery code</label
><input
class="input"
id="simple-two-factor-authentication-page-recovery_code"
autocomplete="one-time-code"
placeholder="abcdef-123456"
name="recovery_code"
/>
</div>
</div>
<button type="submit" class="btn">Continue</button>
</form>
<p
class="text-center text-sm text-muted-foreground [&_a]:font-medium [&_a]:text-foreground [&_a]:underline-offset-4 [&_a:hover]:underline"
>
or you can
<button
type="button"
class="font-medium text-foreground underline-offset-4 hover:underline"
data-two-factor-toggle=""
>
<span data-two-factor-code="">login using a recovery code</span
><span data-two-factor-recovery="" hidden=""
>login using an authentication code</span
>
</button>
</p>
<script>
{
const root = document.currentScript.closest("[data-two-factor]");
root.addEventListener("click", (e) => {
if (!e.target.closest("[data-two-factor-toggle]")) return;
for (const el of root.querySelectorAll(
"[data-two-factor-code], [data-two-factor-recovery]",
))
el.hidden = !el.hidden;
const input = root.querySelector(
"[data-two-factor-code]:not([hidden]) input, [data-two-factor-recovery]:not([hidden]) input",
);
for (const field of root.querySelectorAll("input"))
field.required = field === input;
input.value = "";
input.focus();
});
}
</script>
</div>
</div>
</div><div
class="flex min-h-svh flex-col items-center justify-center bg-muted p-6 md:p-10"
>
<div class="flex w-full max-w-md flex-col gap-6">
<a
href="#"
class="self-center text-foreground [&>svg]:size-7"
aria-label="Starting Point"
><svg
xmlns="http://www.w3.org/2000/svg"
viewBox="4 4 16 16"
fill="currentColor"
>
<path
d="M6.5 16.5 16.5 6.5"
fill="none"
stroke="currentColor"
stroke-width="4"
stroke-linecap="round"
></path>
<circle cx="17" cy="17" r="2.5"></circle></svg
></a>
<div class="card gap-0 py-0">
<div class="card-content grid gap-6 px-6 py-8 sm:px-10">
<div class="grid gap-6" data-two-factor="">
<div data-two-factor-code="">
<div class="grid justify-items-center gap-2 text-center">
<h1 class="text-xl font-medium tracking-tight">
Authentication code
</h1>
<p class="text-sm text-balance text-muted-foreground">
Enter the authentication code provided by your authenticator
application.
</p>
</div>
</div>
<div data-two-factor-recovery="" hidden="">
<div class="grid justify-items-center gap-2 text-center">
<h1 class="text-xl font-medium tracking-tight">Recovery code</h1>
<p class="text-sm text-balance text-muted-foreground">
Please confirm access to your account by entering one of your
emergency recovery codes.
</p>
</div>
</div>
<form
id="card-two-factor-authentication-page"
class="field-group"
action="/two-factor-challenge"
method="post"
>
<div data-two-factor-code="">
<div class="field items-center [&>.label]:sr-only">
<label
class="label"
for="card-two-factor-authentication-page-code"
>Authentication code</label
><label class="input-otp input-otp-separated"
><span></span><span></span><span></span><span></span
><span></span><span></span
><input
maxlength="6"
spellcheck="false"
inputmode="numeric"
autocomplete="one-time-code"
pattern="[0-9]{6}"
required=""
autofocus=""
id="card-two-factor-authentication-page-code"
name="code"
/></label>
</div>
</div>
<div data-two-factor-recovery="" hidden="">
<div class="field [&_label]:sr-only">
<label
class="label"
for="card-two-factor-authentication-page-recovery_code"
>Recovery code</label
><input
class="input"
id="card-two-factor-authentication-page-recovery_code"
autocomplete="one-time-code"
placeholder="abcdef-123456"
name="recovery_code"
/>
</div>
</div>
<button type="submit" class="btn">Continue</button>
</form>
<p
class="text-center text-sm text-muted-foreground [&_a]:font-medium [&_a]:text-foreground [&_a]:underline-offset-4 [&_a:hover]:underline"
>
or you can
<button
type="button"
class="font-medium text-foreground underline-offset-4 hover:underline"
data-two-factor-toggle=""
>
<span data-two-factor-code="">login using a recovery code</span
><span data-two-factor-recovery="" hidden=""
>login using an authentication code</span
>
</button>
</p>
<script>
{
const root = document.currentScript.closest("[data-two-factor]");
root.addEventListener("click", (e) => {
if (!e.target.closest("[data-two-factor-toggle]")) return;
for (const el of root.querySelectorAll(
"[data-two-factor-code], [data-two-factor-recovery]",
))
el.hidden = !el.hidden;
const input = root.querySelector(
"[data-two-factor-code]:not([hidden]) input, [data-two-factor-recovery]:not([hidden]) input",
);
for (const field of root.querySelectorAll("input"))
field.required = field === input;
input.value = "";
input.focus();
});
}
</script>
</div>
</div>
</div>
</div>
</div><div class="grid min-h-svh lg:grid-cols-2">
<div
class="relative hidden flex-col bg-neutral-900 p-10 text-white lg:flex dark:border-e"
>
<img
src="https://images.unsplash.com/photo-1513569771920-c9e1d31714af?w=1400&q=80&auto=format&fit=crop"
alt=""
class="absolute inset-0 size-full object-cover"
/>
<div
class="absolute inset-0 bg-linear-to-b from-black/30 via-transparent to-black/60"
></div>
<a href="#" class="brand relative text-lg text-white"
><span class="brand-logo brand-logo-lg bg-white text-neutral-900"
><svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
>
<path
d="M6.5 16.5 16.5 6.5"
fill="none"
stroke="currentColor"
stroke-width="4"
stroke-linecap="round"
></path>
<circle cx="17" cy="17" r="2.5"></circle></svg></span
><span class="brand-name text-lg">Starting Point</span></a
>
<figure class="relative mt-auto grid gap-2">
<blockquote class="text-base font-medium">
“Simplicity is the ultimate sophistication.”
</blockquote>
<figcaption class="text-base text-white/70">Leonardo da Vinci</figcaption>
</figure>
</div>
<div class="flex items-center justify-center p-6 md:p-10">
<div class="flex w-full max-w-sm flex-col gap-6">
<a
href="#"
class="self-center text-foreground [&>svg]:size-7 lg:hidden"
aria-label="Starting Point"
><svg
xmlns="http://www.w3.org/2000/svg"
viewBox="4 4 16 16"
fill="currentColor"
>
<path
d="M6.5 16.5 16.5 6.5"
fill="none"
stroke="currentColor"
stroke-width="4"
stroke-linecap="round"
></path>
<circle cx="17" cy="17" r="2.5"></circle></svg
></a>
<div class="grid gap-6" data-two-factor="">
<div data-two-factor-code="">
<div class="grid justify-items-center gap-2 text-center">
<h1 class="text-xl font-medium tracking-tight">
Authentication code
</h1>
<p class="text-sm text-balance text-muted-foreground">
Enter the authentication code provided by your authenticator
application.
</p>
</div>
</div>
<div data-two-factor-recovery="" hidden="">
<div class="grid justify-items-center gap-2 text-center">
<h1 class="text-xl font-medium tracking-tight">Recovery code</h1>
<p class="text-sm text-balance text-muted-foreground">
Please confirm access to your account by entering one of your
emergency recovery codes.
</p>
</div>
</div>
<form
id="split-two-factor-authentication-page"
class="field-group"
action="/two-factor-challenge"
method="post"
>
<div data-two-factor-code="">
<div class="field items-center [&>.label]:sr-only">
<label
class="label"
for="split-two-factor-authentication-page-code"
>Authentication code</label
><label class="input-otp input-otp-separated"
><span></span><span></span><span></span><span></span
><span></span><span></span
><input
maxlength="6"
spellcheck="false"
inputmode="numeric"
autocomplete="one-time-code"
pattern="[0-9]{6}"
required=""
autofocus=""
id="split-two-factor-authentication-page-code"
name="code"
/></label>
</div>
</div>
<div data-two-factor-recovery="" hidden="">
<div class="field [&_label]:sr-only">
<label
class="label"
for="split-two-factor-authentication-page-recovery_code"
>Recovery code</label
><input
class="input"
id="split-two-factor-authentication-page-recovery_code"
autocomplete="one-time-code"
placeholder="abcdef-123456"
name="recovery_code"
/>
</div>
</div>
<button type="submit" class="btn">Continue</button>
</form>
<p
class="text-center text-sm text-muted-foreground [&_a]:font-medium [&_a]:text-foreground [&_a]:underline-offset-4 [&_a:hover]:underline"
>
or you can
<button
type="button"
class="font-medium text-foreground underline-offset-4 hover:underline"
data-two-factor-toggle=""
>
<span data-two-factor-code="">login using a recovery code</span
><span data-two-factor-recovery="" hidden=""
>login using an authentication code</span
>
</button>
</p>
<script>
{
const root = document.currentScript.closest("[data-two-factor]");
root.addEventListener("click", (e) => {
if (!e.target.closest("[data-two-factor-toggle]")) return;
for (const el of root.querySelectorAll(
"[data-two-factor-code], [data-two-factor-recovery]",
))
el.hidden = !el.hidden;
const input = root.querySelector(
"[data-two-factor-code]:not([hidden]) input, [data-two-factor-recovery]:not([hidden]) input",
);
for (const field of root.querySelectorAll("input"))
field.required = field === input;
input.value = "";
input.focus();
});
}
</script>
</div>
</div>
</div>
</div>