Check out the comments for line by line explanations. Form-related code starts on line 145.
Before we get started, we have to understand how labels, placeholders, and input fields work and how they are used from a UX perspective.
placeholder=""
attribute.
:placeholder-shown
pseudo-selector.*
We'll be using several pseudo-selectors to create this effect.
This is the ONLY way to achieve this effect without any JavaScript.
:focus
— Represents an element (such as a form input) that has received focus.
:not()
— Prevents the items passed into the argument from being selected.
:placeholder
— Represents the placeholder text of
input
and
textarea
elements.
:placeholder-shown
— Represents any
input
or
textarea
element that is currently displaying placeholder text.
A non-SCSS version can be found here for those that don't use SCSS. Note that this older CodePen has a ROOT font-size of 10px, so any 'rem' values are based on 10px.