How we can create an expanding contact form which expands from an icon or button to form? Solution: See this Expanding Contact Form With Email Validation, Icon To Form Expands.
Previously I have shared some contact forms, but this is a unique one because it is an expanding form. Basically, we use a contact form on our website because the user can easily contact about any query or complaint. And every website uses a separate page for contact information and forms. But if we use this kind of expanding form, then we can place it on every page on the website.
Today you learn to create an icon to form expands program. Basically, there is an SMS/Email icon at the bottom of the page. When you will click on it it will expand and become a contact form. There are some input fields for name, email, and select option, etc on the form, also some titles and icons like contact icon and close icon. You can close the form by click outside of form and press the close icon. There is an email validation function and success message panel.
So, Today I am sharing Expanding Contact Form With Email Validation. There I have used HTML to create the layout, CSS for styling, and jQuery for functioning. When form expands then an overlay effect comes on the page, like that the program has many little animations and effects. This is a very interesting concept, if you use this form on your website then you can put it on every page because it doesn’t take much space.
If you are thinking now how this expanding overlay contact form actually is, then see the preview given below.
Preview Of Icon To Form Expands
See this video preview to getting an idea of how this form animation looks like.
Now you can see this program visually, also you can see it live by pressing the button above. If you like this, then get the source code of its.
You May Also Like:
- Text To Search Input Animation
- Neumorphic Tabs Design
- Responsive FAQ Section
- Moving Input Focus Effect
Expanding Contact Form Source Code
Before sharing source code, let’s talk about it. First I have created the main div with class name ‘form-overlay‘ and placed all the contents inside it. There I have used the font-awesome library to placing the envelope and close icons. After that, I have placed divs and other tags for text and inputs for creating the form’s fields. Also in the HTML file, I have linked other files like CSS, JS, and jQuery CDN.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. With CSS, first I gave basic values like size, position, margin, padding, etc to all the elements. There I have used CSS transition command for animation and cubic-bezier command to define the animation curve. I also used CSS @keyframe command to create the animation effects.
jQuery handling here forms open/close, validation, and error/success features in this program. I used .toggleClass command to open and close toggle function in the form. There I have defined an email pattern for validation and visualize messages after checking using if{} else{} statements. Left all other things you will understand after getting the codes, I can’t explain all in writing.
For creating the program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Expanding Contact Form | Webdevtrick.com</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href='https://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="./style.css"> </head> <body> <div class='form-overlay'></div> <div class='icon fa fa fa-envelope' id='form-container'> <span class='icon fa fa-close' id='form-close'></span> <div id='form-content'> <div id='form-head'> <h1 class='pre'>Get in touch</h1> <p class='pre'>Cotact Us...</p> <h1 class='post'>Thanks!</h1> <p class='post'>I'll be in touch ASAP</p> </div> <form> <input class='input name' name='user_name' placeholder='Your name' type='text'> <input class='input email' name='user_email' placeholder='contact email' type='text'> <select class='input select' name='subject'> <option disabled=''>What shall we talk about?</option> <option selected=''>About a new project</option> <option>About a job opportunity</option> <option>About any complaint</option> </select> <textarea class='input message' placeholder='How can I help?'></textarea> <input class='input submit' type='submit' value='Send Message'> </form> </div> </div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ body { font-family: Roboto, sans-serif; width: 100%; font-size: 16px; margin: 0; padding: 0; background: #FAFAFA; } h1, h2 { font-weight: 700; color: #FFF; font-weight: 700; font-size: 4em; margin: 0; padding: 0 20px; } .form-overlay { width: 0%; height: 100%; top: 0; left: 0; position: fixed; opacity: 0; background: #000; -webkit-transition: background 1s, opacity 0.4s, width 0s 0.4s; transition: background 1s, opacity 0.4s, width 0s 0.4s; } .show-form-overlay .form-overlay { width: 100%; opacity: 0.7; z-index: 999; -webkit-transition: background 1s, opacity 0.4s, width 0s; transition: background 1s, opacity 0.4s, width 0s; } .show-form-overlay.form-submitted .form-overlay { background: #119DA4; -webkit-transition: background 0.6s; transition: background 0.6s; } #form-container { cursor: pointer; color: #FFF; z-index: 1000; position: absolute; margin: 0 auto; left: 0; right: 0; bottom: 5vh; background-color: #f72f4e; overflow: hidden; border-radius: 50%; width: 60px; max-width: 60px; height: 60px; text-align: center; box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23); -webkit-transition: all 0.2s 0.45s, height 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.25s, max-width 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.35s, width 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.35s; transition: all 0.2s 0.45s, height 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.25s, max-width 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.35s, width 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.35s; } #form-container.expand { cursor: auto; box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.17); border-radius: 0; width: 70%; height: 610px; max-width: 610px; padding: 0; -webkit-transition: all 0.2s, max-width 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.1s, height 0.3s ease 0.25s; transition: all 0.2s, max-width 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0.1s, height 0.3s ease 0.25s; } #form-close { cursor: pointer; } .icon::before { cursor: pointer; font-size: 30px; line-height: 60px; display: block; -webkit-transition: all 0.7s cubic-bezier(0.4, 0, 0.2, 1); transition: all 0.7s cubic-bezier(0.4, 0, 0.2, 1); } .icon:hover::before { -webkit-animation: wiggle 0.2s linear infinite; animation: wiggle 0.2s linear infinite; } fa-envelope::before { display: block; } .fa-close::before { display: none; } .expandfa-envelope::before { display: none; } .expand.fa-close::before { display: block; -webkit-animation: none; animation: none; } #form-content { font-family: Roboto, sans-serif; -webkit-transform: translateY(150%); transform: translateY(150%); width: 100%; opacity: 0; text-align: left; -webkit-transition: opacity 0.2s 0.2s, -webkit-transform 0.2s cubic-bezier(0.4, 0, 0.2, 1); transition: opacity 0.2s 0.2s, -webkit-transform 0.2s cubic-bezier(0.4, 0, 0.2, 1); transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s 0.2s; transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s 0.2s, -webkit-transform 0.2s cubic-bezier(0.4, 0, 0.2, 1); } #form-content.expand { -webkit-transform: translateY(0px); transform: translateY(0px); opacity: 1; -webkit-transition: opacity 0s, -webkit-transform 0.7s cubic-bezier(0.4, 0, 0.2, 1) 0.3s; transition: opacity 0s, -webkit-transform 0.7s cubic-bezier(0.4, 0, 0.2, 1) 0.3s; transition: transform 0.7s cubic-bezier(0.4, 0, 0.2, 1) 0.3s, opacity 0s; transition: transform 0.7s cubic-bezier(0.4, 0, 0.2, 1) 0.3s, opacity 0s, -webkit-transform 0.7s cubic-bezier(0.4, 0, 0.2, 1) 0.3s; } #form-content form { color: #FFF; width: 100%; height: 100%; padding: 0 20px 20px 20px; margin-bottom: 10px; box-sizing: border-box; text-align: left; } #form-head { font-size: 100%; padding: 0; margin: 0 20px; color: #FFF; text-align: center; -webkit-transition: all 0.8s 0.6s; transition: all 0.8s 0.6s; } #form-head h1, #form-head p { padding: 0; margin: 0; } #form-head .pre { display: block; } #form-head .post { display: none; } .form-submitted#form-head { -webkit-transform: translateY(250%); transform: translateY(250%); } .form-submitted#form-head .pre { display: none; } .form-submitted#form-head .post { display: block; } .input { background: rgba(0, 0, 0, 0.2); display: block; height: 50px; width: 100%; margin: 10px 0; padding: 0 10px; border-width: 0; box-sizing: border-box; border: none; outline: none; box-shadow: none; -webkit-transform: translateX(0); transform: translateX(0); } ::-webkit-input-placeholder { /* Safari, Chrome and Opera */ color: rgba(255, 255, 255, 0.8); font-size: 90%; } /* Firefox 18- */ :-moz-placeholder { color: rgba(255, 255, 255, 0.8); font-size: 90%; } /* Firefox 19+ */ ::-moz-placeholder { color: rgba(255, 255, 255, 0.8); font-size: 90%; } /* IE 10+ */ :-ms-input-placeholder { color: rgba(255, 255, 255, 0.8); font-size: 90%; } /* Edge */ ::-ms-input-placeholder { color: rgba(255, 255, 255, 0.8); font-size: 90%; } /* Default */ :placeholder-shown { color: rgba(255, 255, 255, 0.8); font-size: 90%; text-transform: uppercase; letter-spacing: 3px; } input, select, textarea { color: #FFF; } .input.message { resize: none; height: 150px; padding: 10px; } .input.submit { background-color: #FFF; color: #f72f4e; font-size: 120%; height: 50px; box-shadow: 0 5px rgba(0, 0, 0, 0.5); -webkit-transition: all 0.1s, -webkit-transform 0s 0.6s; transition: all 0.1s, -webkit-transform 0s 0.6s; transition: all 0.1s, transform 0s 0.6s; transition: all 0.1s, transform 0s 0.6s, -webkit-transform 0s 0.6s; } .input.submit:active { margin-top: 15px; box-shadow: 0 0 rgba(0, 0, 0, 0.5); } .input.form-error { -webkit-animation: error 0.8s ease; animation: error 0.8s ease; background: rgba(0, 0, 0, 0.7); } select option { background: #f72f4e; color: #FFF; border: none; box-shadow: none; outline: none; } select option:disabled { font-style: italic; color: rgba(255, 255, 255, 0.9); font-size: 90%; } .input { -webkit-transition: -webkit-transform 0s 1s; transition: -webkit-transform 0s 1s; transition: transform 0s 1s; transition: transform 0s 1s, -webkit-transform 0s 1s; } .form-submitted .input { -webkit-transform: translateX(150%); transform: translateX(150%); opacity: 0; -webkit-transition: all 0.5s, -webkit-transform 0.4s cubic-bezier(0.4, 0, 0.2, 1) 0s; transition: all 0.5s, -webkit-transform 0.4s cubic-bezier(0.4, 0, 0.2, 1) 0s; transition: all 0.5s, transform 0.4s cubic-bezier(0.4, 0, 0.2, 1) 0s; transition: all 0.5s, transform 0.4s cubic-bezier(0.4, 0, 0.2, 1) 0s, -webkit-transform 0.4s cubic-bezier(0.4, 0, 0.2, 1) 0s; } .form-submitted .input:nth-child(1) { -webkit-transition-delay: 0.1s; transition-delay: 0.1s; } .form-submitted .input:nth-child(2) { -webkit-transition-delay: 0.2s; transition-delay: 0.2s; } .form-submitted .input:nth-child(3) { -webkit-transition-delay: 0.3s; transition-delay: 0.3s; } .form-submitted .input:nth-child(4) { -webkit-transition-delay: 0.4s; transition-delay: 0.4s; } .form-submitted .input:nth-child(5) { -webkit-transition-delay: 0.5s; transition-delay: 0.5s; } input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #FFF inset; } @media (max-width: 600px) { #form-container.expand { height: 100%; width: 100%; max-width: 100%; overflow: initial; overflow-x: hidden; bottom: 0; } h1 { font-size: 300%; } .icon:hover::before { -webkit-animation: none; animation: none; } .form-overlay { display: none; -webkit-transition: none; transition: none; } } @-webkit-keyframes error { 0%, 100% { -webkit-transform: translateX(0); transform: translateX(0); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: translateX(-6px); transform: translateX(-6px); } 20%, 40%, 60%, 80% { -webkit-transform: translateX(6px); transform: translateX(6px); } } @keyframes error { 0%, 100% { -webkit-transform: translateX(0); transform: translateX(0); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: translateX(-6px); transform: translateX(-6px); } 20%, 40%, 60%, 80% { -webkit-transform: translateX(6px); transform: translateX(6px); } } @keyframes wiggle { 0%, 100% { -webkit-transform: rotate(-15deg); transform: rotate(-15deg); } 50% { -webkit-transform: rotate(15deg); transform: rotate(15deg); } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
// Code By Webdevtrick ( https://webdevtrick.com ) var formContainer = $('#form-container'); bindFormClick(); //Opening the form function bindFormClick(){ $(formContainer).on('click', function(e) { e.preventDefault(); toggleForm(); //Ensure container doesn't togleForm when open $(this).off(); }); } //Closing the form $('#form-close, .form-overlay').click(function(e) { e.stopPropagation(); e.preventDefault(); toggleForm(); bindFormClick(); }); function toggleForm(){ $(formContainer).toggleClass('expand'); $(formContainer).children().toggleClass('expand'); $('body').toggleClass('show-form-overlay'); $('.form-submitted').removeClass('form-submitted'); } //Form validation $('form').submit(function() { var form = $(this); form.find('.form-error').removeClass('form-error'); var formError = false; form.find('.input').each(function() { if ($(this).val() == '') { $(this).addClass('form-error'); $(this).select(); formError = true; return false; } else if ($(this).hasClass('email') && !isValidEmail($(this).val())) { $(this).addClass('form-error'); $(this).select(); formError = true; return false; } }); if (!formError) { $('body').addClass('form-submitted'); $('#form-head').addClass('form-submitted'); setTimeout(function(){ $(form).trigger("reset"); }, 1000); } return false; }); function isValidEmail(email) { var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i; return pattern.test(email); }; |
That’s It. Now you have successfully created Expanding Contact Form With Email Validation, Icon To Form Expands. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
very beautiful work..really appreciatable and helpful…
can you make a logo through css animation ??
sure, I will post soon.
Can you explain, in this design on this https://webdevtrick.com/expanding-contact-form/?unapproved=32260&moderation-hash=fcdc876d8004702045c9872d887c3dd4#comment-32260 website page, where the recipient’s email is located, thank you shan
Where does the form go to?
Website management https://webdevtrick.com/, you did not respond to comments, so why put such a system on your website?
Very interesting, is it possible to explain where the recipient’s email is, thank you
Can you explain, in this plan, where the recipient’s email is located, everything I read about html, java, php, for a while I do not have time to practice, forgets the process, thank you shaan
Website management https://webdevtrick.com/, you did not respond to comments, so why put such a system on your website?