From b39810ef013bba082a3b38e4c22ea577cebafd13 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Thu, 19 Sep 2019 12:35:12 -0400 Subject: Add material text fields post --- ...9-09-19-almost-pure-css-material-text-fields.md | 207 +++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 _posts/2019-09-19-almost-pure-css-material-text-fields.md diff --git a/_posts/2019-09-19-almost-pure-css-material-text-fields.md b/_posts/2019-09-19-almost-pure-css-material-text-fields.md new file mode 100644 index 0000000..1d78cc4 --- /dev/null +++ b/_posts/2019-09-19-almost-pure-css-material-text-fields.md @@ -0,0 +1,207 @@ +--- +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 `