Introduction
This is a single HTML file with a plain, bold login form: a username, a password and a Sign in button, drawn with thick black borders and soft blue offset shadows. The fun starts when you click the eye next to the password. The page turns dark, the password appears in yellow, and a cone of warm light shines out of the eye. Move your mouse or drag your finger and the beam turns to follow it, like a torch in a dark room. Click the eye again and the lights come back on.
There is no library, no canvas and no SVG. The eye is one rotated box, the dark theme is a handful of CSS custom properties swapped by one class, and the beam is a single div cut into a triangle with clip-path. About forty lines of JavaScript place the beam on the eye and point it at the pointer.
How to build a password toggle that screen readers understand — how to draw an eye with one border and two pseudo-elements — how to switch a whole theme by redefining custom properties — how to cut a cone of light out of a div with clip-path and blend it with mix-blend-mode — how to aim an element at the pointer with Math.atan2 — and how to keep a rotation from spinning the long way round.
Step 1 — Real Labels, Real Autocomplete, a Real Button
Each field is a label and an input linked with for and id, so clicking the label focuses the input and screen readers read the label out. The password input and the eye button share a wrapper, so the button can sit inside the right edge of the field. The beam is a separate div at the end of the body, outside the form, so it can cover the whole page.
<form class="login" id="login" novalidate>
<h1 class="login__title">Sign in</h1>
<div class="field">
<label class="field__label" for="username">Username</label>
<div class="field__control">
<input class="field__input" type="text" id="username" name="username"
autocomplete="username" autocapitalize="off" spellcheck="false" required>
</div>
</div>
<div class="field">
<label class="field__label" for="password">Password</label>
<div class="field__control">
<input class="field__input" type="password" id="password" name="password"
autocomplete="current-password" spellcheck="false" required>
<button class="eye-toggle" type="button" id="eye-toggle"
aria-controls="password" aria-pressed="false" aria-label="Show password">
<span class="eye" aria-hidden="true"></span>
</button>
</div>
</div>
<button class="login__submit" type="submit">Sign in</button>
<p class="login__status" id="status" role="status" aria-live="polite"></p>
</form>
<div class="beam" id="beam" aria-hidden="true"></div>The eye is a real button with type="button", so pressing it never submits the form. aria-pressed tells assistive tech that it is a toggle and whether it is on, and the aria-label changes between Show password and Hide password. The status paragraph has role="status", so messages like Please fill in both fields are read out without moving focus.
Many demos add autocomplete="off" and data-lpignore to stop browsers and password managers from touching the fields. On a login form that does more harm than good: people who rely on a password manager can no longer sign in easily. autocomplete="username" and autocomplete="current-password" tell the browser exactly what each field is for.
Step 2 — Thick Borders and Two Offset Shadows
All the colours live as custom properties on :root. The look comes from a 2 pixel solid border, square corners, and two hard shadows with no blur: one inset at the top left of the field and one outside at the bottom right. Both use the same see-through blue, so the field looks slightly pushed in and lifted at the same time.
:root {
--bg: #f4f1ea;
--ink: #121212;
--accent: #2f6bff;
--shadow: rgba(47, 107, 255, .22);
--beam: #ffd94a;
--field-bg: #fff;
}
.field__input {
width: 100%;
padding: .8rem 3.5rem .8rem .9rem; /* room on the right for the eye */
font: inherit;
font-size: 1.2rem;
color: var(--ink);
background: var(--field-bg);
border: 2px solid var(--ink);
border-radius: 0;
box-shadow:
inset 5px 5px 0 var(--shadow),
4px 4px 0 var(--shadow);
appearance: none;
}
.field__input:focus-visible {
outline: 3px solid var(--accent);
outline-offset: 3px;
}The right padding of 3.5rem keeps the typed text from running under the eye button. :focus-visible shows the outline for keyboard users but not after a mouse click, and outline-offset leaves a small gap so the ring does not merge with the border.
The Sign in button uses the same shadow. On :active it moves 2 pixels down and right while its shadow shrinks by 2 pixels, so it looks like it is being pressed into the page.
.login__submit {
color: var(--bg);
background: var(--ink);
box-shadow: 4px 4px 0 var(--shadow);
transition: transform .1s, box-shadow .1s;
}
.login__submit:hover { box-shadow: 6px 6px 0 var(--shadow); }
.login__submit:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--shadow); }Step 3 — One Rotated Box, a Pupil and a Slash
The eye shape is a small square with a border, where two opposite corners are rounded and the other two are sharp: border-radius: 75% 0. Turned 45 degrees, the sharp corners point left and right and the rounded ones become the top and bottom lids. That is an almond eye from a single element.
.eye {
position: relative;
width: 1.25rem;
height: 1.25rem;
border: 2px solid currentColor;
border-radius: 75% 0; /* round top-left + bottom-right only */
transform: rotate(45deg);
}
/* the pupil */
.eye::before {
content: '';
position: absolute;
inset: 0;
margin: auto;
width: 40%;
height: 40%;
border-radius: 50%;
background: currentColor;
transition: transform .25s;
}The pupil is centred with inset: 0 and margin: auto, which works for any absolutely placed box with a set size. Everything uses currentColor, so the eye follows the text colour of the button and changes with the theme for free.
Closed and open states
While the password is hidden, a diagonal line is drawn over the eye with the button's ::after, and the pupil shrinks to 40% of its size. When aria-pressed becomes true, the line scales to zero along its length and the pupil grows back. The CSS reads the ARIA attribute directly, so there is no extra class to keep in sync.
.eye-toggle::after {
content: '';
position: absolute;
width: 1.9rem;
height: 2px;
background: currentColor;
box-shadow: 0 0 0 2px var(--field-bg); /* small gap where it crosses the eye */
transform: rotate(-45deg) scaleX(1);
transition: transform .25s;
}
.eye-toggle[aria-pressed="true"]::after { transform: rotate(-45deg) scaleX(0); }
.eye-toggle[aria-pressed="false"] .eye::before { transform: scale(.4); }Using [aria-pressed="true"] as the selector means the look can never disagree with what a screen reader announces. If the JavaScript forgets to update the attribute, the icon is visibly wrong too, so the bug is caught straight away.
Step 4 — Redefine the Variables, Not the Rules
Every colour in the page comes from a custom property, so the dark theme does not need to restyle any element. Adding is-revealed to the body redefines the same variables, and every rule that uses them updates at once. The page goes near-black, the ink goes cream, and the accent and shadows turn the beam's yellow.
body.is-revealed {
--bg: #0c0c10;
--ink: #f4f1ea;
--accent: var(--beam);
--shadow: rgba(255, 217, 74, .14);
--field-bg: #15151b;
}
body {
background: var(--bg);
color: var(--ink);
transition: background-color .35s, color .35s;
}
/* a couple of extra touches only in the dark */
body.is-revealed .field__input { border-style: dashed; box-shadow: none; }
body.is-revealed #password { color: var(--beam); letter-spacing: .04em; }Custom properties themselves cannot be animated here, but the properties that use them can. Because body, the inputs and the button all have a transition on background-color and color, the switch fades over a third of a second instead of flashing. The dashed borders are a small hint that the room is now dark and only the beam gives real light.
One class on the body, five variables redefined, and the whole page changes mood.
Step 5 — Cut a Triangle, Fill It with a Glow
The beam is one fixed div, much wider and taller than the screen. clip-path: polygon(0 50%, 100% 0, 100% 100%) keeps only a triangle with its tip at the middle of the left edge, opening out to the right. The tip is the bulb of the torch. Its position comes from two variables, --beam-x and --beam-y, which JavaScript sets to the centre of the eye.
.beam {
position: fixed;
left: var(--beam-x);
top: var(--beam-y);
z-index: 2;
width: 150vmax;
height: 70vmax;
clip-path: polygon(0 50%, 100% 0, 100% 100%);
background: radial-gradient(
ellipse 100% 60% at 0 50%,
rgba(255, 217, 74, .55),
rgba(255, 217, 74, .18) 45%,
transparent 75%
);
mix-blend-mode: screen;
transform-origin: 0 50%;
transform: translateY(-50%) rotate(var(--beam-angle));
opacity: 0;
pointer-events: none;
transition: opacity .3s, transform .12s linear;
}
body.is-revealed .beam { opacity: 1; }translateY(-50%) moves the div up by half its height, so its left-middle point, the tip of the cone, sits exactly on the eye. transform-origin: 0 50% is that same point, so rotate() swings the cone around the eye instead of around the middle of the div. 150vmax is wider than any screen, so the end of the cone is never seen, even when the eye is at one edge of the page.
Making it look like light
A flat yellow triangle looks like a sticker. A radial gradient that starts bright at the tip and fades to transparent gives it a hot centre and soft fall-off. mix-blend-mode: screen only ever brightens what is under it, so the dark background glows warm, the cream text stays readable, and the beam never darkens anything. pointer-events: none lets clicks pass straight through it to the fields.
vmax is 1% of the larger side of the viewport. Sizing the beam in vmax means it is big enough on a tall phone and on a wide monitor without any media queries, and it keeps the same shape when the screen is rotated.
Step 6 — From Pointer Position to Angle
Pointing at something is a question of angle. Take the gap from the eye to the pointer, dx to the right and dy downwards, and Math.atan2(dy, dx) gives the angle in radians. It handles every quarter of the circle and never divides by zero. Multiply by 180 / π for degrees. Because the cone points right at 0 degrees and screen y grows downwards, that angle can go straight into rotate().
let angle = 180; // default: aim left, across the password text
let pointer = null;
function eyeCentre() {
const r = toggle.getBoundingClientRect();
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
}
function render() {
frame = 0;
const o = eyeCentre();
if (pointer) {
const deg = Math.atan2(pointer.y - o.y, pointer.x - o.x) * 180 / Math.PI;
angle += ((deg - angle + 540) % 360) - 180; // shortest turn
}
root.style.setProperty('--beam-x', o.x + 'px');
root.style.setProperty('--beam-y', o.y + 'px');
root.style.setProperty('--beam-angle', angle + 'deg');
}Never the long way round
atan2 returns values from -180 to 180. If the pointer crosses the line straight left of the eye, the angle jumps from 179 to -179. Set that directly and the transition would spin the beam almost a full circle. Instead the script works out the difference, wraps it into the range -180 to 180 with ((diff + 540) % 360) - 180, and adds it to the last angle. A move from 179 to -179 becomes a 2 degree step, and the stored angle can grow past 180 without any problem.
Before the first pointer move, or for keyboard users, the angle stays at 180 degrees, so the beam lies across the password text. Recalculating the eye's centre on every render also keeps the beam on the eye after a resize, a scroll or the phone keyboard opening.
One update per frame
pointermove can fire many times per frame on a fast mouse. The handler only saves the position and asks for a frame. requestAnimationFrame runs render() once before the next paint, and the frame variable stops a second request being queued. pointermove also covers touch and pen, so dragging a finger on a phone moves the beam too.
let frame = 0;
function schedule() {
if (!frame) frame = requestAnimationFrame(render);
}
window.addEventListener('pointermove', (e) => {
pointer = { x: e.clientX, y: e.clientY };
if (body.classList.contains('is-revealed')) schedule();
}, { passive: true });
window.addEventListener('resize', schedule);
window.addEventListener('scroll', schedule, { passive: true });The beam is position: fixed, so it lives in viewport coordinates. getBoundingClientRect() also returns viewport coordinates, so the pointer must use clientX and clientY. Mixing in pageX would push the beam off target as soon as the page scrolls.
Step 7 — One Function for Both States
setRevealed(on) does everything in one place: it swaps the input type between password and text, updates aria-pressed and the label, toggles the body class, and asks for a render so the beam starts at the right spot. Clicking the eye calls it with the opposite of the current state, then moves focus back to the password with the caret at the end, so the person can keep typing.
function setRevealed(on) {
password.type = on ? 'text' : 'password';
toggle.setAttribute('aria-pressed', String(on));
toggle.setAttribute('aria-label', on ? 'Hide password' : 'Show password');
body.classList.toggle('is-revealed', on);
schedule();
}
toggle.addEventListener('click', () => {
setRevealed(password.type === 'password');
password.focus();
const end = password.value.length;
password.setSelectionRange(end, end);
});The form uses novalidate so the browser's own pop-ups do not appear, and the submit handler does a simple check instead. If a field is empty it says so in the status line. Otherwise it greets the user and calls setRevealed(false), so the password is hidden again before anything else happens.
form.addEventListener('submit', (e) => {
e.preventDefault();
const user = form.username.value.trim();
if (!user || !password.value) {
status.textContent = 'Please fill in both fields.';
return;
}
status.textContent = 'Welcome back, ' + user + '!';
setRevealed(false);
});Respecting reduced motion
The beam's transform transition is only added inside a prefers-reduced-motion: no-preference query, and a reduce query sets every transition to zero. People who have asked their system for less motion still get the lights-off effect and the beam, but it snaps to the pointer instead of gliding.
@media (prefers-reduced-motion: no-preference) {
.beam { transition: opacity .3s, transform .12s linear; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { transition-duration: 0s !important; }
}Tuning Reference
| Value | Example | Effect |
|---|---|---|
| --beam | #ffd94a | Colour of the light, the revealed password and the dark-mode accent |
| --shadow | rgba(47,107,255,.22) | Colour of the offset field and button shadows |
| beam width / height | 150vmax / 70vmax | Length of the cone and how wide it opens |
| gradient stops | .55 → .18 at 45% → 0 at 75% | How bright the beam is and how fast it fades |
| mix-blend-mode | screen | Makes the beam brighten what is under it instead of covering it |
| beam transform transition | .12s linear | How closely the beam trails the pointer |
| default angle | 180deg | Where the beam points before the pointer moves |
| eye border-radius | 75% 0 | How round the eyelids are |
| theme transition | .35s | How fast the page fades between light and dark |
Full Source Code
The whole project is a single self-contained HTML file with its markup, styles and script together and no build step. The only external request is the Google Fonts stylesheet for JetBrains Mono, and the page falls back to the system monospace font if it does not load. It works in any current Chrome, Edge, Safari or Firefox.
// Full source available in the Aduok GitHub repository
// https://github.com/aduok/aduok-code-snippets/blob/main/blog/flashlight-password-login.html