Text Input
A text field whose label floats above the value when focused or filled.
AI prompt
Create a text input with a floating label using only HTML and CSS. Structure: a wrapper containing an input with placeholder=" " followed by a label element linked by for/id. The field is 56px tall with a 1px border, 12px radius and 16px font size. The label starts vertically centred inside the field; when the input is focused or not showing its placeholder (:not(:placeholder-shown)) or autofilled, the label moves up to the top edge, scales to 0.78 and changes to the accent colour while focused, over 160ms ease-out. Focus shows a 2px accent border. Add helper text below ("We never share your email"). Include a second field that is disabled. Ensure font-size 16px, associate the helper text with aria-describedby, and drop the movement under prefers-reduced-motion.About this pattern
Definition
A single-line text field with a label that sits inside the field as a placeholder and moves above the text once the field is focused or has a value. Helper text sits underneath.
Purpose & UX principles
- Saves vertical space while keeping the label visible after the user starts typing, which a plain placeholder cannot do.
- The label is a real
labelelement tied to the input, so tapping it focuses the field and screen readers announce it. - Never rely on placeholder text as the only label.
Interaction states
- Empty, focused (accent border), filled (label stays floated), disabled (muted), with helper text.
- Browser autofill is detected with
:autofillso the label floats and does not overlap the filled value.
Motion specification
- Label: translateY and scale, 160ms ease-out.
- Border colour and ring: 160ms.
- Reduced motion: label moves instantly.
Responsive behaviour
- Field height 56px, full width of its container, font size at least 16px so mobile browsers do not zoom on focus.
- Labels longer than the field truncate with an ellipsis when floated.
Code
<div class="form">
<div class="field"><input id="e" type="email" placeholder=" " aria-describedby="h" autocomplete="email"><label for="e">Email address</label><p id="h" class="help">We never share your email.</p></div>
<div class="field"><input id="n" type="text" placeholder=" " autocomplete="name"><label for="n">Full name</label></div>
<div class="field"><input id="d" type="text" placeholder=" " disabled value="Locked value"><label for="d">Disabled</label></div>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.form{display:grid;gap:22px;width:min(340px,100%)}
.field{position:relative}
.field input{width:100%;height:56px;padding:20px 14px 6px;border-radius:12px;border:1px solid var(--line);background:var(--surface);color:var(--text);font:inherit;font-size:16px;outline:none;transition:border-color .16s,box-shadow .16s}
.field label{position:absolute;left:15px;top:16px;color:var(--muted);pointer-events:none;transform-origin:left top;transition:transform .16s ease-out,color .16s;white-space:nowrap;max-width:calc(100% - 30px);overflow:hidden;text-overflow:ellipsis}
.field input:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(23,23,23,.12)}
.field input:focus+label,.field input:not(:placeholder-shown)+label,.field input:-webkit-autofill+label{transform:translateY(-10px) scale(.78)}
.field input:focus+label{color:var(--accent)}
.field input:disabled{opacity:.5}
.help{margin:6px 4px 0;font-size:12.5px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){.field label,.field input{transition:none}}// The floating label is pure CSS. This optional line selects the text when a filled field gets focus.
document.querySelectorAll('.field input:not(:disabled)').forEach(i=>i.addEventListener('focus',()=>{if(i.value)i.select();}));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Text Input — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.form{display:grid;gap:22px;width:min(340px,100%)}
.field{position:relative}
.field input{width:100%;height:56px;padding:20px 14px 6px;border-radius:12px;border:1px solid var(--line);background:var(--surface);color:var(--text);font:inherit;font-size:16px;outline:none;transition:border-color .16s,box-shadow .16s}
.field label{position:absolute;left:15px;top:16px;color:var(--muted);pointer-events:none;transform-origin:left top;transition:transform .16s ease-out,color .16s;white-space:nowrap;max-width:calc(100% - 30px);overflow:hidden;text-overflow:ellipsis}
.field input:focus{border-color:var(--accent);box-shadow:0 0 0 3px rgba(23,23,23,.12)}
.field input:focus+label,.field input:not(:placeholder-shown)+label,.field input:-webkit-autofill+label{transform:translateY(-10px) scale(.78)}
.field input:focus+label{color:var(--accent)}
.field input:disabled{opacity:.5}
.help{margin:6px 4px 0;font-size:12.5px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){.field label,.field input{transition:none}}
</style></head>
<body><div class="stage">
<div class="form">
<div class="field"><input id="e" type="email" placeholder=" " aria-describedby="h" autocomplete="email"><label for="e">Email address</label><p id="h" class="help">We never share your email.</p></div>
<div class="field"><input id="n" type="text" placeholder=" " autocomplete="name"><label for="n">Full name</label></div>
<div class="field"><input id="d" type="text" placeholder=" " disabled value="Locked value"><label for="d">Disabled</label></div>
</div>
</div>
<script>
// The floating label is pure CSS. This optional line selects the text when a filled field gets focus.
document.querySelectorAll('.field input:not(:disabled)').forEach(i=>i.addEventListener('focus',()=>{if(i.value)i.select();}));
</script>
</body></html>