shadcn.io is not affiliated with official shadcn/ui
Textarea examples · Labeled
Textarea with Floating Label for React
A textarea whose label sits inside it like a placeholder and floats up on focus.
A floating label saves the line a label above the field would take. The Label sits over the empty Textarea like a placeholder and moves to the top edge, smaller, when the field gets focus or has text. It's CSS only: the Textarea is a peer, and the label reads its :placeholder-shown and :focus states.
Preview
Installation
FAQ
How do peer selectors enable floating label animation?
The Textarea has the peer class and comes before the label, so the label's peer-placeholder-shown: and peer-focus: classes apply when the Textarea is empty or focused. Empty, the label sits at top-3 in text-sm; focused or filled, it rests at the top in text-xs, and transition-all animates the move.
Why does the textarea need placeholder with space character?
:placeholder-shown only matches a field that has a placeholder and no value. A single space is an invisible placeholder that makes it match while the Textarea is empty, so the label can sit inside it and float once there's text.
Does the floating label need state to know when the textarea has text?
No. :placeholder-shown stops matching as soon as there's text, typed or set as a default value, so the label floats without any state or handler. Bind the value only if your form needs it for something else.
Why add pt-5 padding to the textarea?
pt-5 keeps the first line of text below the floated label, so they never overlap. While the field is empty, the label covers that space itself, acting as the placeholder.
Related
- Textarea with LabelA textarea with a visible label above it
- Textarea with DescriptionA textarea with a description between its
- Textarea with Inline LabelA three-row textarea with its label
- Simple TextareaA plain textarea with a placeholder and
- Required TextareaA required textarea with a red asterisk
- Textarea with Character CountA textarea with a live count of its