DEV Community

Gaute Meek Olsen
Gaute Meek Olsen

Posted on

4 1

CSS - input floating label animation ✨

Input elements are often used in web development. But using only a placeholder to describe the fields are harmful.

So let's create a floating label with very little code. Check out the code in this CodePen and try out the result.

I have used some JavaScript to detect when the field has a value, but that could also be done with required or placeholder and a not selector.

<input type="text" required/>
input:not(:invalid) + span

or

<input type="text" placeholder=" "/>
input:not(:placeholder-shown) + span

Also I'm looking forward to the lh unit in CSS which are coming, so I don't have to specify the line-height.

But, remember that floating labels are problematic, as this has some of the drawbacks mentioned in that article.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE