An input and a textarea whose label rests inside the field and moves up into the frame on focus or once there is a value. The state comes from CSS alone (:placeholder-shown and peer), so it also works with browser autofill and without client JavaScript.
Browser Support
Tracks whether the field is empty
Styles elements based on their contents
Highlights the focused field frame
Preview
Focus a field or start typing. The email field shows its error after you leave it.
Installation
Usage
import {
FloatingLabelInput,
FloatingLabelTextarea,
} from "@/components/ui/floating-label-input"
export default function Example() {
return (
<form className="grid gap-4">
<FloatingLabelInput label="Email" type="email" name="email" />
<FloatingLabelTextarea label="Message" name="message" />
</form>
)
}Examples
Two variants
outline puts the floated label on the border, inset keeps it at the top inside the frame.
<FloatingLabelInput label="City" />
<FloatingLabelInput label="City" variant="inset" />Prefix and suffix
Slots take icons, currency signs, units or a button. A password visibility toggle is a regular button in the suffix.
import { Button } from "@/components/ui/button"
<FloatingLabelInput label="Search the docs" prefix={<Search />} />
<FloatingLabelInput label="Budget" prefix="$" suffix="USD" inputMode="decimal" />
<FloatingLabelInput
label="Password"
type={visible ? "text" : "password"}
suffix={
<Button type="button" onClick={() => setVisible(!visible)} aria-label="Show password">
{visible ? <EyeOff /> : <Eye />}
</Button>
}
/>Hint and error
hint and error render under the field and are linked to it through aria-describedby. An error replaces the hint and sets aria-invalid. Clear the field to see the hint again.
This username is taken
<FloatingLabelInput
variant="inset"
label="Username"
prefix={<AtSign />}
value={username}
onChange={(e) => setUsername(e.target.value)}
hint="Letters, digits and dots"
error={taken ? "This username is taken" : undefined}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | - | Label text. Also the accessible name of the field |
| hint | ReactNode | - | Helper text under the field |
| error | ReactNode | - | Error text. Marks the field invalid and replaces the hint |
| prefix | ReactNode | - | Content before the value |
| suffix | ReactNode | - | Content after the value |
| variant | "outline" | "inset" | "outline" | Where the floated label goes |
| placeholder | string | " " | Shown only on focus. A single space keeps :placeholder-shown working when there is none |
| className | string | - | Classes for the outer wrapper |
| frameClassName | string | - | Classes for the bordered frame |
| ...props | ComponentProps<"input" | "textarea"> | - | Any input or textarea attribute, including ref |
Components
| Component | Description |
|---|---|
| FloatingLabelInput | Single-line field |
| FloatingLabelTextarea | Multiline field with the same props, rows defaults to 4 |
Features
- CSS only: no focus or value state in JavaScript; works in Server Components
- Autofill: the :autofill selector floats the label when the browser fills the field
- Accessibility: a real label element, aria-describedby for hint and error, aria-invalid on error
- Reduced motion: the label moves without a transition