--- title: (Almost) Pure CSS Material-like Text Fields --- Despite what you may believe from simply looking at this site, I've actually done quite a bit of front-end development. A couple of years ago, I worked on a project with a friend of mine. For part of the project, he'd designed the behavior of a form control inspired by Material Design which I then built from scratch. Recently, he asked me to remind him how I'd implemented it, and I thought I'd take the opportunity to turn it into a blog post. Here's what it looks like:
It's not _quite_ pure CSS, but it's pretty close. Let's think about how this is put together. At a high level, the appearance of the text field at any given moment is the result of two CSS classes, `focused` and `populated`, being added and removed via JavaScript. On this page, I've simply written a few lines of code to add and remove them at the proper times, but in practice this is probably best done through your frontend JavaScript framework (Angular/React/Vue/...), if you're using one. First, let's talk about the moving placeholder. While CSS does have a `::placeholder` pseudo-element that we can use for styling how the `placeholder` attribute of the `` is displayed, unfortunately we can't use it here because we want the placeholder to remain visible while the user edits the field, and the browser-supplied placeholder vanishes when the field isn't empty. Another semantically-useful way to display this is the `