Last updated on October 18, 2023

Divi form styling

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Style forms in Divi using code.

The code snippet below demonstrates how to style a Divi form using custom CSS. This can be useful when you want to customize the appearance of your Divi form to match your website’s design.

/* Add custom CSS to style Divi form */
.wpcf7-form {
  /* Add your custom styles here */
}

.wpcf7-form p {
  /* Add your custom styles here */
}

.wpcf7-form input[type="text"],
.wpcf7-form input[type="email"],
.wpcf7-form textarea {
  /* Add your custom styles here */
}

.wpcf7-form input[type="submit"] {
  /* Add your custom styles here */
}

In the code snippet above, we target the .wpcf7-form class, which is the main container for the Divi form. You can add your custom styles to this class to modify the overall appearance of the form.

We also target the p elements within the form using the .wpcf7-form p selector. This allows you to style the text or labels associated with the form fields.

To style specific form fields, such as text inputs, email inputs, and textareas, we use the .wpcf7-form input[type="text"], .wpcf7-form input[type="email"], and .wpcf7-form textarea selectors respectively. You can add your custom styles to these selectors to modify the appearance of the form fields.

Lastly, we target the submit button using the .wpcf7-form input[type="submit"] selector. This allows you to style the submit button according to your design preferences.

Remember to replace .wpcf7-form with the appropriate class or ID of your Divi form container.

Examples

Example 1: Styling a Divi form using CSS

This example demonstrates how to style a Divi form using CSS. By adding custom CSS code to your WordPress theme, you can modify the appearance of the form elements to match your design preferences.

/* Add custom CSS to style Divi form */
.wpcf7-form input[type="text"],
.wpcf7-form input[type="email"],
.wpcf7-form textarea {
    /* Add your custom styles here */
}

.wpcf7-form input[type="submit"] {
    /* Add your custom styles here */
}

In this code example, we target the form elements using their CSS selectors and apply custom styles to them. You can modify the input[type="text"], input[type="email"], and textarea selectors to match the specific form fields you want to style. Similarly, you can modify the input[type="submit"] selector to style the submit button.

Example 2: Adding custom classes to Divi form elements

This example demonstrates how to add custom classes to Divi form elements. By adding custom classes, you can target specific form fields and apply custom styles or functionality to them.

/**
 * Add custom classes to Divi form elements
 */
function wpsnippets_add_custom_classes($classes, $tag) {
    if ($tag == 'input' || $tag == 'textarea') {
        $classes .= ' custom-class';
    }
    return $classes;
}
add_filter('wpcf7_form_element_class', 'wpsnippets_add_custom_classes', 10, 2);

In this code example, we use the wpcf7_form_element_class filter hook to add a custom class (custom-class) to all input and textarea elements in the Divi form. You can modify the conditional statement to target specific form fields based on their attributes or other criteria.

Example 3: Modifying the HTML structure of Divi form

This example demonstrates how to modify the HTML structure of a Divi form. By modifying the HTML structure, you can rearrange form elements, add additional markup, or customize the form layout.

/**
 * Modify the HTML structure of Divi form
 */
function wpsnippets_modify_form_markup($form_markup, $form) {
    // Modify the form markup as needed
    $form_markup = str_replace('<p>', '<div>', $form_markup);
    $form_markup = str_replace('</p>', '</div>', $form_markup);
    return $form_markup;
}
add_filter('wpcf7_form_elements', 'wpsnippets_modify_form_markup', 10, 2);

In this code example, we use the wpcf7_form_elements filter hook to modify the form markup. In this case, we replace the <p> tags with <div> tags to change the HTML structure of the form. You can modify the $form_markup variable to make any necessary changes to the form’s HTML structure.

Last updated on October 18, 2023. Originally posted on November 23, 2023.

Leave a Reply