

You may need seen types the place the placeholder textual content is animated whenever you begin typing into that enter. This sample isn’t new, but nonetheless stays a well-liked development in UX type design.
At present, we’ll be taught the steps wanted for including this conduct right into a type constructed with Contact Form 7 (CF7), one of the vital well-liked WordPress contact type plugins out there.
What We’ll be Constructing
Right here’s a fast video demo of the WordPress type in motion:
Word: This tutorial assumes that you’re considerably accustomed to WordPress and Contact Kind 7.
Contact Kind 7 Plugins on Codecanyon
Occupied with extending your Contact Kind 7 rapidly and simply? Codecanyon is home to loads of scripts and plugins to take your CF7 types to the following stage. For instance, Multi Step for Contact Form 7 Pro is a good way to enhance the UX of your lengthy types:

With that mentioned, assuming that you simply’ve put in Contact Kind 7 to a WordPress venture, let’s get again into the tutorial and focus on the steps wanted for animating our CF7 type labels.
1. Take away Pointless Tags
Step one is to take away the additional tags that Contact Kind 7 throws within the type.

To do that, we’ll set the worth of the WPCF7_AUTOP
constant to false
within the wp-config.php
file, like this:
2. Create the Contact Kind
The second step is to create the shape from the WordPress dashboard.

We’ll use a mix of Contact Kind 7 shortcodes together with some customized HTML.
The shape will include two enter
components, a textarea
component, and a submit button. Additionally, we’ll affiliate every of the enter
and textarea
components with a label
component. Every label will behave as a placeholder.
Right here’s the code that we now have so as to add to the Contact Kind 7 editor:
<h1>Contact Us</h1> <ul> <li class="form-wrapper"> [text* your-fullname id:your-fullname] <label for="your-fullname">Full Identify</label> </li> <li class="form-wrapper"> [email* your-email id:your-email] <label for="your-email">Electronic mail</label> </li> <li class="form-wrapper form-textarea-wrapper"> [textarea* your-message id:your-message] <label for="your-message">Message</label> </li> <li class="form-wrapper form-submit-wrapper"> [submit "Submit"] <svg width="24" peak="24" viewBox="0 0 24 24"> <path d="M24 21h-24v-18h24v18zm-23-16.477v15.477h22v-15.477l-10.999 10-11.001-10zm21.089-.523h-20.176l10.088 9.171 10.088-9.171z"/> </svg> </li> </ul>
3. Add the CSS Kind Kinds
Transferring on, we’ll enqueue a brand new CSS file within the capabilities.php
file of our venture, like this (your vacation spot folder could differ although):
wp_enqueue_style( 'customized', get_template_directory_uri() . '/property/css/customized.css' );
Inside it, we’ll place a bunch of kinds:
- We’ll begin with some reset (generic) ones. Simply to make the shape a little bit extra enticing, we’ll use a customized Google Font. Remember to enqueue this font if you wish to use it in your venture.
- Subsequent, we’ll outline the primary kinds for our type. As we’ll see later, the shape will obtain the
form-with-animated-labels
class. This may assist us isolate it and keep away from conflicts with different kinds. - Lastly, we’ll customise a little bit bit the looks of a few of the CF7 response messages.
Most significantly, take a look at how we type the labels. These are completely positioned components and vertically centered inside their .form-wrapper
guardian. As we’ll see in a bit, when their associated type component receives focus, they are going to be moved on high of it by way of the centered
class.
Listed here are all of the required kinds:
/* RESET RULES & INITIAL SET UP –––––––––––––––––––––––––––––––––––––––––––––––––– */ :root { --white: #fff; --black: #222; --brown: #9f4631; --gray: #9a9a9a; --lightgray: #f2f3f5; } * { padding: 0; margin: 0; box-sizing: border-box; border: none; define: none; } enter, textarea { font-family: inherit; font-size: 100%; } [type="submit"] { cursor: pointer; } textarea { resize: none; } ul { list-style: none; } physique { font: 20px/24px "IBM Plex Sans", sans-serif; } type { max-width: 600px; padding: 0 15px; margin: 100px auto; } h1 { font-size: 60px; line-height: 78px; margin-bottom: 50px; } /* FORM ELEMENTS –––––––––––––––––––––––––––––––––––––––––––––––––– */ .form-with-animated-labels .form-wrapper { place: relative; } .form-with-animated-labels .form-wrapper + .form-wrapper { margin-top: 40px; } .form-with-animated-labels [type="text"], .form-with-animated-labels [type="email"], .form-with-animated-labels textarea { width: 100%; padding: 15px 10px; border: 1px strong clear; border-radius: 5px; coloration: var(--black); background: var(--lightgray); font-size: 18px; } .form-with-animated-labels textarea { peak: 150px; } .form-with-animated-labels [type="text"]:focus, .form-with-animated-labels [type="email"]:focus, .form-with-animated-labels textarea:focus { border-color: var(--brown); } .form-with-animated-labels label { place: absolute; high: 50%; left: 10px; font-size: 18px; rework: translateY(-50%); coloration: var(--gray); transition: all 0.25s ease-in-out; } .form-with-animated-labels .form-textarea-wrapper label { high: 10px; rework: none; } .form-with-animated-labels label.centered { high: -22px; rework: none; font-size: 13px; font-weight: daring; coloration: var(--black); } .form-with-animated-labels [type="submit"] { min-width: 160px; padding: 20px 4px; border-radius: 10px; width: 100%; font-size: 22px; font-weight: daring; text-transform: uppercase; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08); coloration: var(--white); background: var(--brown); } .form-with-animated-labels .form-submit-wrapper svg { place: absolute; high: 0; left: 0; rework: rotate(40deg); opacity: 0.75; fill: var(--white); width: 60px; peak: 60px; } .form-with-animated-labels .wpcf7-form-control-wrap { place: static; } .form-with-animated-labels .wpcf7-not-valid-tip { place: absolute; backside: 100%; proper: 10px; font-size: 12px; } /* OUTPUT MESSAGES CF7 –––––––––––––––––––––––––––––––––––––––––––––––––– */ .wpcf7 type .wpcf7-response-output { font-size: 18px; padding: 10px; margin: 0; } .wpcf7 type.invalid .wpcf7-response-output, .wpcf7 type.unaccepted .wpcf7-response-output, .wpcf7 type.despatched .wpcf7-response-output { border-color: var(--brown); }
Word: For this instance, I’m utilizing a vanilla theme. Relying in your theme although, a few of these kinds could also be overridden. Provided that reality, if the shape doesn’t seem as anticipated in your venture, make sure you test your theme kinds and make the required modifications.
Tip: You should use conditional tags to load this CSS file solely on the pages that embrace the shape.
Why a CSS-Solely Strategy Gained’t Work
Earlier than transferring on, let me make clear one factor.
You’ve gotten most likely seen many CSS-only implementations for creating floating labels on the internet. These primarily benefit from the “CSS checkbox hack method”. Certainly, it’s a really useful method which we now have mentioned many occasions within the past.
Sadly on this case, we can’t use it. When you examine the markup that’s generated from Contact Kind 7’s shortcodes, you’ll discover that every one type controls are wrapped inside a span
component.

Because of this, the controls and their related labels aren’t siblings and a method like this gained’t have any impact:
.form-with-animated-labels [type="text"]:focus + label { // kinds for the animation right here }
4. Add the JavaScript
As a subsequent step, we’ll enqueue a brand new JavaScript file within the capabilities.php
file of our venture, like this (your vacation spot folder could differ although):
wp_enqueue_script( 'customized', get_template_directory_uri() . '/property/js/customized.js', array(), '', true );
Inside it, we’ll carry out the next actions:
- Loop by way of all of the
.form-with-animated-labels
types. - For every type, we’ll seize its goal controls (
enter
s,textarea
). In your case, these could differ. - Then, for every management, we’ll hear for the
focus
andblur
occasions. These occasions might be accountable for animating the labels by toggling thecentered
class which we outlined earlier. - Lastly, we’ll use one in every of CF7’s AJAX events to carry again the labels to their preliminary place. Be at liberty to make use of any of these occasions in accordance with your wants.
Right here’s the corresponding JavaScript code:
const formsWithAnimatedLabels = doc.querySelectorAll( ".form-with-animated-labels" ); const focusedClass = "centered"; for (const type of formsWithAnimatedLabels) { const formControls = type.querySelectorAll( '[type="text"], [type="email"], textarea' ); for (const formControl of formControls) { formControl.addEventListener("focus", operate () { this.parentElement.nextElementSibling.classList.add(focusedClass); }); formControl.addEventListener("blur", operate () { if (!this.worth) { this.parentElement.nextElementSibling.classList.take away( focusedClass ); } }); } type.parentElement.addEventListener("wpcf7mailsent", operate () { const labels = doc.querySelectorAll(".centered"); for (const label of labels) { label.classList.take away(focusedClass); } }); }
Tip: You should use conditional tags to load this JavaScript file solely on the pages that embrace the shape.
5. Name the Contact Kind
Final however not least, we’ll embrace the generated Contact Kind 7 shortcode within the desired a part of our venture:
[contact-form-7 404 "Not Found"]
Discover the category added by way of the html_class
attribute. That is our acquainted form-with-animated-labels
class.
Conclusion
And we’re accomplished, of us! On this brief tutorial, we mentioned a easy methodology for customizing the appear and feel of a Contact Kind 7 type by including floating labels. I’m positive there might be different efficient options as nicely, but this one appears to do its job! Hopefully you’ll give it a attempt in one in every of your upcoming WordPress initiatives.
Have you ever ever created such an impact with Contact Kind 7? Have you ever managed to give you a CSS-only answer? Share your ideas within the feedback beneath.
As at all times, thanks quite a bit for studying!
Extra Contact Kind 7 Customization and Floating Labels
If you’re curious to learn to construct a responsive handmade SVG type with Contact Kind 7, or want to dive deeper into floating labels, check out these tutorials: