FormsPremium

Underline Animation

A bare field with a grey underline: step into it and an indigo line starts from the middle and reaches both ends in 0.3 s. The line belongs to the container, so it stays as long as focus is inside the group.

CSSFocusPseudo-element

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Forms has 21 effects, including 2 free. Explore the category →

3 usage examples

Number of jars in a honey farm's basket — the line holds from the field to the − and + buttons — Underline Animation example 1

① Number of jars in a honey farm's basket — the line holds from the field to the − and + buttons

WhenThe customer reaches the linden honey quantity with the keyboard: the amber line is drawn under the whole group. One more Tab lands on "+", two presses take the quantity from 2 to 4 without the line moving. It only withdraws when leaving the group.
WhyThe line belongs to the container, not to the field: the number and its two buttons read as one control, and you always know which basket row you are on, even when focus sits on a button.
SettingsThe − and + buttons are placed inside .udn-field, set in a row with display:flex; --udn-width:112px, --udn-accent:#b45309, --udn-line:transparent (the resting line is drawn under the whole group by an inset shadow from the scene), centred digits. Each field has its own aria-label ("Quantity of linden honey"). The buttons and the totals are scene code; focus and presses are played by its script.
A word looked up in an online dialect dictionary — one large frameless field — Underline Animation example 2

② A word looked up in an online dialect dictionary — one large frameless field

WhenThe page opens on a question and a field in very large letters. As soon as you step in, the brick-red line crosses the column from edge to edge; "garbelot" is typed and its definition shows underneath.
WhyOn a page made for reading, a form box would stick out. A line under 37 px letters keeps the tone of a book, and the stretching line gives the only signal needed: this is where you write.
Settings--udn-width:100%, --udn-font-size:2.3rem, --udn-accent:#9a3412, --udn-line:#cdc3af, --udn-text:#1c1a17, --udn-muted:#6d6558, serif font set on the root. aria-label="Word to look up", form with role="search". The definition is shown by the scene, which also plays the typing.
The "To" and "Subject" lines of a new message — a town hall's internal mail — Underline Animation example 3

③ The "To" and "Subject" lines of a new message — a town hall's internal mail

WhenThe clerk types the department's address then presses Tab: the green line leaves the "To" line and is drawn under the "Subject" line, caption included.
WhyHere the caption stays on screen to the left of the input: the flaw of the vanishing hint no longer applies. Each line is a <label>, and the line underlines the caption and what you type in one stroke.
SettingsContainer written as <label class="udn-field"> with display:flex, the caption in a span before the field: no aria-label, no placeholder. --udn-width:100%, --udn-accent:#0f766e, --udn-line:transparent, --udn-font-size:.86rem. Made-up example.com address; typing and the move from one line to the next are played by the scene.

How it works

The .udn-input field has no frame and no fill, only a 2 px grey bottom border. The coloured line is not a tag: it is the ::after pseudo-element of the .udn-field container, laid on the same line, 0 wide and placed at left: 50%.

The .udn-field:focus-within::after rule brings it to left: 0 and width: 100%. Both values change together in 0.3 s, so the line seems to open from the centre. On the way out it closes back towards the middle the same way.

:focus-within does not only watch the field: any element of the container that takes focus keeps the line on. Put a button next to the field, inside .udn-field, and the line underlines both while you move from one to the other (measured: a 280 px line with focus on the button).

There is no label that moves up. As shipped, the field says what it expects through its placeholder, which the browser clears at the first letter. The .fx-0292-root root brings the dark demo background and the box-sizing.

Accessibility

  • Field name: it comes from aria-label="Your name"; the identical grey text is only a hint, and it goes away as soon as you type. Keep the effect as is for a field whose meaning is obvious, or give it a visible caption as in the mail example.
  • Hint colour: the effect sets it itself to #a1a1aa (7.7:1 on the demo background). Without that rule each browser picked its own grey, and Chromium's dropped to 4.3:1.
  • Lines: the resting underline is pale (white at 20%, 1.8:1); the active line, indigo, reaches 4.4:1. On your own background, tune --udn-line and --udn-accent to keep at least 3:1 on the active line.
  • Keyboard: Tab is enough, the line is drawn when focus arrives wherever it comes from (click, tap, Tab, focus()). No other focus cue is drawn on the field: the line is that cue.
  • System-imposed colours: in high-contrast mode, the line takes the system's selection colour.
  • Reduced motion: the line appears and disappears at once, without stretching.
  • Touch: 44 px tall, 16 px text.

Browser compatibility

:focus-within sets the floor: Chrome 60, Firefox 52, Safari 10.1 (@mdn/browser-compat-data); ::placeholder and CSS variables are older. Run on 04/12/2026 in Chromium 149, Firefox 151 and WebKit 26.5: two instances on the page (typing in the second, the first one's line at 0 px), container written as a <label>, focus on a button of the group, 390 px touch screen, reduced motion, forced colours in Chromium and Firefox.

Chrome 60+✓ Full (measured in Chromium 149)
Firefox 52+✓ Full (measured in Firefox 151)
Safari 10.1+✓ Full (measured in WebKit 26.5)
Edge 79+✓ Full (Chromium engine)
iOS Safari 10.3+✓ Not checked on a real device
Android Chrome 60+✓ Full (Chromium engine)

Without :focus-within the coloured line is never drawn: what remains is a grey-underlined field, usable, with no visual feedback on focus. No JavaScript is loaded.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="fx-0292-root">
  <div class="udn-field">
    <input class="udn-input" type="text" name="name" autocomplete="name" aria-label="Your name" placeholder="Your name">
  </div>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
Width (CSS — --udn-width) 280px Width of the container, hence of the line. 100% to follow the column.
Active line colour (CSS — --udn-accent) #6366f1 A solid colour. For a gradient, write it straight into the background of .udn-field::after.
Resting underline (CSS — --udn-line) white at 20% transparent if you draw the line yourself under a group wider than the field.
Texts (CSS — --udn-text, --udn-muted, --udn-font-size) #fff · #a1a1aa · 1rem Typed value, hint text, size. On a light background, darken both colours.
Where the line starts (CSS — left: 50% in .udn-field::after) the middle left: 0 makes it start from the left; left: auto; right: 0 from the right (then remove the left: 0 of the :focus-within rule).
Thickness and duration (CSS — height, border-bottom, transition) 2px · 0.3 s Change the thickness in both places so the line covers the grey one exactly.
Caption (HTML — aria-label, placeholder) Your name The two texts go together: translate them together, or replace them with a visible <label>.

FAQ

Underline Focus has a floating label that rises above the field and an indigo-to-magenta gradient line, carried by a separate tag and tied to the field alone. Underline Animation is more stripped down: a solid line, drawn by the container itself, that answers to anything taking focus inside it, and no moving label. The first suits a classic form; this one suits a field whose caption is already shown elsewhere, or several controls that make up a single setting.
Turn the container into a <label> and put the caption inside: <label class="udn-field" style="display:flex;align-items:center;gap:12px"><span>Subject</span><input class="udn-input" type="text" name="subject"></label>. Then remove aria-label and placeholder. The line will underline the caption and the field together.
Because that button is inside .udn-field: the line follows the focus of the whole container. This is intended for a field that comes with − and + buttons or a clear cross. If the button should not count, move it out of the container.