As @somethinghere comment mentioned, you will run into accessibility / usability issues when the input fields are not empty, which removes the placeholder. Although somewhat different from your original approach, using a combination of CSS positioning, HTML tags and flexbox can achieve the same effect without losing the indicators of each field when they are filled.
The approach is simple:
- Insert input elements and labels into
<div>
elements. Shortcuts must appear after to enter for the adjacent sibling +
selector to work - Use flexbox to place a label in front of the entry by changing the direction of the column using
flex-direction: column-reverse
. The advantage of this approach is that you can change the order of the elements as you like, for example, on mobile screens, etc. - Use
+
to move the labels by changing their top
property when the companion input / text area is in focus.
Here is an example of evidence:
div { display: flex; flex-direction: column-reverse; } input, textarea { display: block; font-family: 'Arial'; font-size: 1em; } label { display: block; font-family: 'Arial'; font-size: 1em; position: relative; top: 1.5em; left: .35em; transition: all .5s ease-in-out; } input:focus + label, textarea:focus + label { top: 0; }
<div> <input type="text" id="edit-field-name-0-value" value="" size="60" maxlength="255" placeholder="Name *" required="required"> <label for="edit-field-name-0-value">Name *</label> </div> <div> <textarea rows="8" cols="60" placeholder="Message *" required="required" id="textarea0"></textarea> <label for="textarea0">Message *</label> </div>
If you want to use CSS transforms instead of positioning top
, this is also possible:
div { display: flex; flex-direction: column-reverse; } input, textarea { display: block; font-family: 'Arial'; font-size: 1em; } label { display: block; font-family: 'Arial'; font-size: 1em; position: relative; transform: translateY(1.5em); left: .35em; transition: all .5s ease-in-out; } input:focus + label, textarea:focus + label { transform: translateY(0); }
<div> <input type="text" id="edit-field-name-0-value" value="" size="60" maxlength="255" placeholder="Name *" required="required"> <label for="edit-field-name-0-value">Name *</label> </div> <div> <textarea rows="8" cols="60" placeholder="Message *" required="required" id="textarea0"></textarea> <label for="textarea0">Message *</label> </div>
source share