Advertisement
  1. Web Design
  2. UX/UI
  3. Material Design

How to Customize Contact Form 7 for WordPress: Floating Labels

Scroll to top
Read Time: 8 min

You might have seen forms where the placeholder text is animated when you start typing into that input. This pattern isn’t new, yet still remains a popular trend in UX form design.

Today, we’ll learn the steps needed for adding this behavior into a form built with Contact Form 7 (CF7), one of the most popular WordPress contact form plugins available.

What We’ll be Building

Here’s a quick video demo of the WordPress form in action:

Note: This tutorial assumes that you are somewhat familiar with WordPress and Contact Form 7.

Contact Form 7 Plugins on Codecanyon

Interested in extending your Contact Form 7 quickly and easily? Codecanyon is home to loads of scripts and plugins to take your CF7 forms to the next level. For example, Multi Step for Contact Form 7 Pro is a great way to improve the UX of your long forms:

Multi step contact form 7Multi step contact form 7Multi step contact form 7

With that said, assuming that you’ve installed Contact Form 7 to a WordPress project, let’s get back into the tutorial and discuss the steps needed for animating our CF7 form labels.

1. Remove Unnecessary Tags

The first step is to remove the extra tags that Contact Form 7 throws in the form.

The extra tags that Contact Form 7 throws in the formThe extra tags that Contact Form 7 throws in the formThe extra tags that Contact Form 7 throws in the form

To do this, we'll set the value of the WPCF7_AUTOP constant to false in the wp-config.php file, like this:

2. Create the Contact Form

The second step is to create the form from the WordPress dashboard. 

Contact Form 7 ShortcodeContact Form 7 ShortcodeContact Form 7 Shortcode

We’ll use a combination of Contact Form 7 shortcodes along with some custom HTML.

The form will consist of two input elements, a textarea element, and a submit button. Also, we’ll associate each of the input and textarea elements with a label element. Each label will behave as a placeholder.

Here’s the code that we have to add to the Contact Form 7 editor:

1
<h1>Contact Us</h1>
2
<ul>
3
  <li class="form-wrapper">
4
    [text* your-fullname id:your-fullname]
5
    <label for="your-fullname">Full Name</label>
6
  </li>
7
  <li class="form-wrapper">
8
    [email* your-email id:your-email]
9
    <label for="your-email">Email</label>
10
  </li>
11
  <li class="form-wrapper form-textarea-wrapper">
12
    [textarea* your-message id:your-message]
13
    <label for="your-message">Message</label>
14
  </li>
15
  <li class="form-wrapper form-submit-wrapper">
16
    [submit "Submit"]
17
    <svg width="24" height="24" viewBox="0 0 24 24">
18
      <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"/>
19
    </svg>
20
  </li>
21
</ul>

3. Add the CSS Form Styles

Moving on, we’ll enqueue a new CSS file in the functions.php file of our project, like this (your destination folder may differ though):

1
wp_enqueue_style( 'custom', get_template_directory_uri() . '/assets/css/custom.css' );

Inside it, we’ll place a bunch of styles:

  • We’ll start with some reset (generic) ones. Just to make the form a little more attractive, we’ll use a custom Google Font. Be sure to enqueue this font if you want to use it in your project.
  • Next, we’ll define the main styles for our form. As we’ll see later, the form will receive the form-with-animated-labels class. This will help us isolate it and avoid conflicts with other styles.
  • Finally, we’ll customize a little bit the appearance of some of the CF7 response messages.

Most importantly, have a look at how we style the labels. These are absolutely positioned elements and vertically centered inside their .form-wrapper parent. As we’ll see in a bit, when their related form element receives focus, they will be moved on top of it via the focused class. 

Here are all the required styles:

1
/* RESET RULES & INITIAL SET UP

2
–––––––––––––––––––––––––––––––––––––––––––––––––– */
3
:root {
4
    --white: #fff;
5
    --black: #222;
6
    --brown: #9f4631;
7
    --gray: #9a9a9a;
8
    --lightgray: #f2f3f5;
9
}
10
11
* {
12
    padding: 0;
13
    margin: 0;
14
    box-sizing: border-box;
15
    border: none;
16
    outline: none;
17
}
18
19
input,
20
textarea {
21
    font-family: inherit;
22
    font-size: 100%;    
23
}
24
25
[type="submit"] {
26
    cursor: pointer;
27
}
28
29
textarea {
30
    resize: none;
31
}
32
33
ul {
34
    list-style: none;
35
}
36
37
body {
38
    font: 20px/24px "IBM Plex Sans", sans-serif;
39
}
40
41
form {
42
    max-width: 600px;
43
    padding: 0 15px;
44
    margin: 100px auto;
45
}
46
47
h1 {
48
    font-size: 60px;
49
    line-height: 78px;
50
    margin-bottom: 50px;
51
}
52
53
54
/* FORM ELEMENTS

55
–––––––––––––––––––––––––––––––––––––––––––––––––– */
56
.form-with-animated-labels .form-wrapper {
57
    position: relative;
58
}
59
60
.form-with-animated-labels .form-wrapper + .form-wrapper {
61
    margin-top: 40px;
62
}
63
64
.form-with-animated-labels [type="text"],
65
.form-with-animated-labels [type="email"],
66
.form-with-animated-labels textarea {
67
    width: 100%;
68
    padding: 15px 10px;
69
    border: 1px solid transparent;
70
    border-radius: 5px;
71
    color: var(--black);
72
    background: var(--lightgray);
73
    font-size: 18px;
74
}
75
76
.form-with-animated-labels textarea {
77
    height: 150px;
78
}
79
80
.form-with-animated-labels [type="text"]:focus,
81
.form-with-animated-labels [type="email"]:focus,
82
.form-with-animated-labels textarea:focus {
83
    border-color: var(--brown);
84
}
85
86
.form-with-animated-labels label {
87
    position: absolute;
88
    top: 50%;
89
    left: 10px;
90
    font-size: 18px;
91
    transform: translateY(-50%);
92
    color: var(--gray);
93
    transition: all 0.25s ease-in-out;
94
}
95
96
.form-with-animated-labels .form-textarea-wrapper label {
97
    top: 10px;
98
    transform: none;
99
}
100
101
.form-with-animated-labels label.focused {
102
    top: -22px;
103
    transform: none;
104
    font-size: 13px;
105
    font-weight: bold;
106
    color: var(--black);
107
}
108
109
.form-with-animated-labels [type="submit"] {
110
    min-width: 160px;
111
    padding: 20px 4px;
112
    border-radius: 10px;
113
    width: 100%;
114
    font-size: 22px;
115
    font-weight: bold;
116
    text-transform: uppercase;
117
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
118
    color: var(--white);
119
    background: var(--brown);
120
}
121
122
.form-with-animated-labels .form-submit-wrapper svg {
123
    position: absolute;
124
    top: 0;
125
    left: 0;
126
    transform: rotate(40deg);
127
    opacity: 0.75;
128
    fill: var(--white);
129
    width: 60px;
130
    height: 60px;
131
}
132
133
.form-with-animated-labels .wpcf7-form-control-wrap {
134
    position: static;
135
}
136
137
.form-with-animated-labels .wpcf7-not-valid-tip {
138
    position: absolute;
139
    bottom: 100%;
140
    right: 10px;
141
    font-size: 12px;
142
}
143
144
145
/* OUTPUT MESSAGES CF7

146
–––––––––––––––––––––––––––––––––––––––––––––––––– */
147
.wpcf7 form .wpcf7-response-output {
148
    font-size: 18px;
149
    padding: 10px;
150
    margin: 0;
151
}
152
153
.wpcf7 form.invalid .wpcf7-response-output,
154
.wpcf7 form.unaccepted .wpcf7-response-output,
155
.wpcf7 form.sent .wpcf7-response-output {
156
    border-color: var(--brown);
157
}

Note: For this example, I’m using a vanilla theme. Depending on your theme though, some of these styles may be overridden. Given that fact, if the form doesn’t appear as expected in your project, be sure to check your theme styles and make the necessary changes. 

Tip: You can use conditional tags to load this CSS file only on the pages that include the form.

Why a CSS-Only Approach Won’t Work

Before moving on, let me clarify one thing.

You have probably seen many CSS-only implementations for creating floating labels on the web. These mainly take advantage of the “CSS checkbox hack technique”. Indeed, it’s a very handy technique which we have discussed many times in the past.

Unfortunately in this case, we cannot use it. If you inspect the markup that is generated from Contact Form 7’s shortcodes, you’ll notice that all form controls are wrapped within a span element.

Wrapper around a form control in Contact Form 7Wrapper around a form control in Contact Form 7Wrapper around a form control in Contact Form 7

As a result, the controls and their associated labels aren’t siblings and a style like this won’t have any effect:

1
.form-with-animated-labels [type="text"]:focus + label {
2
  // styles for the animation here
3
}

4. Add the JavaScript

As a next step, we’ll enqueue a new JavaScript file in the functions.php file of our project, like this (your destination folder may differ though):

1
wp_enqueue_script( 'custom', get_template_directory_uri() . '/assets/js/custom.js', array(), '', true );

Inside it, we’ll perform the following actions:

  1. Loop through all the .form-with-animated-labels forms.
  2. For each form, we’ll grab its target controls (inputs, textarea). In your case, these may differ.
  3. Then, for each control, we’ll listen for the focus and blur events. These events will be responsible for animating the labels by toggling the focused class which we defined earlier.
  4. Lastly, we’ll use one of CF7’s AJAX events to bring back the labels to their initial position. Feel free to use any of those events according to your needs.

Here’s the corresponding JavaScript code:

1
const formsWithAnimatedLabels = document.querySelectorAll(
2
    ".form-with-animated-labels"
3
);
4
const focusedClass = "focused";
5
6
for (const form of formsWithAnimatedLabels) {
7
    const formControls = form.querySelectorAll(
8
        '[type="text"], [type="email"], textarea'
9
    );
10
    for (const formControl of formControls) {
11
        formControl.addEventListener("focus", function () {
12
            this.parentElement.nextElementSibling.classList.add(focusedClass);
13
        });
14
15
        formControl.addEventListener("blur", function () {
16
            if (!this.value) {
17
                this.parentElement.nextElementSibling.classList.remove(
18
                    focusedClass
19
                );
20
            }
21
        });
22
    }
23
24
25
    form.parentElement.addEventListener("wpcf7mailsent", function () {
26
        const labels = document.querySelectorAll(".focused");
27
        for (const label of labels) {
28
            label.classList.remove(focusedClass);
29
        }
30
    });
31
}

Tip: You can use conditional tags to load this JavaScript file only on the pages that include the form.

5. Call the Contact Form

Last but not least, we’ll include the generated Contact Form 7 shortcode in the desired part of our project:

1
[contact-form-7 id="5" title="Contact Form With Animated Labels" html_class="form-with-animated-labels"]

Notice the class added via the html_class attribute. This is our familiar form-with-animated-labels class.

Conclusion

And we’re done, folks! In this short tutorial, we discussed a simple method for customizing the look and feel of a Contact Form 7 form by adding floating labels. I’m sure there will be other effective solutions as well, yet this one seems to do its job! Hopefully you will give it a try in one of your upcoming WordPress projects.

Have you ever created such an effect with Contact Form 7? Have you managed to come up with a CSS-only solution? Share your thoughts in the comments below.

As always, thanks a lot for reading!

More Contact Form 7 Customization and Floating Labels

If you are curious to learn how to build a responsive handmade SVG form with Contact Form 7, or would like to dive deeper into floating labels, take a look at these tutorials:

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.